c# - 我们需要同时使用 ADODB 和 OLEDB 吗?

标签 c# database

我是 C# 新手,正在开发一个使用小型 Access 数据库的应用程序。我试图了解该使用什么,ADODB 还是 OLEDB,但似乎我无法解决这个问题。

那么,是否可以只使用这些库之一?还是两者都正常使用?

例如,数据库的常见内容:

string MyQuery = "SELECT * FROM MyTable";
Recordset rs = new Recordset();
rs.Open(MyQuery, MyConnection, CursorTypeEnum.adOpenKeyset, LockTypeEnum.adLockOptimistic);

使用所有 ADODB 对象,直到:

OleDbDataAdapter adapter = new OleDbDataAdapter();
DataTable dt = new DataTable("MyTable");
adapter.Fill(dt, rs);

所以我将数据表提供给 DataGridView。适配器是OLEDB,而不是ADODB。有没有办法避免OLEDB?所以,我想我摆脱了 ADODB 并在 OLEDB 中完成所有操作,一切都很顺利,直到我想在运行时添加表:

ADOX.Catalog MyCat = new ADOX.Catalog();
MyCat.ActiveConnection = MyConnection;
ADOX.Table table = new ADOX.Table();
table.Name = "MyTable";
table.Columns.Append("ID", ADOX.DataTypeEnum.adInteger);
table.Columns["ID"].ParentCatalog = MyCat;
table.Columns["ID"].Properties["AutoIncrement"].Value = true;
table.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "ID");
table.Columns.Append("DateAdded", ADOX.DataTypeEnum.adDate);
table.Columns.Append("Name");
table.Columns.Append("Surname");
cat.Tables.Append(table);

目录绝对拒绝 OLEDB 连接并需要 ADODB 连接,因此我需要拯救 ADODB。

那么,最后,我是否需要辞职才能使用两者,或者我在发布的代码示例中做错了什么?欢迎替代方案,提前致谢。

对于新手来说,如果知道该使用哪个系统并坚持使用它,知道它可以完成需要完成的所有工作,那就太好了。

最佳答案

取自 this answer by an MCC, MVP :

OLEDB is a Microsoft standard that defines a set of API (Application Interface) functions for accessing a database. It is a COM (Component Object Model) API that was a follow-up to the ODBC API. Typically OLEDB is used to create a database specific driver, known as a provider, that can be implemented by a higher level data access library such as ADO or ADO.NET.

ADO (ADODB) is a generic (COM) database library, that can be used by programming languages such as Visual Basic and C++ to access any type of database for which an OLEDB Provider has been developed. In this context, ADO is an OLEDB Consumer. It communicates with the OLEDB Provider, which in turn communicates with the database directly or a database server.

For example, to open an Access database, the Connection object of ADODB would specify the Jet OLEDB Provider in its connection string to open and subsequently communicate with the database:

ADODB.Connection cnn;

cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                            "Data Source=E:\My Documents\db1.mdb;" +
                            "Jet OLEDB:Engine Type=5";
cnn.Open();

对我来说,这意味着除非您想要使用特定的 ADO 提供程序,否则直接使用 OLEDB 会更好/更快。

然而,OLE 可能需要更多的开发时间,尽管 ADO 的开发速度更快,但性能比 OLE 慢。

一些主要问题: 你的连接字符串是什么? (您的问题意味着您在连接字符串中使用了 ADODB 提供程序)。 ADOX是什么?

附注一些引用资料供您引用:

Microsoft Access Performance Tips to Speed up Your Access Databases

Access connection strings

编辑:取自here : “从 2007 版开始,Access 包含 Office 特定版本的 Jet,最初称为 Office Access 连接引擎 (ACE),但现在称为 Access 数据库引擎。该引擎与以前版本的 Jet 完全向后兼容引擎,因此它可以读取和写入早期 Access 版本的 (.mdb) 文件。它引入了新的默认文件格式 (.accdb),这为 Access 带来了多项改进,包括复杂的数据类型,例如多值字段、附件数据类型以及备忘录字段中的历史记录跟踪。它还带来了安全性和加密方面的改进,并支持与 Microsoft Windows SharePoint Services 3.0 和 Microsoft Office Outlook 2007 的集成”

此外,您可能会遇到 Jet 的 64 位问题...

“该驱动程序不是 Windows 操作系统的一部分,但可作为可再发行版本提供。[11] 以前,Jet 数据库引擎仅为 32 位,并且不能在 64 位版本的 Windows 下 native 运行。”

有关如何通过 C# 使用 MS Access,请阅读 this good tutorial 如果您向下滚动到标题为“Access (accdb)”的部分,您将看到代码使用 ADOX.catalog。

编辑2: 有关 ADOX.Catalog 的信息 read this “通过将 ActiveConnection 属性设置为 ADO 连接对象或有效的连接字符串来打开目录。”

所以尝试这样的事情... Taken from the post underneath the accepted answer by Gord Thompson

ADOX.Catalog cat;
ADOX.Table tbl;

cat.ActiveConnection = _
    "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=C:\Users\Public\Database1.accdb;"

关于c# - 我们需要同时使用 ADODB 和 OLEDB 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32736756/

相关文章:

c# - 传递带有已指定参数的方法调用

c# - 如何调试 mvc4 razor View ?

c# - Button 的 Visible 属性在 Page_Init 和 Page_Load 之间变化

sql - 从配置表中检索值以用于查询

html - 对于动态内容生成器来说,将 HTML 保存到 MySQL 数据库是一个好方法吗?

sql-server - 如何对 SQL Server 查询进行基准测试?

c# - 使用 List<T> 测试 List<T> 成员资格

c# - 是否可以访问本地目录中的文件? (通用Windows平台)

php - 如何在php中从数据库中的数组中添加多行

mysql - 使用mysql的数据集群