SQL Server 更新数据库时必须声明标量变量

kr98yfug  于 2023-01-04  发布在  其他
关注(0)|答案(1)|浏览(272)

我的代码:

SqlConnection con = new SqlConnection(@"DATA;");
            SqlDataAdapter cmd = new SqlDataAdapter();
            cmd.InsertCommand = new SqlCommand(" UPDATE TIME set TimeOut = @TimeOut Where TimeOut = @textBox1.Text", con);
            cmd.InsertCommand.Parameters.Add("@timeOut", DateTime.Now);
            con.Open();
            cmd.InsertCommand.ExecuteNonQuery();
            con.Close();

错误:必须声明标量变量"@textBox1"

I tried declaring a variable with textBox1.Text but it didn't work
vltsax25

vltsax251#

SqlConnection con = new SqlConnection(@"DATA;");
SqlDataAdapter cmd = new SqlDataAdapter();
cmd.InsertCommand = new SqlCommand(@"UPDATE `TIME` 
                                     SET TimeOut = @NewTimeOut 
                                     WHERE TimeOut = @OldTimeOut", con);
cmd.InsertCommand.Parameters.Add("@NewTimeOut", DateTime.Now);
cmd.InsertCommand.Parameters.Add("@OldTimeOut", DateTime.Parse(textBox1.Text));
con.Open();
cmd.InsertCommand.ExecuteNonQuery();
con.Close();

相关问题