1个按钮插入和更新2个表

时间:2016-11-17 17:07:05

标签: c# asp.net

我有一个带有按钮的asp.net页面,我想将数据提交到sql数据库。我正在尝试在一个表上运行insert命令,在另一个表中运行update命令。我一直收到错误:

Incorrect syntax near ','. 
Line 242:                    cmd.Parameters.AddWithValue("@CLc", CLc);
Line 243:                    cmd.Parameters.AddWithValue("@RMKc", RMKc);
Line 244:                    cmd2.ExecuteNonQuery();
Line 245:                }
Line 246:

我似乎无法理解为什么会出现问题。也许有更好的方法来做到这一点?

string conn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        using (SqlConnection sqlCon = new SqlConnection(conn))
        {
            sqlCon.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "INSERT INTO tblHistory4 ([TankNum],[HFt],[HIn]) SELECT @TankNum,@HFt,@HIn";
                cmd.Parameters.AddWithValue("@TankNum", TankNum);
                cmd.Parameters.AddWithValue("@HFt", HFt);
                cmd.Parameters.AddWithValue("@HIn", HIn);
                cmd.Connection = sqlCon;
                cmd.ExecuteNonQuery();
            }
            using (SqlCommand cmd2 = new SqlCommand())
            {
                cmd2.CommandText = "UPDATE GPCurrentGauge4 SET HFt=@HFtc,HIn=@HInc,HFx=@HFxc WHERE TankNumId=@TankNumId";
                cmd2.Parameters.AddWithValue("@TankNumId", TankNumId);
                cmd2.Parameters.AddWithValue("@HFtc", HFtc);
                cmd2.Parameters.AddWithValue("@HInc", HInc);
                cmd2.Parameters.AddWithValue("@HFxc", HFxc);
                cmd2.Connection = sqlCon;
                cmd2.ExecuteNonQuery();
            }
        }
    }
}

}

0 个答案:

没有答案