1

我根据下拉列表中的选择将多个项目插入表中。当我从下拉列表中选择一个项目时,一切正常,但是当我选择多个项目时,我收到此错误

The variable name '@CompName' has already been declared. Variable names must be unique within a query batch or stored procedure.

我究竟做错了什么?谢谢这是我的代码

protected void DV_Test_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        foreach (ListItem listItem in cblCustomerList.Items)
        {
            if (listItem.Selected)
            {
                string Name= listItem.Value;
                sqlcon.Open();
                string CompName= ((TextBox)DV_Test.FindControl("txtCompName")).Text.ToString();
                string Num = ((TextBox)DV_Test.FindControl("txtNum")).Text.ToString();
                SqlCommand cmd = new SqlCommand("select CompNamefrom MyTable where CompName= '" + CompName+ "' and Num = '" + Num + "' and Name= '" + Name+ "'    ", sqlcon);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    lblmsg.Text = "Not Valid";
                }
                else
                {
                    dr.Close();

                    sqlcmd.CommandText = "INSERT INTO MyTable(CompName, Num, Name) VALUES(@CompName, @Num, @Name)";
                    sqlcmd.Parameters.Add("@CompName", SqlDbType.VarChar).Value = CompName;
                    sqlcmd.Parameters.Add("@Num", SqlDbType.VarChar).Value = Num;
                    sqlcmd.Connection = sqlcon;                 
                    sqlcmd.ExecuteNonQuery();
                    DV_Test.ChangeMode(DetailsViewMode.Insert);
                    sqlcon.Close();
                }
                sqlcon.Close();               
            }

         }
 }
4

4 回答 4

5

每次通过循环时,您都在添加参数@CompName和查询。@Num

您需要将调用移到Add循环外部并使用以下内容更新它们:

sqlcmd.Parameters["@CompName"].Value = CompName;
sqlcmd.Parameters["@Num"].Value = Num;

所以你的代码变成:

sqlcmd.CommandText = "INSERT INTO MyTable(CompName, Num, Name) VALUES(@CompName, @Num, @Name)";
sqlcmd.Parameters.Add("@CompName", SqlDbType.VarChar);
sqlcmd.Parameters.Add("@Num", SqlDbType.VarChar);

foreach (ListItem listItem in cblCustomerList.Items)
{
    if (....)
    {
        ....
    }
    else
    {
        dr.Close();

        sqlcmd.Parameters["@CompName"].Value = CompName;
        sqlcmd.Parameters["@Num"].Value = Num;
        sqlcmd.Connection = sqlcon;                 
        sqlcmd.ExecuteNonQuery();
        DV_Test.ChangeMode(DetailsViewMode.Insert);
        sqlcon.Close();
    }

}
于 2012-12-13T20:45:52.490 回答
5

sqlcmd.Parameters.Clear();在最后添加。这样每个参数在每个循环结束时都不会存在。

例子:

// ...
sqlcmd.CommandText = "INSERT INTO MyTable(CompName, Num, Name) VALUES(@CompName, @Num, @Name)";
                    sqlcmd.Parameters.Add("@CompName", SqlDbType.VarChar).Value = CompName;
                    sqlcmd.Parameters.Add("@Num", SqlDbType.VarChar).Value = Num;
                    sqlcmd.Connection = sqlcon;                 
                    sqlcmd.ExecuteNonQuery();
                    DV_Test.ChangeMode(DetailsViewMode.Insert);
                    sqlcon.Close();
                    sqlcmd.Parameters.Clear();
//...
于 2013-07-25T09:43:18.433 回答
2

我在这里的假设是因为您已经sqlcmd在循环范围之外声明了;因此,当您对其进行迭代时,您可能会多次添加相同的参数名称。

请修复这一行: SqlCommand cmd = new SqlCommand("select CompNamefrom MyTable where CompName= '" + CompName+ "' and Num = '" + Num + "' and Name= '" + Name+ "' ", sqlcon); 因为它正在等待 SQL 注入发生。

于 2012-12-13T20:48:59.933 回答
1

只需添加以下内容:

SqlCmd.Parameters.Clear();

在您的代码中,在添加参数之前。它将清除已添加的参数列表并将您的参数视为新参数。

于 2016-09-27T11:46:55.090 回答