C#動(dòng)態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法
本文實(shí)例講述了C#動(dòng)態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法。分享給大家供大家參考。
具體實(shí)現(xiàn)方法如下:
//添加兩個(gè)com組件引用
//Microsoft ADO Ext. 2.8 for DDL and Security
//Microsoft ActiveX Data Objects 2.8 Library
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADOX;
using System.IO;
namespace WebRequestTest.Common
{
public static class AccessDbHelper
{
/// <summary>
/// 創(chuàng)建access數(shù)據(jù)庫
/// </summary>
/// <param name="filePath">數(shù)據(jù)庫文件的全路徑,如 D:\\NewDb.mdb</param>
public static bool CreateAccessDb(string filePath)
{
ADOX.Catalog catalog = new Catalog();
if (!File.Exists(filePath))
{
try
{
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;DData Source=" + filePath + ";Jet OLEDB:Engine Type=5");
}
catch (System.Exception ex)
{
return false;
}
}
return true;
}
/// <summary>
/// 在access數(shù)據(jù)庫中創(chuàng)建表
/// </summary>
/// <param name="filePath">數(shù)據(jù)庫表文件全路徑如D:\\NewDb.mdb 沒有則創(chuàng)建 </param>
/// <param name="tableName">表名</param>
/// <param name="colums">ADOX.Column對(duì)象數(shù)組</param>
public static void CreateAccessTable(string filePath, string tableName, params ADOX.Column[] colums)
{
ADOX.Catalog catalog = new Catalog();
//數(shù)據(jù)庫文件不存在則創(chuàng)建
if (!File.Exists(filePath))
{
try
{
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Jet OLEDB:Engine Type=5");
}
catch (System.Exception ex)
{
}
}
ADODB.Connection cn = new ADODB.Connection();
cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath, null, null, -1);
catalog.ActiveConnection = cn;
ADOX.Table table = new ADOX.Table();
table.Name = tableName;
foreach (var column in colums)
{
table.Columns.Append(column);
}
// column.ParentCatalog = catalog;
//column.Properties["AutoIncrement"].Value = true; //設(shè)置自動(dòng)增長
//table.Keys.Append("FirstTablePrimaryKey", KeyTypeEnum.adKeyPrimary, column, null, null); //定義主鍵
catalog.Tables.Append(table);
cn.Close();
}
//========================================================================================調(diào)用
//ADOX.Column[] columns = {
// new ADOX.Column(){Name="id",Type=DataTypeEnum.adInteger,DefinedSize=9},
// new ADOX.Column(){Name="col1",Type=DataTypeEnum.adWChar,DefinedSize=50},
// new ADOX.Column(){Name="col2",Type=DataTypeEnum.adLongVarChar,DefinedSize=50}
// };
// AccessDbHelper.CreateAccessTable("d:\\111.mdb", "testTable", columns);
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C#使用Oracle.ManagedDataAccess.dll組件連接Oracle數(shù)據(jù)庫
- C# 操作 access 數(shù)據(jù)庫的實(shí)例代碼
- c# 使用Entity Framework操作Access數(shù)據(jù)庫的示例
- C#連接Oracle數(shù)據(jù)庫使用Oracle.ManagedDataAccess.dll
- C#實(shí)現(xiàn)的ACCESS數(shù)據(jù)庫操作類完整實(shí)例
- C#編程實(shí)現(xiàn)連接ACCESS數(shù)據(jù)庫實(shí)例詳解
- c#連接access數(shù)據(jù)庫操作類分享
- C# Access數(shù)據(jù)庫增刪查改的簡(jiǎn)單方法
- C#操作Access數(shù)據(jù)庫的實(shí)現(xiàn)過程(vs2019)
相關(guān)文章
NPOI實(shí)現(xiàn)兩級(jí)分組合并功能(示例講解)
下面小編就為大家分享一篇NPOI實(shí)現(xiàn)兩級(jí)分組合并功能的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12基于WPF實(shí)現(xiàn)用戶頭像選擇器的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何基于WPF實(shí)現(xiàn)用戶頭像選擇器,文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們學(xué)習(xí)WPF有一定幫助,感興趣的可以了解一下2022-07-07C#獲取ListView鼠標(biāo)下的Item實(shí)例
下面小編就為大家?guī)硪黄狢#獲取ListView鼠標(biāo)下的Item實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01