我只是简单的方法显示如下:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string sqlcmdString = string.Format("UPDATE Bills SET Name = '@name', Time = '@time', Product = '@pro', Price = @money WHERE Name = '@value';");
using (SqlConnection con = new SqlConnection(sqlcmdString))
using (SqlCommand cmd = new SqlCommand("dbo.Bills", con))
{
// tell ADO.NET it's a stored procedure (not inline SQL statements)
cmd.CommandType = CommandType.StoredProcedure;
// define parameters
cmd.Parameters.Add("@name", SqlDbType.NVarChar, 100).Value = tb_TenKH.Text;
cmd.Parameters.Add("@time", SqlDbType.DateTime).Value = cb_Thoigian.Text;
cmd.Parameters.Add("@pro", SqlDbType.NVarChar, 100).Value = tb_SanPham.Text;
cmd.Parameters.Add("@money", SqlDbType.Money).Value = tb_ThanhTien.Text;
cmd.Parameters.Add("@value", SqlDbType.NVarChar, 100).Value = cellvalue;
// open connection, execute stored procedure, close connection again
con.Open();
if (cmd.ExecuteNonQuery() > 0)
{
//dosomething
}
else
{
MessageBox.Show("Failed!!!");
}
con.Close();
}
}
无法运行此行。当我调试它时,它显示错误:
System.ArgumentException:'初始化字符串的格式有 不符合从索引0开始的规范。'
我知道我的sqlcmdString
语法可能出错了,但我无法确定。请给我一个帮助。
答案 0 :(得分:0)
我的语法错了:我应该使用如下:
string sqlcmdString = string.Format("UPDATE Bills SET Name = '@name', Time = '@time', Product = '@pro', Price = @money WHERE Name = '@value';");
using (SqlConnection con = new SqlConnection(connString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(sqlcmdString, con))
{
cmd.CommandType = CommandType.Text;
// define parameters
cmd.Parameters.Add("@name", SqlDbType.NVarChar, 100).Value = tb_TenKH.Text;
cmd.Parameters.Add("@time", SqlDbType.DateTime).Value = cb_Thoigian.Text;
cmd.Parameters.Add("@pro", SqlDbType.NVarChar, 100).Value = tb_SanPham.Text;
cmd.Parameters.Add("@money", SqlDbType.Money).Value = tb_ThanhTien.Text;
cmd.Parameters.Add("@value", SqlDbType.NVarChar, 100).Value = cellvalue;
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("Thành Công!!!");
if (passrow != null)
{
string[] result_back = { tb_TenKH.Text, cb_Thoigian.Text, tb_SanPham.Text, tb_ThanhTien.Text };
passrow(result_back);
{
this.Hide();
}
}
}
else
{
MessageBox.Show("Thất Bại!!!");
}
con.Close();
}
}