0
我想更新用户输入的文本框值datagrid/sql表的选定行。可以使用下面的代码进行更新,但输入到第一个文本框textBox1的值将输入到每一行/列中。 Imgur的datagridview的更新后的专辑:按钮来更新SQL datagridview数据不正确地更新行
private void button2_Click(object sender, EventArgs e)
{
SqlConnection connectiont = new SqlConnection("Data Source=DESKTOP-P3JSE1C;Initial Catalog=logins;Integrated Security=True");
{
connectiont.Open();
SqlCommand update = new SqlCommand("UPDATE dbo.logins SET site = @site, username = @username, pword = @pword, email = @email", connectiont);
update.Parameters.AddWithValue("@site", textBox1.Text);
update.Parameters.AddWithValue("@username", textBox1.Text);
update.Parameters.AddWithValue("@pword", textBox1.Text);
update.Parameters.AddWithValue("@email", textBox1.Text);
update.ExecuteNonQuery();
}
clear();//method to clear textboxes on the form w/ datagridview
}
您没有** WHERE **子句。你打算更新整个桌子吗? - 我希望不是。 – GurV
此外,我希望这只是一个例子,当你使用'textBox1.Text'的所有因素,除非你添加一个ID列或任何,然后即使使用WHERE子句,你仍然会更新与'textBox1.Text' –
[从使用SQL Server的SELECT更新]的可能重复(http://stackoverflow.com/questions/2334712/update-from-select-using-sql-server) – Usman