我想从我的datagridview同步我的数据库。当我修改,添加或删除我的datagridview中的列时,我想仅更新已更改的行。
这是我的代码,但是这段代码每次运行时都会插入新行,因此它有重复的值。
string conStr = "Server=servername,1433;Database=dbname;User ID=userid;Password=password;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
public void UpdateDatabase(DataTable dtOldList, DataTable dtNewList)
{
string selectStatement = "SELECT * FROM Customers";
System.Data.DataTable dt = new System.Data.DataTable();
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter();
sqlDa.SelectCommand = new SqlCommand(selectStatement, conn);
SqlCommandBuilder cb = new SqlCommandBuilder(sqlDa);
dtOldList.Merge(dtNewList);
DataTable d3 = dtNewList.GetChanges();
sqlDa.Update(d3);
}