一、實(shí)現(xiàn)效果
1.1、實(shí)現(xiàn)功能
? ①實(shí)現(xiàn)創(chuàng)建Access數(shù)據(jù)庫(kù);
? ②實(shí)現(xiàn)創(chuàng)建指定Access數(shù)據(jù)庫(kù)的表;
? ③實(shí)現(xiàn)給Access數(shù)據(jù)庫(kù)的指定表【插入、查詢(xún)、更新、刪除、分頁(yè)查詢(xún)】數(shù)據(jù);
? ④實(shí)現(xiàn)獲取Access數(shù)據(jù)庫(kù)中的所有表名稱(chēng)及其表包含的所有列名稱(chēng)
1.2、效果圖
二、實(shí)現(xiàn)核心
該項(xiàng)目的完整工程下載地址如下:Access數(shù)據(jù)庫(kù)操作項(xiàng)目的完整工程下載地址https://download.csdn.net/download/xiaochenXIHUA/85163940
2.1、添加引用
? 在項(xiàng)目中添加【Microsoft ActiveX Data Objects 6.0 Library】和【Microsoft ADO Ext.6.0 for DDL and Security】引用,如下圖所示。
注意事項(xiàng):
?引入這兩個(gè)Com組件后,在使用【ADOX.CatalogClass】報(bào)如下圖的錯(cuò)誤時(shí);只用選中【Interop.ADOX】,然后在屬性下的【嵌入互操作類(lèi)型-->修改為否】即可解決,如下圖所示:
?
2.2、Access數(shù)據(jù)幫助類(lèi)
這里Access數(shù)據(jù)庫(kù)幫助類(lèi)的部分內(nèi)容,如下所示:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-481881.html
/***
* Title:"輕量數(shù)據(jù)庫(kù)" 項(xiàng)目
* 主題:Access數(shù)據(jù)庫(kù)的幫助類(lèi)
* Description:
* 功能:
* ①構(gòu)造函數(shù)時(shí)可以創(chuàng)建Access指定的連接字符串
* ②創(chuàng)建Access的mdb類(lèi)型數(shù)據(jù)庫(kù)
* Date:2022
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using LiteDBHelper.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Text;
namespace LiteDBHelper
{
public class AccessDBHelper
{
#region 基礎(chǔ)參數(shù)
//數(shù)據(jù)庫(kù)連接字符串
private string _ConnStr;
//獲取到數(shù)據(jù)庫(kù)連接字符串
public string ConnStr { get { return _ConnStr; } }
#endregion
#region 構(gòu)造函數(shù)
/// <summary>
/// mdb文件的連接字符串構(gòu)造函數(shù)
/// </summary>
/// <param name="connnection"></param>
public AccessDBHelper(string connnection)
{
if (string.IsNullOrEmpty(connnection)) return;
_ConnStr = connnection;
}
/// <summary>
/// mdb文件無(wú)用戶名和密碼構(gòu)造函數(shù)
/// </summary>
/// <param name="mdbFilePathAndName">mdb文件的路徑和名稱(chēng)(比如:@"D:\\HalmEL\\2022-4-11.mdb")</param>
public AccessDBHelper(string mdbFilePathAndName,AccessDBType accessDBType)
{
if (string.IsNullOrEmpty(mdbFilePathAndName)) return;
string strDriver = GetDirverOfAccessDBType(accessDBType);
_ConnStr = $"{strDriver};Data source={mdbFilePathAndName};";
InstanceSqlHelper(_ConnStr);
}
#endregion
#region 創(chuàng)建Access數(shù)據(jù)庫(kù)、表及其字段
/// <summary>
/// 創(chuàng)建Mdb數(shù)據(jù)庫(kù)
/// </summary>
/// <param name="mdbFilePathAndName">mdb文件的路徑和名稱(chēng)(比如:@"D:\\HalmEL\\2022-4-11.mdb")</param>
/// <returns>返回創(chuàng)建結(jié)果</returns>
public ResultInfo CreateMdbDataBase(string mdbFilePathAndName)
{
ResultInfo resultInfo = new ResultInfo();
if (File.Exists(mdbFilePathAndName))
{
resultInfo.ResultStatus = ResultStatus.Success;
resultInfo.Message = $"{mdbFilePathAndName} 文件已經(jīng)存在!";
}
try
{
//如果目錄不存在,則創(chuàng)建目錄
string folder = Path.GetDirectoryName(mdbFilePathAndName);
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
//創(chuàng)建Catalog目錄類(lèi)
ADOX.CatalogClass catalog = new ADOX.CatalogClass();
//根據(jù)聯(lián)結(jié)字符串使用Jet數(shù)據(jù)庫(kù)引擎創(chuàng)建數(shù)據(jù)庫(kù)
catalog.Create(_ConnStr);
catalog = null;
resultInfo.ResultStatus = ResultStatus.Success;
resultInfo.Message = $"{mdbFilePathAndName} 文件創(chuàng)建成功!";
}
catch (Exception ex)
{
resultInfo.ResultStatus = ResultStatus.Error;
resultInfo.Message = $"{ex.Message}";
}
return resultInfo;
}
/// <summary>
/// 創(chuàng)建mdb表(字段都是短文本類(lèi)型)
/// </summary>
/// <param name="mdbFilePathAndName">mdb文件的路徑和名稱(chēng)(比如:@"D:\\HalmEL\\2022-4-11.mdb")</param>
/// <param name="tableName">表名稱(chēng)</param>
/// <param name="fieldNameList">表字段名稱(chēng)列表</param>
/// <returns></returns>
public ResultInfo CreateMdbTable(string mdbFilePathAndName, string tableName, List<string> fieldNameList)
{
ResultInfo resultInfo = new ResultInfo();
if (string.IsNullOrEmpty(mdbFilePathAndName) || string.IsNullOrEmpty(tableName)
|| fieldNameList == null || fieldNameList.Count < 1)
{
resultInfo.SetContent(ResultStatus.Error, "內(nèi)容為空,請(qǐng)檢查!", null);
return resultInfo;
}
ADOX.CatalogClass catalog = new ADOX.CatalogClass();
ADODB.Connection connection = new ADODB.Connection();
try
{
//打開(kāi)數(shù)據(jù)庫(kù)連接
connection.Open(_ConnStr, null, null, -1);
catalog.ActiveConnection = connection;
//新建一個(gè)表
ADOX.TableClass table = new ADOX.TableClass();
table.ParentCatalog = catalog;
table.Name = tableName;
int fieldCount = fieldNameList.Count;
for (int i = 0; i < fieldCount; i++)
{
//增加一個(gè)文本字段
string fieldName = fieldNameList[i].ToString();
ADOX.ColumnClass column = new ADOX.ColumnClass();
column.ParentCatalog = catalog;
column.Name = fieldName;
column.Properties["Jet OLEDB:Allow Zero Length"].Value = false;
//table.Columns.Append(column, ADOX.DataTypeEnum.adLongVarChar, 100);
table.Columns.Append(fieldName, ADOX.DataTypeEnum.adVarWChar, 106);
}
//將創(chuàng)建的表加入數(shù)據(jù)庫(kù)
catalog.Tables.Append(table);
table = null;
catalog = null;
resultInfo.SetContent(ResultStatus.Success, $"創(chuàng)建:{tableName} 表成功", null);
}
catch (Exception ex)
{
resultInfo.SetContent(ResultStatus.Error, $"{ex.Message}", null);
}
finally
{
//關(guān)閉連接
connection.Close();
}
return resultInfo;
}
#endregion
#region 私有方法
/// <summary>
/// 根據(jù)Access類(lèi)型返回對(duì)應(yīng)的驅(qū)動(dòng)
/// </summary>
/// <param name="accessDBType">Access數(shù)據(jù)庫(kù)類(lèi)型</param>
/// <returns></returns>
private string GetDirverOfAccessDBType(AccessDBType accessDBType)
{
string connStr = $"Microsoft.ACE.OLEDB.12.0";
switch (accessDBType)
{
case AccessDBType.Is2007AndLater:
connStr = $"Provider=Microsoft.ACE.OLEDB.12.0";
break;
case AccessDBType.Is2003AndBefore:
connStr = $"Provider=Microsoft.Jet.OLEDB.4.0";
break;
default:
break;
}
return connStr;
}
#endregion
}//Class_end
/// <summary>
/// Access數(shù)據(jù)庫(kù)類(lèi)型
/// </summary>
public enum AccessDBType
{
//2007及其更高的版本
Is2007AndLater,
//2003等之前的版本
Is2003AndBefore,
}
}
2.3、關(guān)于未注冊(cè)Microsoft.ACE.OLEDB.12.0解決辦法
程序報(bào)錯(cuò)“未在本地計(jì)算機(jī)上注冊(cè)“Microsoft.ACE.OLEDB.12.0”提供程序”解決辦法https://blog.csdn.net/xiaochenXIHUA/article/details/124031921?spm=1001.2014.3001.5501文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-481881.html
三、其他的相關(guān)資料
DataTypeEnum - ActiveX Data Objects (ADO) | Microsoft Docshttps://docs.microsoft.com/zh-cn/sql/ado/reference/ado-api/datatypeenum?view=sql-server-ver15Access 桌面數(shù)據(jù)庫(kù)的數(shù)據(jù)類(lèi)型 (microsoft.com)https://support.microsoft.com/zh-cn/office/access-%E6%A1%8C%E9%9D%A2%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B-df2b83ba-cef6-436d-b679-3418f622e482#ID0EBBD=Office_2007_-_2010INSERT INTO 語(yǔ)句 (Microsoft Access SQL) | Microsoft Docshttps://docs.microsoft.com/zh-cn/office/client-developer/access/desktop-database-reference/insert-into-statement-microsoft-access-sql概念(Access VBA 參考) | Microsoft Docshttps://docs.microsoft.com/zh-cn/office/vba/access/concepts/miscellaneous/concepts-access-vba-reference\t \r \n轉(zhuǎn)義字符https://www.cnblogs.com/lsqbk/p/10259044.html
到了這里,關(guān)于C#實(shí)現(xiàn)對(duì)Access數(shù)據(jù)庫(kù)的通用操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!