我正在尝试更新SQL数据库的特定信息。我在网上搜索了有用的答案,但还没有找到我可以使用的任何东西。代码一直工作到我点击保存按钮的时候。这是我的代码。非常感谢所有的帮助!
string NyString;
string itemString;
private void cmdHämtaLista_Click(object sender, EventArgs e)
{
ListBoxOne.Items.Clear();
SqlConnection cm = new SqlConnection(@"Data Source = (localdb)\MSSQLLocalDB;
Initial Catalog = NORTHWND; Integrated Security = True; Connect Timeout = 30;
Encrypt = False; TrustServerCertificate = True; ApplicationIntent = ReadWrite;
MultiSubnetFailover = False");
cm.Open();
SqlCommand cmd = cm.CreateCommand();
cmd.CommandText = "SELECT TOP(1000)" +
"[CategoryName]" +
"FROM[NORTHWND].[dbo].[Categories]";
//"order by[CategoryName]Desc"; tog bort sortering för kategorierna läggs in
//i listan efter bokstavsordning ist för id som de är sorterade på i sql
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
ListBoxOne.Items.Add(rd.GetString(0));
}
cm.Close();
rd.Close();
}
private void ListBoxOne_SelectedIndexChanged(object sender, EventArgs e)
{
ListBoxTwo.Items.Clear();
var sitem = (ListBoxOne.SelectedIndex) + 1;
SqlConnection cm = new SqlConnection(@"Data Source = (localdb)\MSSQLLocalDB;
Initial Catalog = NORTHWND; Integrated Security = True; Connect Timeout = 30;
Encrypt = False; TrustServerCertificate = True; ApplicationIntent = ReadWrite;
MultiSubnetFailover = False");
cm.Open();
SqlCommand cmd = cm.CreateCommand();
cmd.CommandText = "SELECT TOP(1000)" +
"[ProductName]" +
"FROM[NORTHWND].[dbo].[Products]" +
"WHERE [CategoryID]=" + sitem.ToString();
//"ORDER BY[ProductName]Desc"; tog bort sortering för kategorierna läggs in i listan
//efter bokstavsordning ist för id som de är sorterade på i sql
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
ListBoxTwo.Items.Add(rd.GetString(0));
}
cm.Close();
rd.Close();
MessageBox.Show("Closed.");
}
private void ListBoxTwo_SelectedIndexChanged(object sender, EventArgs e)
{
itemString = ListBoxTwo.SelectedItem.ToString();
SqlConnection cm = new SqlConnection(@"Data Source = (localdb)\MSSQLLocalDB; Initial Catalog = NORTHWND; Integrated Security = True; Connect Timeout = 30; Encrypt = False; TrustServerCertificate = True; ApplicationIntent = ReadWrite; MultiSubnetFailover = False");
cm.Open();
SqlCommand cmd = cm.CreateCommand();
cmd.CommandText = "SELECT TOP(1000) [UnitPrice]" +
"FROM[NORTHWND].[dbo].[Products]" +
"WHERE [ProductName]=@ProductName";
cmd.Parameters.AddWithValue("@ProductName", itemString.ToString());
//"ORDER BY[ProductName]Desc"; tog bort sortering för kategorierna läggs in i listan efter bokstavsordning ist för id som de är sorterade på i sql
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
txtPris.Text = Math.Round(rd.GetDecimal(0), 2).ToString();
}
cm.Close();
rd.Close();
txtNamn.Text = ListBoxTwo.SelectedItem.ToString();
txtNamn.Text = itemString;
}
private void cmdSave_Click(object sender, EventArgs e)
{
NyString = txtNamn.Text;
itemString = ListBoxTwo.SelectedItem.ToString();
SqlConnection cm = new SqlConnection(@"Data Source = (localdb)\MSSQLLocalDB;
Initial Catalog = NORTHWND; Integrated Security = True; Connect Timeout = 30;
Encrypt = False; TrustServerCertificate = True; ApplicationIntent = ReadWrite;
MultiSubnetFailover = False");
cm.Open();
SqlCommand cmd = cm.CreateCommand();
cmd.CommandText = "Update Products" +
"Set [ProductName] =@NewName" +
cmd.Parameters.AddWithValue("@NewName", NyString.ToString()) +
//"FROM[NORTHWND].[dbo].[Products]" +
"WHERE [ProductName]=@ProductName" +
cmd.Parameters.AddWithValue("@ProductName", itemString.ToString());
cmd.ExecuteNonQuery();
cm.Close();
}
}
答案 0 :(得分:2)
你在这里做了什么:将参数连接到命令文本?错误的做法
cmd.CommandText = "Update Products" +
"Set [ProductName] =@NewName" +
cmd.Parameters.AddWithValue("@NewName", NyString.ToString()) +
//"FROM[NORTHWND].[dbo].[Products]" +
"WHERE [ProductName]=@ProductName" +
cmd.Parameters.AddWithValue("@ProductName", itemString.ToString());
尝试将以上代码替换为以下代码
cmd.CommandText = "Update Products Set [ProductName] =@NewName WHERE [ProductName]=@ProductName";
cmd.Parameters.AddWithValue("@NewName", NyString.ToString())
cmd.Parameters.AddWithValue("@ProductName", itemString.ToString());