在C#中使用Access数据库吗? [英] Using Access databases in C#?

查看:133
本文介绍了在C#中使用Access数据库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何使用Microsoft Access( .ACCDB )的数据库在C#(控制台应用程序,而不是web asp.net)?从我读过,我需要使用 ADO.NET ,但我真的很茫然,如何做到这一点在C#控制台应用程序。在与的MySQL PHP,我会找 mysqli_construct 任何人都可以点我朝着一个教程或文件,这将有助于我吗?我试图用它的方式来存储和访问数据,我的非Web,非ASP.NET 应用程序,如果改变任何东西。

How would I use a Microsoft Access (.accdb) database in C# (console application, not web asp.net)? From what I've read, I'll need to use ADO.NET, but I'm really at a loss as to how to do this in a C# console application. In PHP with MySQL, I'd be looking for mysqli_construct Could anyone point me towards a tutorial or documentation that would help me with that? I'm trying to use it as a way to store and access data for my non-web, non ASP.NET application, if that changes anything.

谢谢!

推荐答案

请参阅此演练使用ADO.NET编辑Access数据库

现在而例子是使用一个Web应用程序,不用担心,使用的类仍然适用于控制台应用程序也是如此。

Now while that example is using a web application, don't worry, the classes being used are still applicable for a console application as well.

主要课程,你将需要使用的:

The main classes you will need to use are:

  • OleDbConnection
  • OleDbCommand
  • OleDbDataReader

类的最重要的文件有code样本的控制台应用程序的例子。

The documentation of the classes above all have code samples that are console app examples.

下面是一个code片段在一个控制台应用程序使用(记住添加:使用System.Data.OleDb;

Below is a code snippet for use in a console app (remember to add the: using System.Data.OleDb;

string connectionString = 
    @"Provider=Microsoft.Jet.OLEDB.4.0;" +
    @"Data Source=C:\path\to\your\database.mdb;" +
    @"User Id=;Password=;";

string queryString = "SELECT Foo FROM Bar";

using (OleDbConnection connection = new OleDbConnection(connectionString))
using (OleDbCommand command = new OleDbCommand(queryString, connection))
{
    try
    {
        connection.Open();
        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine(reader[0].ToString());
        }
        reader.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

另外,刚注意到你明确表示了.ACCDB数据库。对于这种抢 Microsoft Access数据库引擎2010可再发行。在连接字符串中的供应商,然后将需要更改为: Microsoft.ACE.OLEDB.12.0 (见注,在该链接获取更多信息)

Also, just noticed you explicitly said an .accdb database. For this grab the Microsoft Access Database Engine 2010 Redistributable. The provider in the connection string would then need to be changed to: Microsoft.ACE.OLEDB.12.0 (see notes at that link for more info).

这篇关于在C#中使用Access数据库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆