日期:2014-05-17 浏览次数:20550 次
public class OleDbHelper { static OleDbConnection conn = null; static OleDbCommand cmd = null; static string appPath = System.Web.HttpContext.Current.Request.ApplicationPath; static string connStr = "provider=microsoft.jet.oledb.4.0;data source="+System.Web.HttpContext.Current.Server.MapPath(appPath + "/App_Data/ZHXYDB.mdb")+""; public OleDbConnection OpenConnection() { try { //connStr = ConfigurationManager.ConnectionStrings["DBS"].ToString() + System.Web.HttpContext.Current.Server.MapPath(appPath + "/App_Data/ZHXYDB.mdb"); conn = new OleDbConnection(connStr); if (conn.State == ConnectionState.Closed) { conn.Open(); } } catch { } return conn; } public void CloseConnection() { if (conn.State == ConnectionState.Open) { conn.Close(); } } public void CloseConnection(OleDbConnection connection) { if (connection.State == ConnectionState.Open) { connection.Close(); } } public object DB_ExecuteScalar(string sqlStr) { OleDbConnection conn = OpenConnection(); cmd = new OleDbCommand(sqlStr, conn); return cmd.ExecuteScalar(); } public OleDbDataReader ExecuteOdr(string sqlStr, OleDbConnection conn) { cmd = new OleDbCommand(sqlStr, conn); OleDbDataReader odr = cmd.ExecuteReader(); return odr; } public int DB_ExecuuteNonQuery(string sqlStr) { OleDbConnection conn = OpenConnection(); cmd = new OleDbCommand(sqlStr, conn); int result = cmd.ExecuteNonQuery(); CloseConnection(conn); return result; } public DataTable DB_Fill(string sqlStr) { OleDbConnection conn = OpenConnection(); DataTable dt = new DataTable(); OleDbDataAdapter oda = new OleDbDataAdapter(sqlStr, conn); oda.Fill(dt); return dt; } }
public MODEL.CompanyInfo GetModel(int id) { MODEL.CompanyInfo model = new MODEL.CompanyInfo(); OleDbConnection conn = helper.OpenConnection(); sqlStr = new StringBuilder(); sqlStr.Append("select * from CompanyInfo "); sqlStr.Append("where ID=" + id + ""); OleDbDataReader odr = helper.ExecuteOdr(sqlStr.ToString (), conn); if (odr.Read() == true) { model.ID = (int)odr[0]; model.InfoTitle = odr[1].ToString(); model.InfoContent = odr[2].ToString(); } odr.Close(); helper.CloseConnection(conn); return model; }