namespace添加如下
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ADOX;using System.Data; using System.Data.OleDb; using System.IO;
一》创建access数据库
首先添加ADOX COM组件
添加过程如下;点击添加引用,在COM组件栏选择相应组件。
使用Microsoft ADO Ext.2.8点击确定。
C#代码实现如下
namespace Database { public class accessdatabase { public void Database() { } public void creatdb(string path) { ADOX.Catalog catalog = new Catalog(); catalog.Create(("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ path +";Jet OLEDB:Engine Type=5")); }
二》创建access表单
我们通过COM组件ADOB来实现这个功能,具体实现过程如下:
1>添加引用
使用上述COM实现,表的创建,实例代码如下,我们创建之前常用的Userdb,存储登陆用的账户密码及权限,全部是string类型 50长度,其中Username是主键。
public void User_init(string path) { if (!File.Exists(path)) { ADOX.Catalog A = new Catalog(); A.Create(("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Jet OLEDB:Engine Type=5")); ADODB.Connection cn = new ADODB.Connection(); cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+path, null, null, -1); A.ActiveConnection = cn; ADOX.Table table = new ADOX.Table(); table.Name = "User"; ADOX.Column column = new ADOX.Column(); column.ParentCatalog = A; column.Name = "User_Name"; column.Type = DataTypeEnum.adVarWChar; column.DefinedSize =50; // column.Properties["AutoIncrement"].Value = true; table.Columns.Append(column, DataTypeEnum.adVarWChar, 50); table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null); table.Columns.Append("User_ID", DataTypeEnum.adVarWChar, 50); table.Columns.Append("Pass_Word", DataTypeEnum.adVarWChar, 9); A.Tables.Append(table); } ///检查该数据库是否存在,如果数据库不存在将会新建该数据库。 }
上述代码时C#具体数据库表单的建立,
结合labview DB工具包的思路和样式我们进一步泛化,封装代码如下:
首先我们先建立一个枚举和一个结构体,结构体与labview vi中的这个簇相同,枚举用于将labview中的数据结构转换到ADO能识别的数据结构
枚举和结构体的代码如下:
public enum type:byte {String,U32,Dbl,Data} public struct columninformation { public string columnname; public type datatype; public int size; public bool keyornot; }
C#创建accesss表的实现代码如下:
public void Appendtable(string path, string table, columninformation[] informations) { ADOX.Catalog A = new Catalog(); ADODB.Connection cn = new ADODB.Connection(); cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path, null, null, -1); A.ActiveConnection = cn; ADOX.Table table1 = new ADOX.Table(); table1.Name = table; int sn = 0; while (sn < informations.Length) { ADOX.Column column = new ADOX.Column(); column.ParentCatalog = A; column.Name = informations[sn].columnname; switch (informations[sn].datatype) { case (type.String): column.Type = DataTypeEnum.adWChar; break; case (type.Data): column.Type = DataTypeEnum.adFileTime; break; case (type.Dbl): column.Type = DataTypeEnum.adDecimal; break; case (type.U32): column.Type = DataTypeEnum.adDouble; break; } column.DefinedSize = informations[sn].size; // column.Properties["AutoIncrement"].Value = true; table1.Columns.Append(column, column.Type, column.DefinedSize); if (informations[sn].keyornot) { table1.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null); }; sn += 1; } A.Tables.Append(table1); }
参数过多,我们在类库中添加字段path和table并封装创建对应的访问器代码如下
private string path; public string Path { get { return path; } set { path = value; } } private string table; public string Table { get { return table; } set { table = value; } }
将创建表单的程序再压缩封装代码如下
public void Appendtable(columninformation[] informations) { Appendtable(path, table, informations); }
回头看最初的例子,我们可以使用我们库中创建表单的程序代码了
public void User_init(string path) { if (!File.Exists(path)) { accessdatabase userdb = new accessdatabase(); userdb.Path="D:\\1.mdb"; userdb.Table="User"; userdb.creatdb(); accessdatabase.columninformation A, B, C; A.columnname = "User_Name"; A.datatype = accessdatabase.type.String; A.keyornot = true; A.size = 50; B.columnname = "User_ID"; B.datatype = accessdatabase.type.String; B.keyornot = false; B.size = 50; C.columnname = "Pass_Word"; C.datatype = accessdatabase.type.String; C.keyornot = false; C.size = 50; accessdatabase.columninformation[] M={A,B,C}; userdb.Appendtable(M); } ///检查该数据库是否存在,如果数据库不存在将会新建该数据库。 }
请发表评论