Home > Database > Mysql Tutorial > body text

动态创建ACCESS数据库及其表结构

WBOY
Release: 2016-06-07 15:29:15
Original
1139 people have browsed it

1.动态创建表结构,引入com中的二个动态库,一个是ADOX,一个是ADODB二个dll,ADOX.dll和Interop.ADODB.dll 2.增加wfm中的按钮,按钮事件如下: 其中的m_path是当前程序运行的路径,可以写绝对路径,目前是相对路径。 private void button1_Click(object send

1.动态创建表结构,引入com中的二个动态库,一个是ADOX,一个是ADODB二个dll,ADOX.dll和Interop.ADODB.dll

2.增加wfm中的按钮,按钮事件如下:

其中的m_path是当前程序运行的路径,可以写绝对路径,目前是相对路径。

private void button1_Click(object sender, EventArgs e)
{
string strdb = m_path + "\\DomTest.mdb";


if (File.Exists("" + strdb + "")) //先判断当前是否存在该数据库,没有则创建
{

MessageBox.Show("当前存在数据库,请先删除掉", "信息提示");
return;
}
ADOX.Catalog catalog = new Catalog();

catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + strdb + "';Jet OLEDB:Engine Type=5");

ADODB.Connection cn = new ADODB.Connection();

cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + strdb + "'", null, null, -1);
catalog.ActiveConnection = cn;

ADOX.Table table = new ADOX.Table();
table.Name = "Table1";

ADOX.Column column = new ADOX.Column();
column.ParentCatalog = catalog;
column.Name = "RecordId"; //列名称
column.Type = DataTypeEnum.adInteger; //列数据类型
column.DefinedSize = 9;//大小
column.Properties["AutoIncrement"].Value = true; //是否是自动增加属性
table.Columns.Append(column, DataTypeEnum.adInteger, 9); //增加第一列
table.Keys.Append("TablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null);
table.Columns.Append("Name", DataTypeEnum.adVarWChar, 50); //增加第二列
table.Columns.Append("Age", DataTypeEnum.adInteger, 9); //增加第三列
table.Columns.Append("Birthday", DataTypeEnum.adDate, 0);//增加第四列
catalog.Tables.Append(table); //增加当前的表

cn.Close();
MessageBox.Show("创建数据库成功,位置存放在" + m_path, "信息提示");
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!