SQL插入While循环

本文关键字:循环 While 插入 SQL | 更新日期: 2023-09-27 18:22:02

我正试图使用while循环来循环一些数据,然后将每个数据添加到sql中。这是我迄今为止所做的代码:

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = SqlConn;
        while (dr.Read())
            {
                cmd.CommandText = "insert into CPC_Coupons(PortalID, CreatedByUser, CouponCode, ProductID, ExpiresOn, Quantity, Title, FirstName, LastName, Company, Address1, City, Region, Zip, Country, WorkPhone, Email, Campaign, Source, Market, Notes) values(@PortalID, @CreatedByUser, @Coupon, @ProductID, @ExpiresOn, @Quantity, @Title, @FirstName, @LastName, @Company, @Address1, @City, @Region, @Zip, @Country, @WorkPhone, @Email, @Campaign, @Source, @Market, @Notes)";
                cmd.Parameters.Add("@PortalID", SqlDbType.NVarChar).Value = 0;
                cmd.Parameters.Add("@Coupon", SqlDbType.NVarChar).Value = dr[0].ToString();
                cmd.Parameters.Add("@CreatedByUser", SqlDbType.NVarChar).Value = "3517";
                cmd.Parameters.Add("@ProductID", SqlDbType.NVarChar).Value = "0";
                cmd.Parameters.Add("@ExpiresOn", SqlDbType.NVarChar).Value = "01/01/2013";
                cmd.Parameters.Add("@Quantity", SqlDbType.NVarChar).Value = "100";
                cmd.Parameters.Add("@Title", SqlDbType.NVarChar).Value = "Mr.";
                cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = dr[3].ToString();
                cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = dr[4].ToString();
                cmd.Parameters.Add("@Company", SqlDbType.NVarChar).Value = dr[2].ToString();
                cmd.Parameters.Add("@Address1", SqlDbType.NVarChar).Value = dr[5].ToString();
                cmd.Parameters.Add("@City", SqlDbType.NVarChar).Value = dr[6].ToString();
                cmd.Parameters.Add("@Region", SqlDbType.NVarChar).Value = dr[7].ToString();
                cmd.Parameters.Add("@Zip", SqlDbType.NVarChar).Value = dr[8].ToString();
                cmd.Parameters.Add("@Country", SqlDbType.NVarChar).Value = dr[9].ToString();
                cmd.Parameters.Add("@WorkPhone", SqlDbType.NVarChar).Value = dr[10].ToString();
                cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = dr[11].ToString();
                cmd.Parameters.Add("@Campaign", SqlDbType.NVarChar).Value = txtCampaign.Text;
                cmd.Parameters.Add("@Source", SqlDbType.NVarChar).Value = dr[12].ToString();
                cmd.Parameters.Add("@Market", SqlDbType.NVarChar).Value = txtMarketSegment.Text;
                cmd.Parameters.Add("@Notes", SqlDbType.NVarChar).Value = txtNotesToSales.Text;
                cmd.CommandType = CommandType.Text;
                SqlConn.Open();
                cmd.ExecuteNonQuery();
                SqlConn.Close();
            }

但第二次通过时,它会到达cmd.ExecuteNonQuery();,并给出以下错误:The variable name '@PortalID' has already been declared. Variable names must be unique within a query batch or stored procedure.

知道我做错了什么吗?

谢谢!

SQL插入While循环

在循环中实例化一个新的SQLCommand可能会解决您的问题。像这样:

    while (dr.Read())
    {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = SqlConn;
            cmd.CommandText = "insert into CPC_Coupons(PortalID, CreatedByUser, CouponCode, ProductID, ExpiresOn, Quantity, Title, FirstName, LastName, Company, Address1, City, Region, Zip, Country, WorkPhone, Email, Campaign, Source, Market, Notes) values(@PortalID, @CreatedByUser, @Coupon, @ProductID, @ExpiresOn, @Quantity, @Title, @FirstName, @LastName, @Company, @Address1, @City, @Region, @Zip, @Country, @WorkPhone, @Email, @Campaign, @Source, @Market, @Notes)";
            cmd.Parameters.Add("@PortalID", SqlDbType.NVarChar).Value = 0;
            cmd.Parameters.Add("@Coupon", SqlDbType.NVarChar).Value = dr[0].ToString();
            cmd.Parameters.Add("@CreatedByUser", SqlDbType.NVarChar).Value = "3517";
            cmd.Parameters.Add("@ProductID", SqlDbType.NVarChar).Value = "0";
            cmd.Parameters.Add("@ExpiresOn", SqlDbType.NVarChar).Value = "01/01/2013";
            cmd.Parameters.Add("@Quantity", SqlDbType.NVarChar).Value = "100";
            cmd.Parameters.Add("@Title", SqlDbType.NVarChar).Value = "Mr.";
            cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = dr[3].ToString();
            cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = dr[4].ToString();
            cmd.Parameters.Add("@Company", SqlDbType.NVarChar).Value = dr[2].ToString();
            cmd.Parameters.Add("@Address1", SqlDbType.NVarChar).Value = dr[5].ToString();
            cmd.Parameters.Add("@City", SqlDbType.NVarChar).Value = dr[6].ToString();
            cmd.Parameters.Add("@Region", SqlDbType.NVarChar).Value = dr[7].ToString();
            cmd.Parameters.Add("@Zip", SqlDbType.NVarChar).Value = dr[8].ToString();
            cmd.Parameters.Add("@Country", SqlDbType.NVarChar).Value = dr[9].ToString();
            cmd.Parameters.Add("@WorkPhone", SqlDbType.NVarChar).Value = dr[10].ToString();
            cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = dr[11].ToString();
            cmd.Parameters.Add("@Campaign", SqlDbType.NVarChar).Value = txtCampaign.Text;
            cmd.Parameters.Add("@Source", SqlDbType.NVarChar).Value = dr[12].ToString();
            cmd.Parameters.Add("@Market", SqlDbType.NVarChar).Value = txtMarketSegment.Text;
            cmd.Parameters.Add("@Notes", SqlDbType.NVarChar).Value = txtNotesToSales.Text;
            cmd.CommandType = CommandType.Text;
            SqlConn.Open();
            cmd.ExecuteNonQuery();
            SqlConn.Close();
        }

但是,你确定要这么做吗。为什么不一次性插入所有数据-例如,请参阅尤克斯的答案。

您应该在循环外使用默认值创建这些参数,并在每次迭代期间访问它们,并设置其新的参数自变量。

总结:不要为每次迭代添加它们,要访问它们!

编辑:

为了更具体,请查看以下MSDN条目:

  • SqlParameterCollection索引器http://msdn.microsoft.com/en-us/library/dk96b781.aspx

cmdwhile循环之外有一个作用域,因此您设置的所有内容(即参数)都会在迭代之间持续存在。

将其更改为这样它就会工作:

while (dr.Read())
{
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = SqlConn;
    // ... rest as before
}

您最好填充一个DataSet,然后循环使用它,或者累积它的结果,将其发送一次到存储过程。对于任何大的结果集,这将是非常低效的。

当您重用命令对象时,您将在第二次迭代中添加另一组参数。

添加循环外的参数,并设置循环内更改的值。

此外,您应该在循环之前打开数据库连接,然后关闭它。

您不需要每次都打开和关闭连接。

您不需要每次都创建一个新的SqlCommand。

SqlCommand cmd = new SqlCommand();
cmd.Connection = SqlConn;
cmd.CommandText = "insert into CPC_Coupons(PortalID, CreatedByUser, CouponCode, ProductID, ExpiresOn, Quantity, Title, FirstName, LastName, Company, Address1, City, Region, Zip, Country, WorkPhone, Email, Campaign, Source, Market, Notes) values(@PortalID, @CreatedByUser, @Coupon, @ProductID, @ExpiresOn, @Quantity, @Title, @FirstName, @LastName, @Company, @Address1, @City, @Region, @Zip, @Country, @WorkPhone, @Email, @Campaign, @Source, @Market, @Notes)";
cmd.CommandType = CommandType.Text;
SqlConn.Open();
while (dr.Read())
{
    cmd.Parameters.Clear()
    cmd.Parameters.Add("@PortalID", SqlDbType.NVarChar).Value = 0;
    ''' other parameters here
    cmd.ExecuteNonQuery();
}
SqlConn.Close();