当我按下按钮时,我想通过datagridview更新sql数据库。我该怎么做呢?任何帮助将不胜感激。
这是我到目前为止所做的:
private void btnUpdate_Click(object sender, EventArgs e)
{
SqlConnection Connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Logins.mdf;Integrated Security=True");
try
{
Connection.Open();
SqlCommand cmd = new SqlCommand(@"UPDATE LoginsTable SET Name=@name, Surname=@surName, Username=@uname, Password=@pass, AccessLevel=@accessLevel", Connection);
cmd.Parameters.Add("@name", SqlDbType.NVarChar, 50, "Name");
cmd.Parameters.Add("@surName", SqlDbType.NVarChar, 50, "Surname");
cmd.Parameters.Add("@uname", SqlDbType.NVarChar, 50, "Username");
cmd.Parameters.Add("@pass", SqlDbType.NVarChar, 50, "Password");
cmd.Parameters.Add("@accessLevel", SqlDbType.NVarChar, 50, "AccessLevel");
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Connection Unsucesful:" + ex.Message);
}
答案 0 :(得分:0)
检查出来
http://www.codeproject.com/Articles/14759/VB-DataGridView-Automatically-Saving-Updates-to-a
您只需将datagridview绑定到数据库中,完成后数据库中的更新将自动完成