Sql复杂更新查询错误

时间:2013-10-24 10:33:01

标签: c# sql

我制作的aquery从数据库保存调用max id并将其保存在adata网格视图中并且正常工作但我无法对其进行更新查询,因为我想更新datagrid视图行中的值但是想要保持其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();

}

0 个答案:

没有答案
相关问题