[英]C# MS Access Database Connection System.Data.SqlClient.SqlException' occurred in System.Data.dll
I am not sure if this problem has anything to do with my code or simply the way that my database is set up. 我不确定这个问题是否与我的代码或数据库的设置方式有关。 Any pointers would be awesome!
任何指针都很棒!
This is the error message that I get: 这是我收到的错误消息:
I have gone to "Modify Connection" and used the 'Test Connection' tool and that says that it connects fine but when the actual program runs nothing happens and I get the error. 我已经进入“修改连接”并使用了“测试连接”工具,它表示连接正常,但是当实际程序运行时,什么也没有发生,并且我得到了错误。
Here is my code: 这是我的代码:
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();
}
}
You should try create the SqlCommand with: 您应该尝试使用以下命令创建SqlCommand:
SqlCommand xp = CustomerInfo.CreateCommand();
See this example: 请参阅以下示例:
https://msdn.microsoft.com/pt-br/library/system.data.sqlclient.sqlconnection.createcommand(v=vs.110).aspx https://msdn.microsoft.com/pt-br/library/system.data.sqlclient.sqlconnection.createcommand(v=vs.110).aspx
Update: 更新:
Try use OleDbConnection
. 尝试使用
OleDbConnection
。 See: 看到:
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;
}
See more details in: https://msdn.microsoft.com/en-us/library/System.Data.OleDb.OleDbParameterCollection(v=vs.110).aspx 请参阅以下详细信息: https : //msdn.microsoft.com/zh-cn/library/System.Data.OleDb.OleDbParameterCollection(v=vs.110).aspx
It sounds like your trying to use a local database or auto-named database: Here is the syntax for your connection string: 听起来像是您尝试使用本地数据库或自动命名的数据库:这是您的连接字符串的语法:
Data Source=(LocalDB)\v11.0;
AttachDbFilename=Your local location here;
Integrated Security=True
Hope that helps. 希望能有所帮助。
The problem is you're using System.Data.Sql
as the provider, when it should all be coming from System.Data.OleDb
; 问题是您正在使用
System.Data.Sql
作为提供程序,而所有这些都应来自System.Data.OleDb
;
However, you can use <asp:SqlDataSource>
in ASPX, to connect to an Access Database, but that is not the same thing as System.Data.Sql
. 但是,你可以使用
<asp:SqlDataSource>
在ASPX,连接到一个Access数据库,但不是一回事 System.Data.Sql
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.