错误:必须在C#中声明标量变量 [英] Error : must declare the scalar variable in C#

查看:381
本文介绍了错误:必须在C#中声明标量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须声明标量变量@code



我试过没有动态查询n它的工作原理,但是当我试图传递参数时发送给我错误,请帮忙



问候



我的尝试:



 public void Insert()
{
ReglaController re = new ReglaController();
re.Regla_2();

string constr = ConfigurationManager.ConnectionStrings [Constring]。ConnectionString;
using(SqlConnection con = new SqlConnection(constr))
{
con.Open();

string query =INSERT INTO test(r1)VALUES(@code);

使用(SqlCommand cmd = new SqlCommand(query,con))
{
cmd.ExecuteNonQuery();


cmd.Parameters.Add(@ code,SqlDbType.NVarChar).Value = re.Regla_2();



}

}

}

解决方案

嗯......再看看你的代码。显而易见的是问题是什么。



您正在调用ExecuteNonQuery来执行INSERT查询,然后您尝试添加定义@code的参数。那显然是倒退了。交换这两行代码然后这应该可行。



您可以通过一点思考和使用调试器来解决这个问题。

我解决了它





 public void Insert()
{
ReglaController re = new ReglaController();
re.Regla_2();

string constr = ConfigurationManager.ConnectionStrings [Constring]。ConnectionString;
using(SqlConnection con = new SqlConnection(constr))
{
con.Open();

string query =INSERT INTO test(r1)VALUES(@code);

使用(SqlCommand cmd = new SqlCommand(query,con))
{
cmd.ExecuteNonQuery();


cmd.Parameters.Add(@ code,SqlDbType.NVarChar).Value = re.Regla_2();


}

}

}


Must declare the scalar variable @code

I tried with no dynamic query n its works , but when i tried to pass parameters send me that error , please help

regards

What I have tried:

public void Insert()
       {
           ReglaController re = new ReglaController();
           re.Regla_2();

           string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
           using (SqlConnection con = new SqlConnection(constr))
           {
               con.Open();

               string query = "INSERT INTO test (r1) VALUES (@code)";

               using (SqlCommand cmd = new SqlCommand(query, con))
               {
                   cmd.ExecuteNonQuery();


                  cmd.Parameters.Add("@code", SqlDbType.NVarChar).Value = re.Regla_2();



               }

           }

       }

解决方案

Ummm... look at your code again. It's plainly obvious what the problem is.

You're calling ExecuteNonQuery to execute the INSERT query and THEN you attempt to add the parameter that defines @code. That's obviously backwards. Swap those two lines of code and then this should work.

You could have figured this out with a little bit of thought and the use of the debugger.


I solved it


public void Insert()
        {
            ReglaController re = new ReglaController();
            re.Regla_2();

            string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                con.Open();

                string query = "INSERT INTO test (r1) VALUES (@code)";

                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    cmd.ExecuteNonQuery();


                    cmd.Parameters.Add("@code", SqlDbType.NVarChar).Value = re.Regla_2(); 
                    

                }

            }

        }


这篇关于错误:必须在C#中声明标量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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