C# Sql复杂更新查询错误
我做了一个查询,从数据库保存调用最大id,并将其保存在adata网格视图中,该查询工作正常,但我无法对其进行更新查询,因为我想更新datagrid视图行中的值,但想保留其maxid,但无法在查询中正确写入maxid。请帮助我为此编写更新 以下是我的保存查询:C# Sql复杂更新查询错误,c#,sql,C#,Sql,我做了一个查询,从数据库保存调用最大id,并将其保存在adata网格视图中,该查询工作正常,但我无法对其进行更新查询,因为我想更新datagrid视图行中的值,但想保留其maxid,但无法在查询中正确写入maxid。请帮助我为此编写更新 以下是我的保存查询: string sqlTemplate = "INSERT INTO molaak_det(MAXID,MALKID,AKARTYPE,AKARADDRESS) VALUES ({0}, '{1}', '{2}', '{3}')"
string sqlTemplate = "INSERT INTO molaak_det(MAXID,MALKID,AKARTYPE,AKARADDRESS) VALUES ({0}, '{1}', '{2}', '{3}')";
string sqlSubquery = "(SELECT COALESCE(max(MAXID)+1, 1) FROM molaak_det)";
System.Diagnostics.Debug.AutoFlush = true;
SqlConnection CN = new SqlConnection(mysql.CON.ConnectionString);
CN.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string Query = String.Format(sqlTemplate,
sqlSubquery,
this.txtID.Text,
this.dataGridView1.Rows[i].Cells[0].Value,
this.dataGridView1.Rows[i].Cells[1].Value);
System.Diagnostics.Debug.WriteLine(Query);
SqlCommand cmd = new SqlCommand(Query, CN);
cmd.ExecuteNonQuery();
}
CN.Close();
下面是我的“尝试进行更新”查询此更新具有相同值的所有行只需更新已编辑的行:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
mysql.sa.UpdateCommand.CommandText = string.Format(" UPDATE molaak_det SET AKARTYPE='{1}',AKARADDRESS='{2}' where MALKID='{0}'", txtID.Text, this.dataGridView1.Rows[i].Cells[0].Value, this.dataGridView1.Rows[i].Cells[1].Value);
mysql.sa.UpdateCommand.ExecuteNonQuery();
}您的错误消息是什么?我放置了更新查询,它错误,因为它使用相同的值更新了所有行。我只需要更新我在数据网格中更改其值的行