如何解决必须在SQL insert命令上声明标量变量? [英] How to solve must declare the scalar variable on SQL insert command?

查看:88
本文介绍了如何解决必须在SQL insert命令上声明标量变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected   void  Add_Click(  object  sender,EventArgs e)
{


string constr = ConfigurationManager.ConnectionStrings [ ApplicationServices]。ConnectionString;
SqlConnection con = new SqlConnection(constr);
// SqlCommand cmd = new SqlCommand(插入学生值('+ RegNo.Text + ','+ Name.Text +','+ Address.Text +'));
SqlCommand cmd = new SqlCommand( 插入学生(@ RegNo,@ Name,@ Address,@ CreatedTime)值(@ RegNo,@名称,@地址,GETDATE()));

cmd.Connection = con;
con.Open();

cmd.ExecuteNonQuery();
con.Close();

}





我的尝试:



这里尝试插入记录。我有3个文本框,即RegNo,Name,Address,在我的表中,我有$列RegNo,Name,Address,CreatedTime。在我的代码中遇到以下错误。

必须声明标量变量@RegNo

做什么。

解决方案

列RegNo,Name,Address,CreatedTime。在我的代码中遇到以下错误。

必须声明标量变量@RegNo

该怎么做。


当您创建参数化查询时 - 这正是执行SQL操作的正确方法 - 您还必须创建一个包含您希望传递给SQL的值的参数。这有点像在C#中调用方法。你定义它

  public   int  DoInsert ( int  regNo, string  name, string 地址,日期时间insertDate)
{
...
}

但是当你调用它时,你必须提供值:

< pre lang =c#> int rowsAffected = DoInsert( 666 Mike Smith 2 Main Street,Kentucky,DateTime.Now);

如果你遗漏了任何参数,就会出现编译错误。

SQL是一样的。

为每个参数添加一行:

 cmd.Parameters.AddWithValue(  @ NameOfParameterInSQLCommand,valueIWantInTheColumn ); 

它将开始起作用。


protected void Add_Click(object sender, EventArgs e)
      {


          string constr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
          SqlConnection con = new SqlConnection(constr);
          //SqlCommand cmd = new SqlCommand("insert into Student values('" + RegNo.Text + "','" + Name.Text + "','" + Address.Text + "')");
          SqlCommand cmd = new SqlCommand("insert into Student(@RegNo,@Name,@Address,@CreatedTime) values(@RegNo,@Name,@Address,Getdate())");

          cmd.Connection = con;
          con.Open();

          cmd.ExecuteNonQuery();
          con.Close();

      }



What I have tried:

here am try to insert records . i have 3 textboxes namely RegNo,Name,Address and in my table I have $ columns RegNo,Name,Address,CreatedTime. in my code the following error encountered .
Must declare the scalar variable @RegNo .
what to do.

解决方案

columns RegNo,Name,Address,CreatedTime. in my code the following error encountered .
Must declare the scalar variable @RegNo .
what to do.


When you create a parameterised query - and that is exactly the right way to do SQL operations - you have to also create a Parameter which contains teh value you wish to pass to SQL. It's a bit like calling a method in C#. You define it

public int DoInsert(int regNo, string name, string address, DateTime insertDate)
   {
   ...
   }

But when you call it, you must provide the values:

int rowsAffected = DoInsert(666, "Mike Smith", "2 Main Street, Kentucky", DateTime.Now);

If you miss out any parameters, you get a compiler error.
SQL is the same.
Add a line for each parameter:

cmd.Parameters.AddWithValue("@NameOfParameterInSQLCommand", valueIWantInTheColumn);

And it'll start to work.


这篇关于如何解决必须在SQL insert命令上声明标量变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆