说明: 1,采用dynamic调用com组件,适用于.net 4.0以上支持dynamic版本的才可以; 2,执行速度不敢恭维,只是因为要用于silverlight oob模式中才研究一二; 3,测试环境 .net 4.5 + silverlight 5.0 + visual studio 2013 4,见如下helper类(需引用 using
说明:1,采用dynamic调用com组件,适用于.net 4.0以上支持dynamic版本的才可以;
2,执行速度不敢恭维,只是因为要用于silverlight oob模式中才研究一二;
3,测试环境.net 4.5 + silverlight 5.0 + visual studio 2013
4,见如下helper类(需引用using system.runtime.interopservices.automation;):
1 public class slaccesshelper 2 { 3 private dynamic m_accessapp;// access.application 4 private dynamic m_database;// database 5 private dynamic m_recordset; 6 7 /// 8 /// 构造函数 9 /// 10 /// access是否可见 11 public slaccesshelper(bool visible) 12 { 13 m_accessapp = automationfactory.createobject(access.application); 14 m_accessapp.visible = visible; 15 } 16 17 /// 18 /// 打开数据库 19 /// 20 /// access数据库文件路径 21 /// 是否共享 22 /// 密码 23 public void opendb(string filepath, bool exclusive = false, string bstrpassword = ) 24 { 25 m_accessapp.opencurrentdatabase(filepath, exclusive, bstrpassword); 26 m_database = m_accessapp.currentdb(); 27 } 28 29 /// 30 /// 获取当前数据库中所有表名称集合 31 /// 32 /// 所有表名称集合 33 public liststring> gettablenames() 34 { 35 liststring> tablenames = new liststring>(); 36 dynamic tabledefs = m_database.tabledefs; 37 foreach (dynamic tabledef in tabledefs) 38 { 39 tablenames.add(tabledef.name); 40 } 41 42 return tablenames; 43 } 44 45 /// 46 /// 加载表数据 47 /// 48 /// 表名称 49 /// 表数据 50 public liststring>> loadtable(string tablename) 51 { 52 dynamic recordset = m_database.openrecordset(tablename); 53 int fieldscount = recordset.fields.count; 54 liststring>> data = new liststring>>(); 55 if (fieldscount > 0) 56 { 57 try 58 { 59 liststring> fieldnames = new liststring>(); 60 for (int i = 0; i ) 61 { 62 fieldnames.add(recordset.fields[i].name); 63 } 64 data.add(fieldnames); 65 if (!recordset.eof) 66 { 67 recordset.movefirst(); 68 while (!recordset.eof) 69 { 70 object[] datarow = recordset.getrows();// 返回一维数组 71 liststring> datarowstr = new liststring>(); 72 for (int i = 0; i ) 73 { 74 datarowstr.add(datarow[i] == null ? : datarow[i].tostring()); 75 } 76 data.add(datarowstr); 77 } 78 } 79 } 80 catch (exception ex) 81 { 82 throw new exception(ex.message); 83 } 84 finally 85 { 86 if (recordset != null) 87 { 88 recordset.close(); 89 ((idisposable)recordset).dispose(); 90 recordset = null; 91 } 92 } 93 } 94 95 return data; 96 } 97 98 /// 99 /// 添加新纪录100 /// 101 /// 表格名称102 /// 数据103 public void addnewrecord(string tablename, liststring, object>> data)104 {105 try106 {107 m_recordset = m_database.openrecordset(tablename, 1);// 1=recordsettypeenum.dbopentable108 int fieldscount = m_recordset.fields.count;109 liststring> fieldnames = new liststring>();110 for (int i = 0; i )111 {112 fieldnames.add(m_recordset.fields[i].name);113 }114 for (int rowindex = 0; rowindex )115 {116 m_recordset.addnew();117 foreach (string fieldname in fieldnames)118 {119 m_recordset.fields[fieldname].value = data[rowindex][fieldname];120 }121 m_recordset.update();122 }123 }124 catch(exception ex)125 {126 throw new exception(ex.message);127 }128 finally129 {130 if (m_recordset != null)131 {132 m_recordset.close();133 ((idisposable)m_recordset).dispose();134 m_recordset = null;135 }136 }137 }138 139 /// 140 /// 更新表格数据141 /// 142 /// 表格名称143 /// 数据144 public void updatetable(string tablename, liststring, string>> data)145 {146 try147 {148 m_recordset = m_database.openrecordset(tablename, 1);// 1=recordsettypeenum.dbopentable149 m_recordset.movefirst();150 for (int rowindex = 0; rowindex )151 {152 m_recordset.edit();153 foreach (string fieldname in data[rowindex].keys)154 {155 m_recordset.fields[fieldname].value = data[rowindex][fieldname];156 }157 m_recordset.update();158 m_recordset.movenext();159 }160 }161 catch (exception ex)162 {163 throw new exception(ex.message);164 }165 finally166 {167 if (m_recordset != null)168 {169 m_recordset.close();170 ((idisposable)m_recordset).dispose();171 m_recordset = null;172 }173 }174 }175 176 /// 177 /// 关闭178 /// 179 public void close()180 {181 if (m_database != null)182 {183 m_database.close();184 ((idisposable)m_database).dispose();185 m_database = null;186 }187 if (m_accessapp != null)188 {189 m_accessapp.closecurrentdatabase();190 // m_accessapp.quit();// 导致最后会弹出access主页面191 ((idisposable)m_accessapp).dispose();192 m_accessapp = null;193 }194 gc.collect();195 }196 }
view code
通过dynamic构建的com对象,在使用完成后都要手动关闭销毁,比如代码中的m_accessapp, m_database, m_recordset三个对象,否则只是将m_accessapp关闭清空释放掉,access进程还是无法关闭,在程序关闭之前,始终都会有一个空白的无法关闭的access界面;
在循环中处理dynamic和c#类型转换会降低程序执行效率,就比如像gettablenames方法中循环遍历表名,都要花两三秒时间,所以尽量像object[] datarow = recordset.getrows();直接获取其中的所有数据,然后再遍历处理,会极大提高执行效率;
要修改access中的数据时,一定要先m_recordset.edit();才会允许你编辑其中的内容;