C# - 无法使用新的 SqlConnection() 连接到本地 ACCDB 文件

C# - unable to connect to a local ACCDB file with new SqlConnection()

做一个大学项目需要一个应用程序来存储和输出本地访问数据库文件中的数据。

到目前为止,我的 C# 知识很少,但我对如何编写代码以及函数和方法有基本的了解。

我在 Whosebug 和其他网站上查找了很多关于此类问题的不同问题,但我找不到解决问题的方法。

这是代码 -

private void search_db(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection())
    {
        string search_query = search_input.Text;
        conn.ConnectionString = "Data Source=D:/Projects/WIP/8515/Academy/Travel Agency C#/Hotel_Agency/Hotel_Database.accdb;";

        conn.Open();

        SqlCommand command = new SqlCommand("SELECT * FROM Customers WHERE (name LIKE @query) OR (EGN LIKE @query)", conn);
        command.Parameters.Add(new SqlParameter("query", search_query));

        // Create new SqlDataReader object and read data from the command.
        using (SqlDataReader reader = command.ExecuteReader())
        {
            // while there is another record present
            while (reader.Read())
            {
                // write the data on to the screen
                Console.WriteLine(String.Format("{0} \t | {1} \t | {2} \t | {3}",
                    // call the objects from their index
                reader[0], reader[1], reader[2], reader[3]));
            }
        }
    }
}

在给定代码的第 6 行,如果我的 ConnectionString 提供了我已经三次检查和确认的数据源是正确的,我唯一不确定的是我应该使用哪种类型的斜线,因为来自 windows 目录的默认 \ 斜杠被 Visual Studio 误认为是转义符,而使用 / 斜杠就可以了。

问题是与数据库的连接总是失败并出现此错误

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL

这是我们用于此应用程序的名称空间的完整列表

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

我曾尝试将提供程序关键字添加到连接字符串,但这会导致另一个关于关键字不受支持的错误,因为据称 System.Data.SqlClient 命名空间会自动设置提供程序。

对于可能导致此问题的任何帮助,我们将不胜感激!

PS: 对于我所提供的任何进一步的知识不足,我深表歉意,我确实是 C# 编程的新手,但我觉得它很有趣和令人兴奋,并且想了解更多。

你在这里犯了一个常见的错误。在命名空间 System.Data.SqlClient(SqlConnection、SqlCommand 等)中定义的 类 只能与 Sql 服务器数据库系统(Full、Express 或 LocalDb)。它们不能使用 Access 数据库。

对于此数据库,您应该使用 System.Data.OleDb 命名空间(OleDbConnection、OleDbCommand 等)中的 类 这些 类 了解connectionstring 以访问 Access 数据库并可以打开并使用它。

所以你的代码应该是:

....
using System.Data.OleDb;

private void search_db(object sender, EventArgs e)
{
    using (OleDbConnection conn = new OleDbConnection())
    {
        conn.ConnectionString = ......
        conn.Open();
        string cmdText = @"SELECT * 
                           FROM Customers 
                           WHERE ([name] LIKE @q1) OR (EGN LIKE @q2)";
        using(OleDbCommand command = new OleDbCommand(cmdText, conn))
        {
            command.Parameters.Add("@q1", OleDbType.VarWChar).Value = search_query;
            command.Parameters.Add("@q2", OleDbType.VarWChar).Value = search_query;
            using (OleDbDataReader reader = command.ExecuteReader())
            {
               ....
            }
        }
    }
}

使用 OleDb 时要记住的一件重要事情是您有 位置 参数。参数不是通过它们的名称来识别的,而是通过它们在查询文本中的位置来识别的。所以如果你有两个参数placeholder,即使他们是为了同一个值,你也需要把两个参数放在集合中。一个用于第一个占位符,一个用于第二个占位符。当您有不同值的占位符时,这一点就更为重要。您需要按照查询文本中预期的确切顺序添加这些值。