此功能将显示“连接未正确初始化”的错误。为什么呢?
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();
}
}
}
答案 0 :(得分:3)
您需要设置SqlCeCommand
的连接SqlCeCommand command = new SqlCeCommand(
queryString, connection);
将连接字符串设置为
@"Data Source=|DataDirectory|\Database.sdf;Persist Security Info=False;"
答案 1 :(得分:-1)