C#MS Access数据库连接系统.Data.SqlClient.SqlException';发生在System.Data.dll中

C#MS Access数据库连接系统.Data.SqlClient.SqlException';发生在System.Data.dll中,c#,ms-access,ms-access-2010,sqlconnection,C#,Ms Access,Ms Access 2010,Sqlconnection,我不确定这个问题是否与我的代码有关,或者仅仅与数据库的设置方式有关。任何指针都会很棒 这是我收到的错误消息: 我去了“修改连接”并使用了“测试连接”工具,它说它连接良好,但当实际程序运行时,什么也没有发生,我得到了错误 这是我的密码: private void btnAddCustomer_Click(object sender, EventArgs e) { SqlConnection CustomerInfo = new SqlConnection("Data So

我不确定这个问题是否与我的代码有关,或者仅仅与数据库的设置方式有关。任何指针都会很棒

这是我收到的错误消息:

我去了“修改连接”并使用了“测试连接”工具,它说它连接良好,但当实际程序运行时,什么也没有发生,我得到了错误

这是我的密码:

    private void btnAddCustomer_Click(object sender, EventArgs e)
    {
    SqlConnection CustomerInfo = new SqlConnection("Data Source=C:\\Users\\Cory\\Desktop\\DeluxWrapsWindows\\DeluxWrapsWindows\\DeluxWraps_DB.mdb");
    { 
    SqlCommand xp = new SqlCommand("Insert into CustomerInfo(LastName, FirstName, Email, PhoneNumber, Address, Instagram, CarMake, CarModel, AdditionalNotes) Values(@LastName, @Firstname, @Email, @PhoneNumber, @Address, @Instagram, @CarMake, @CarModel, @AdditionalNotes)", CustomerInfo);

     xp.Parameters.AddWithValue("@LastName", txtLastName.Text);
     xp.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
     xp.Parameters.AddWithValue("@Email", txtEmail.Text);
     xp.Parameters.AddWithValue("@PhoneNumber", txtPhoneNumber.Text);
     xp.Parameters.AddWithValue("@Address", txtAddress.Text);
     xp.Parameters.AddWithValue("@Instagram", txtInstagram.Text);
     xp.Parameters.AddWithValue("@Carmake", txtCarMake.Text);
     xp.Parameters.AddWithValue("@CarModel", txtCarModel.Text);
     xp.Parameters.AddWithValue("@AdditionalNotes", txtAdditionalNotes.Text);

     CustomerInfo.Open();
     xp.ExecuteNonQuery();
     CustomerInfo.Close();

    }
}

您应该尝试使用以下命令创建SqlCommand:

SqlCommand xp = CustomerInfo.CreateCommand();
请参见此示例:

更新:

尝试使用OLEDB连接。见:

public DataSet GetDataSetFromAdapter(
    DataSet dataSet, string connectionString, string queryString)
{
    using (OleDbConnection connection =
               new OleDbConnection(connectionString))
    {
        OleDbDataAdapter adapter =
            new OleDbDataAdapter(queryString, connection);

        // Set the parameters.
        adapter.SelectCommand.Parameters.Add(
            "@CategoryName", OleDbType.VarChar, 80).Value = "toasters";
        adapter.SelectCommand.Parameters.Add(
            "@SerialNum", OleDbType.Integer).Value = 239;

        // Open the connection and fill the DataSet. 
        try
        {
            connection.Open();
            adapter.Fill(dataSet);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the 
        // code exits the using block.
    }
    return dataSet;
}

有关更多详细信息,请参见:

听起来您好像在尝试使用本地数据库或自动命名数据库:以下是连接字符串的语法:

Data Source=(LocalDB)\v11.0;
AttachDbFilename=Your local location here;
Integrated Security=True

希望这能有所帮助。

问题是您正在使用
System.Data.Sql
作为提供程序,而它应该全部来自
System.Data.OleDb


但是,您可以在ASPX中使用
连接到Access数据库,但这与
System.Data.Sql

只有在尝试连接到Sql server数据库时才应使用Sqlconnection。请参阅此链接以查看有关此类连接的更多信息。要连接到MS Access数据库,请查看这篇SO文章。此外,您还需要指定sql server的本地db verson。我的版本是v11.0。我使用的是MS Access 2010。这是我第一次尝试连接到这样的数据库,因此感谢您的帮助。数据库在我的本地计算机上。在这种情况下,您的连接字符串需要使用其他驱动程序,因为您将使用Microsoft.Jet.OLEDB.4.0。下面是一个连接字符串示例:connectionString=“Provider=Microsoft.Jet.OLEDB.4.0;数据源=| DataDirectory | Northwind.mdb;”。只需确保您的.mdb文件位于数据目录中,并用您自己的.mdb文件名替换该名称。谢谢!我会尽快尝试。谢谢雷纳托·马查多的建议!不过,我还是犯了同样的错误。由于某种原因,它将无法连接到数据库。