1 usingSystem;2 usingSystem.Data;3 usingSystem.Configuration;4 usingSystem.Web;5 usingSystem.Data.OleDb;6
7 namespaceDNCWinService8 {9 /**/
10 ///
11 ///DataAccess 的摘要说明12 ///
13 public classAccessDBHelper14 {15 protected static OleDbConnection conn = newOleDbConnection();16 protected static OleDbCommand comm = newOleDbCommand();17 publicAccessDBHelper()18 {19 //init
20 }21 /**/
22 ///
23 ///打开数据库24 ///
25 private static void openConnection(stringpath)26 {27 if (conn.State ==ConnectionState.Closed)28 {29 conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + path;//web.config文件里设定。
30 comm.Connection =conn;31 try
32 {33 conn.Open();34 }35 catch(Exception e)36 { throw newException(e.Message); }37 }38 }39 /**/
40 ///
41 ///关闭数据库42 ///
43 private static voidcloseConnection()44 {45 if (conn.State ==ConnectionState.Open)46 {47 conn.Close();48 conn.Dispose();49 comm.Dispose();50 }51 }52 /**/
53 ///
54 ///执行sql语句55 ///
56 ///
57 public static void excuteSql(string sqlstr, stringpath)58 {59 try
60 {61 openConnection(path);62 comm.CommandType =CommandType.Text;63 comm.CommandText =sqlstr;64 comm.ExecuteNonQuery();65 }66 catch(Exception e)67 {68 throw newException(e.Message);69 }70 finally
71 { closeConnection(); }72 }73 /**/
74 ///
75 ///返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。76 ///
77 ///
78 ///
79 public static OleDbDataReader dataReader(string sqlstr, stringpath)80 {81 OleDbDataReader dr = null;82 try
83 {84 openConnection(path);85 comm.CommandText =sqlstr;86 comm.CommandType =CommandType.Text;87 dr =comm.ExecuteReader(CommandBehavior.CloseConnection);88 }89 catch
90 {91 try
92 {93 dr.Close();94 closeConnection();95 }96 catch{ }97 }98 returndr;99 }100 /**/
101 ///
102 ///返回指定sql语句的OleDbDataReader对象,使用时请注意关闭103 ///
104 ///
105 ///
106 public static void dataReader(string sqlstr, ref OleDbDataReader dr, stringpath)107 {108 try
109 {110 openConnection(path);111 comm.CommandText =sqlstr;112 comm.CommandType =CommandType.Text;113 dr =comm.ExecuteReader(CommandBehavior.CloseConnection);114 }115 catch
116 {117 try
118 {119 if (dr != null && !dr.IsClosed)120 dr.Close();121 }122 catch
123 {124 }125 finally
126 {127 closeConnection();128 }129 }130 }131 /**/
132 ///
133 ///返回指定sql语句的dataset134 ///
135 ///
136 ///
137 public static DataSet dataSet(string sqlstr, stringpath)138 {139 DataSet ds = newDataSet();140 OleDbDataAdapter da = newOleDbDataAdapter();141 try
142 {143 openConnection(path);144 comm.CommandType =CommandType.Text;145 comm.CommandText =sqlstr;146 da.SelectCommand =comm;147 da.Fill(ds);148 }149 catch(Exception e)150 {151 throw newException(e.Message);152 }153 finally
154 {155 closeConnection();156 }157 returnds;158 }159 /**/
160 ///
161 ///返回指定sql语句的dataset162 ///
163 ///
164 ///
165 public static void dataSet(string sqlstr, ref DataSet ds, stringpath)166 {167 OleDbDataAdapter da = newOleDbDataAdapter();168 try
169 {170 openConnection(path);171 comm.CommandType =CommandType.Text;172 comm.CommandText =sqlstr;173 da.SelectCommand =comm;174 da.Fill(ds);175 }176 catch(Exception e)177 {178 throw newException(e.Message);179 }180 finally
181 {182 closeConnection();183 }184 }185 /**/
186 ///
187 ///返回指定sql语句的datatable188 ///
189 ///
190 ///
191 public static DataTable dataTable(string sqlstr, stringpath)192 {193 DataTable dt = newDataTable();194 OleDbDataAdapter da = newOleDbDataAdapter();195 try
196 {197 openConnection(path);198 comm.CommandType =CommandType.Text;199 comm.CommandText =sqlstr;200 da.SelectCommand =comm;201 da.Fill(dt);202 }203 catch(Exception e)204 {205 throw newException(e.Message);206 }207 finally
208 {209 closeConnection();210 }211 returndt;212 }213 /**/
214 ///
215 ///返回指定sql语句的datatable216 ///
217 ///
218 ///
219 public static void dataTable(string sqlstr, ref DataTable dt, stringpath)220 {221 OleDbDataAdapter da = newOleDbDataAdapter();222 try
223 {224 openConnection(path);225 comm.CommandType =CommandType.Text;226 comm.CommandText =sqlstr;227 da.SelectCommand =comm;228 da.Fill(dt);229 }230 catch(Exception e)231 {232 throw newException(e.Message);233 }234 finally
235 {236 closeConnection();237 }238 }239 /**/
240 ///
241 ///返回指定sql语句的dataview242 ///
243 ///
244 ///
245 public static DataView dataView(string sqlstr, stringpath)246 {247 OleDbDataAdapter da = newOleDbDataAdapter();248 DataView dv = newDataView();249 DataSet ds = newDataSet();250 try
251 {252 openConnection(path);253 comm.CommandType =CommandType.Text;254 comm.CommandText =sqlstr;255 da.SelectCommand =comm;256 da.Fill(ds);257 dv = ds.Tables[0].DefaultView;258 }259 catch(Exception e)260 {261 throw newException(e.Message);262 }263 finally
264 {265 closeConnection();266 }267 returndv;268 }269
270 }271 }