执行非查询:连接尚未正确初始化
此函数将显示错误“连接尚未正确初始化”。为什么?
public void updateCustomer()
{
using (SqlCeConnection aConnection = new SqlCeConnection(@"Data Source=|DataDirectory|\Database.sdf"))
{
aConnection.Open();
using (SqlCeCommand aCommand = new SqlCeCommand("UPDATE customer SET credit = @credit WHERE(ID = @ID)"))
{
aCommand.Parameters.Add(new SqlCeParameter("credit", SqlDbType.NVarChar, 100)).Value = getRemaining();
aCommand.Parameters.Add(new SqlCeParameter("ID", SqlDbType.Int, 8)).Value = Convert.ToInt32(textBox2.Text);
int resultUpdate = aCommand.ExecuteNonQuery();
if (resultUpdate != -1)
label9.Text = "customer updated sussessfully";
else
label9.Text = "some error in updating customer";
aConnection.Close();
}
}
}
This function will display an error that "connection has not been properly initialized." Why?
public void updateCustomer()
{
using (SqlCeConnection aConnection = new SqlCeConnection(@"Data Source=|DataDirectory|\Database.sdf"))
{
aConnection.Open();
using (SqlCeCommand aCommand = new SqlCeCommand("UPDATE customer SET credit = @credit WHERE(ID = @ID)"))
{
aCommand.Parameters.Add(new SqlCeParameter("credit", SqlDbType.NVarChar, 100)).Value = getRemaining();
aCommand.Parameters.Add(new SqlCeParameter("ID", SqlDbType.Int, 8)).Value = Convert.ToInt32(textBox2.Text);
int resultUpdate = aCommand.ExecuteNonQuery();
if (resultUpdate != -1)
label9.Text = "customer updated sussessfully";
else
label9.Text = "some error in updating customer";
aConnection.Close();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要设置 SqlCeCommand
设置连接字符串的连接,
检查 SQL Server 的连接字符串精简版了解更多信息
you need to set the connection for the SqlCeCommand
set connection string as
check Connection strings for SQL Server Compact Edition for more information
我忘记了 unhandledException 所说的
很多次。记住每次当你使用 sqlcommand 时它都需要 con 作为一个参数...
否则使用名为 Sqldataadapter 的更高级实用程序...
I Forgot this thing stated by unhandledexception
many of the times .rememeber every time that whenever you are using sqlcommand it wants con as an argument...
otherwise use a more advanced utility named Sqldataadapter...