错误:变量名在查询批处理或存储过程中必须是唯一的。 [英] ERROR: Variable names must be unique within a query batch or stored procedure.

查看:99
本文介绍了错误:变量名在查询批处理或存储过程中必须是唯一的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个asp.net网站。当在学习框中输入学生ID时,一旦更改,就会从数据库中检索与id相关的结果并显示在指定的标签中。这是我的代码:

I have created an asp.net website.When the student id is typed in the textbox,as soon as it changes the results related to the id are retrieved from the database and displayed in the specified labels.this is my code:

protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        Match match = Regex.Match(TextBox1.Text, @"^\d{4}[A-Z]{5}\d{3}$");
        if (match.Success)
        {
            string dpt = (string)Session["deptmnt"];
         
            idd = TextBox1.Text;
            string num = idd.Substring(7,2);
            if (num.Equals(dpt))
            {
                Label33.Text = idd;
                string val = TextBox1.Text;
                string con = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
                SqlConnection con1 = new SqlConnection(con);
                con1.Open();
                try
                {
                    SqlDataReader readers = null;
                    SqlCommand myCommand1 = new SqlCommand("SELECT * from student where sid='" + val + "'", con1);
                    readers = myCommand1.ExecuteReader();
                    if (readers.HasRows == false)
                    {
                        readers.Close();
                        Label35.Visible = true;
                        TextBox1.Text = "";
                    }
                    else
                    {
                        try
                        {

                            while (readers.Read()) // if can read row from database
                            {
                                try
                                {
                                    Panel3.Visible = true;
                                    Label3.Text = readers["sname"].ToString();
                                    Label5.Text = readers["dept"].ToString();
                                    Label25.Text = readers["yr"].ToString();
                                }
                                catch (Exception ex)
                                {
                                    TextBox1.Text = ex.ToString();
                                }

                                //DropDownList1.Items.Add(
                            }
                            readers.Close();
                            con1.Close();
                        }
                        catch (Exception ex)
                        {
                            TextBox1.Text = ex.ToString();
                        }
                        DropDownList1.Focus();

                    }

                }
                catch (Exception ex)
                {
                    TextBox1.Text = ex.ToString();
                }
            }
            else
            {
                Label35.Visible = true;
            
            }
        }
        else
        {
            formatlabel.Visible = true;
        }
    }





但是,当我调试时,我收到错误说变量名称@ sid'已经被声明了。变量名在查询批处理或存储过程中必须是唯一的。我用try catch到处查找确切的原因。我做了很多修改以找到错误来源的来源,但是,无法。,我在另一个webform中也得到了相同的错误。,plz help。



but,when i debug,i get an error saying "The variable name '@sid' has already been declared. Variable names must be unique within a query batch or stored procedure."I used try catch everywhere to find the exact cause.I did many changes to find where the source of the error is coming,but,was unable to.,i get the same error in another webform also.,plz help.

推荐答案

);
if (match.Success)
{
string dpt =(字符串)会话[ deptmnt];

idd = TextBox1.Text;
string num = idd.Substring( 7 ,< span class =code-digit> 2 );
if (num.Equals(dpt))
{
Label33.Text = idd;
string val = TextBox1.Text;
string con = ConfigurationManager.ConnectionStrings [ CON]的ConnectionString。
SqlConnection con1 = new SqlConnection(con);
con1.Open();
try
{
SqlDataReader readers = null ;
SqlCommand myCommand1 = new SqlCommand( SELECT *来自学生,其中sid =' + val + ',con1);
readers = myCommand1.ExecuteReader();
if (readers.HasRows == false
{
readers.Close();
Label35.Visible = true ;
TextBox1.Text = ;
}
其他
{
尝试
{

while (readers.Read()) // < span class =code-comment> if是否可以从数据库中读取行

{
尝试
{
Panel3.Visible = true ;
Label3.Text = readers [ sname]。ToString();
Label5.Text = readers [ dept]。ToString();
Label25.Text = readers [ yr]。ToString();
}
catch (例外情况)
{
TextBox1.Text = ex.ToString();
}

// DropDownList1.Items.Add(
}
readers.Close();
con1.Close();
}
catch (例外情况ex)
{
TextBox1.Text = ex.ToString();
}
DropDownList1.Focus();

}

}
catch (例外情况)
{
TextBox1.Text = ex.ToString();
}
}
else
{
Label35.Visible = ;

}
}
else
{
formatlabel.Visible = ;
}
}
"); if (match.Success) { string dpt = (string)Session["deptmnt"]; idd = TextBox1.Text; string num = idd.Substring(7,2); if (num.Equals(dpt)) { Label33.Text = idd; string val = TextBox1.Text; string con = ConfigurationManager.ConnectionStrings["con"].ConnectionString; SqlConnection con1 = new SqlConnection(con); con1.Open(); try { SqlDataReader readers = null; SqlCommand myCommand1 = new SqlCommand("SELECT * from student where sid='" + val + "'", con1); readers = myCommand1.ExecuteReader(); if (readers.HasRows == false) { readers.Close(); Label35.Visible = true; TextBox1.Text = ""; } else { try { while (readers.Read()) // if can read row from database { try { Panel3.Visible = true; Label3.Text = readers["sname"].ToString(); Label5.Text = readers["dept"].ToString(); Label25.Text = readers["yr"].ToString(); } catch (Exception ex) { TextBox1.Text = ex.ToString(); } //DropDownList1.Items.Add( } readers.Close(); con1.Close(); } catch (Exception ex) { TextBox1.Text = ex.ToString(); } DropDownList1.Focus(); } } catch (Exception ex) { TextBox1.Text = ex.ToString(); } } else { Label35.Visible = true; } } else { formatlabel.Visible = true; } }





但是,当我调试时,我收到错误说变量名称@ sid'已经被声明了。变量名在查询批处理或存储过程中必须是唯一的。我用try catch到处查找确切的原因。我做了很多修改以找到错误来源的来源,但是,无法。,我在另一个webform中也得到同样的错误。,plz help。



but,when i debug,i get an error saying "The variable name '@sid' has already been declared. Variable names must be unique within a query batch or stored procedure."I used try catch everywhere to find the exact cause.I did many changes to find where the source of the error is coming,but,was unable to.,i get the same error in another webform also.,plz help.


你听说过 SQLInjection [ ^ ]?很少有防止SQLInjection的方法 [ ^ ]。其中之一是使用存储过程 [ ^ ]。



如果你想知道如何从后面的代码中调用SP,请参阅:

如何:执行返回行的存储过程 [ ^ ]

如何:执行返回单个值的存储过程 [ ^ ]

如何:执行不返回值的存储过程 [ ^ ]

如何:为命令对象设置和获取参数 [ ^ ]





启动是否足够?
Have you ever heard about SQLInjection[^]? There are few ways to protect from SQLInjection[^]. One of them is to use stored procedures[^].

If you want to know how to call SP from code behind, see these:
How to: Execute a Stored Procedure that Returns Rows[^]
How to: Execute a Stored Procedure that Returns a Single Value[^]
How to: Execute a Stored Procedure that Returns No Value[^]
How to: Set and Get Parameters for Command Objects[^]


Is it enough for start?


抱歉故障是我的。我一步一步调试,发现,在我的gridview中,我已经将sid作为同一查询中两个条件的相同变量
sorry the fault is mine.,I debugged step by step and found,in my gridview i had given "sid" as the same variable for two conditions in the same query


这篇关于错误:变量名在查询批处理或存储过程中必须是唯一的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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