データベースから呼び出しの最大IDを保存し、それをデータグリッドビューに保存するクエリを作成しますが、それは正しく機能しますが、データグリッドビューの行の値を更新したいが、その値を保持したいので、更新クエリを作成できませんmaxid ですが、正しい方法でクエリに maxid を書き込むことはできません。これの更新を書くのを手伝ってください
ここに私の保存クエリがあります:
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();
}