c#操作access实例是怎么实现的呢?让我们来看看具体的代码:
using system; using system.data; using system.configuration; using system.web; using system.web.security; using system.web.ui; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.web.ui.htmlcontrols; using system.data.oledb; /// /// dataaccess 的摘要说明 c#操作access实例解析 /// public class dataaccess { protected static oledbconnection conn = new oledbconnection(); protected static oledbcommand comm = new oledbcommand(); public dataaccess() { //init c#操作access实例解析 } private static void openconnection() { if (conn.state == connectionstate.closed) { conn.connectionstring = @provider=microsoft.jet.oledb.4.0; data source=+configurationmanager.appsettings[myconn]; //web.config文件里设定。 comm.connection = conn; try { conn.open(); } catch (exception e) { throw new exception(e.message); } } }//打开数据库 c#操作access实例解析 private static void closeconnection() { if (conn.state == connectionstate.open) { conn.close(); conn.dispose(); comm.dispose(); } }//关闭数据库 c#操作access实例解析 public static void excutesql(string sqlstr) { try { openconnection(); comm.commandtype = commandtype.text; comm.commandtext = sqlstr; comm.executenonquery(); } catch (exception e) { throw new exception(e.message); } finally { closeconnection(); } }//执行sql语句 c#操作access实例解析 public static oledbdatareader datareader(string sqlstr) { oledbdatareader dr = null; try { openconnection(); comm.commandtext = sqlstr; comm.commandtype = commandtype.text; dr = comm.executereader(commandbehavior.closeconnection); } catch { try { dr.close(); closeconnection(); } catch { } } return dr; } //返回指定sql语句的oledbdatareader对象,使用时请注意关闭这个对象。 public static void datareader(string sqlstr, ref oledbdatareader dr) { try { openconnection(); comm.commandtext = sqlstr; comm.commandtype = commandtype.text; dr=comm.executereader(commandbehavior.closeconnection); } catch { try { if (dr != null && !dr.isclosed) dr.close(); } //c#操作access实例解析catch { } finally { closeconnection(); } } } //返回指定sql语句的oledbdatareader对象,使用时请注意关闭 public static dataset dataset(string sqlstr) { dataset ds = new dataset(); oledbdataadapter da = new oledbdataadapter(); try { openconnection(); comm.commandtype = commandtype.text; comm.commandtext = sqlstr; da.selectcommand = comm; da.fill(ds); } catch (exception e) { throw new exception(e.message); } finally { closeconnection(); } return ds; }//返回指定sql语句的dataset c#操作access实例解析 public static void dataset( string sqlstr, ref dataset ds) { oledbdataadapter da = new oledbdataadapter(); try { openconnection(); comm.commandtype = commandtype.text; comm.commandtext = sqlstr; da.selectcommand = comm; da.fill(ds); } catch (exception e) { throw new exception(e.message); } finally { closeconnection(); } }//返回指定sql语句的dataset c#操作access实例解析 public static datatable datatable(string sqlstr) { datatable dt = new datatable(); oledbdataadapter da = new oledbdataadapter(); try { openconnection(); comm.commandtype = commandtype.text; comm.commandtext = sqlstr; da.selectcommand = comm; da.fill(dt); } catch (exception e) { throw new exception(e.message); } finally { closeconnection(); } return dt; }//返回指定sql语句的datatable public static void datatable( string sqlstr, ref datatable dt) { oledbdataadapter da = new oledbdataadapter(); try { openconnection(); comm.commandtype = commandtype.text; comm.commandtext = sqlstr; da.selectcommand = comm; da.fill(dt); } catch (exception e) { throw new exception(e.message); } finally { closeconnection(); } }//返回指定sql语句的datatable c#操作access实例解析 public static dataview dataview(string sqlstr) { oledbdataadapter da = new oledbdataadapter(); dataview dv = new dataview(); dataset ds = new dataset(); try { openconnection(); comm.commandtype = commandtype.text; comm.commandtext = sqlstr; da.selectcommand = comm; da.fill(ds); dv = ds.tables[0].defaultview; } catch (exception e) { throw new exception(e.message); } finally { closeconnection(); } return dv; } //返回指定sql语句的dataview c#操作access实例解析 }
c#操作access实例解析的基本内容就向你介绍到这里,希望对你了解和学习c#操作access有所帮助。
