用C#动态创建Access数据库
2007-10-12 19:47
323 查看
记得以前要动态的创建Access数据库的mdb文件都是采用DAO,用VC开发,一大堆的API,很是麻烦。现在好像也鲜有人提起DAO。其实动态的创建mdb数据的最简单的方法还是ADOX。
用ADOX创建access数据库方法很简单,只需要new一个Catalog对象,然后调用它的Create方法就可以了,如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ADOX.Catalog catalog = new Catalog();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:/test.mdb;Jet OLEDB:Engine Type=5");
仅仅两行代码就搞定了。下来我主要介绍一下在c#中的实现细节。首先你要添加引用,在“Add reference”对话框里切换到Com页面,选择“Microsoft ADO Ext. 2.8 for DDL and Security”,然后点击OK。在文件的开头using ADOX名字空间。然后添加如上面所示的代码就可以成功的创建Access 数据库了,代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections.Generic;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using ADOX;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace testADOX
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
class Program
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ADOX.Catalog catalog = new Catalog();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:/test.mdb;Jet OLEDB:Engine Type=5");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
创建了数据库文件是没有实际用处的,我们还要创建表。在创建表之前,我们必须连接目标数据库,用来连接数据的桥梁居然是ADO的Connection对象,所以我们不得不再次添加对ADO的应用,在添加引用对话框中切换到Com页面,选择“Microsoft ActiveX Data Objects 2.8 Library”,然后点击OK。下边是创建表的完整代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections.Generic;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using ADOX;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace testADOX
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
class Program
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ADOX.Catalog catalog = new Catalog();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:/test.mdb;Jet OLEDB:Engine Type=5");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ADODB.Connection cn = new ADODB.Connection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:/test.mdb", null, null, -1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catalog.ActiveConnection = cn;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ADOX.Table table = new ADOX.Table();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
table.Name = "FirstTable";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ADOX.Column column = new ADOX.Column();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
column.ParentCatalog = catalog;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
column.Name = "RecordId";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
column.Type = DataTypeEnum.adInteger;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
column.DefinedSize = 9;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
column.Properties["AutoIncrement"].Value = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
table.Columns.Append(column, DataTypeEnum.adInteger, 9);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
table.Columns.Append("CustomerName", DataTypeEnum.adVarWChar, 50);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
table.Columns.Append("Age", DataTypeEnum.adInteger, 9);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
table.Columns.Append("Birthday", DataTypeEnum.adDate, 0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catalog.Tables.Append(table);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cn.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
用ADOX创建access数据库方法很简单,只需要new一个Catalog对象,然后调用它的Create方法就可以了,如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ADOX.Catalog catalog = new Catalog();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:/test.mdb;Jet OLEDB:Engine Type=5");
仅仅两行代码就搞定了。下来我主要介绍一下在c#中的实现细节。首先你要添加引用,在“Add reference”对话框里切换到Com页面,选择“Microsoft ADO Ext. 2.8 for DDL and Security”,然后点击OK。在文件的开头using ADOX名字空间。然后添加如上面所示的代码就可以成功的创建Access 数据库了,代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections.Generic;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using ADOX;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace testADOX
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
class Program
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ADOX.Catalog catalog = new Catalog();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:/test.mdb;Jet OLEDB:Engine Type=5");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
创建了数据库文件是没有实际用处的,我们还要创建表。在创建表之前,我们必须连接目标数据库,用来连接数据的桥梁居然是ADO的Connection对象,所以我们不得不再次添加对ADO的应用,在添加引用对话框中切换到Com页面,选择“Microsoft ActiveX Data Objects 2.8 Library”,然后点击OK。下边是创建表的完整代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections.Generic;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using ADOX;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace testADOX
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
class Program
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ADOX.Catalog catalog = new Catalog();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:/test.mdb;Jet OLEDB:Engine Type=5");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ADODB.Connection cn = new ADODB.Connection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:/test.mdb", null, null, -1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catalog.ActiveConnection = cn;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ADOX.Table table = new ADOX.Table();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
table.Name = "FirstTable";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ADOX.Column column = new ADOX.Column();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
column.ParentCatalog = catalog;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
column.Name = "RecordId";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
column.Type = DataTypeEnum.adInteger;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
column.DefinedSize = 9;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
column.Properties["AutoIncrement"].Value = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
table.Columns.Append(column, DataTypeEnum.adInteger, 9);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
table.Columns.Append("CustomerName", DataTypeEnum.adVarWChar, 50);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
table.Columns.Append("Age", DataTypeEnum.adInteger, 9);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
table.Columns.Append("Birthday", DataTypeEnum.adDate, 0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catalog.Tables.Append(table);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cn.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- 用C#动态创建Access数据库
- 用C#动态创建Access数据库
- 用C#动态创建Access数据库
- 用C#动态创建Access数据库
- 用C#动态创建Access数据库
- 用C#动态创建Access数据库
- 用C#动态创建Access数据库
- C#中怎么创建ACCESS数据库文件
- Vc++中Ado动态创建access数据库
- 在.NET 2.0框架下动态创建Access数据库和表
- vc下动态创建access数据库的实现
- 解决了几乎所有Delphi+Access同仁的问题—动态创建、压缩Access数据库(*.MDB)
- 动态创建、压缩Access数据库(*.MDB)
- 在.NET 2.0框架下动态创建Access数据库和表
- 如何动态创建ACCESS数据库
- ODBC动态创建access数据库
- C# 动态创建Access数据库及表
- 在.NET 2.0框架下动态创建Access数据库和表
- 动态创建Access数据库和压缩Access数据库
- 【转载】C#动态创建Access数据库及表的方法