[英]How can i update data in c#
private void button1_Click(object sender, EventArgs e) { String source = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Szabolcs\Documents\Adatbázis Kezelés2.mdf;Integrated Security=True;Connect Timeout=30 "; SqlConnection con = new SqlConnection(source); con.Open();
String sqlSelectQuery = "SELECT * FROM [Table] WHERE ID = "+ int.Parse(textBox1.Text);
SqlCommand cmd = new SqlCommand(sqlSelectQuery, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
textBox2.Text = (dr["Name"].ToString());
textBox3.Text = (dr["Kor"].ToString());
label4.Text = (dr["Kor"].ToString());
label5.Text = (dr["Kor"].ToString());
int s = 11;
string y = (dr["Kor"].ToString());
label4.Text = (dr["Kor"].ToString());
x = Int32.Parse(label4.Text);
x = x + 0;
label6.Text = (x.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
String source = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Szabolcs\Documents\Adatbázis Kezelés2.mdf;Integrated Security=True;Connect Timeout=30";
SqlConnection con = new SqlConnection(source);
con.Open();
x = x + 1;
label6.Text = (x.ToString());
String st = "UPDATE supplier SET Kor = " + label6.Text + " WHERE Id = " + textBox1.Text;
}
添加這些行
SqlCommand cmd = new SqlCommand(st, con);
int result = cmd.ExecuteNonQuery();
請放置一個斷點並檢查st
的值,它是否生成了有效的查詢。
我建議使用存儲過程並從代碼中傳遞參數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.