我正在尝试使用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.
知道我做错了吗?
谢谢!
答案 0 :(得分:3)
在循环中实例化新的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();
}
但是,你确定要这么做吗?为什么不一次插入所有数据? - 比如看看Yucks的答案。
答案 1 :(得分:3)
您应该使用默认值在循环外创建这些参数,并在每次迭代期间访问它们并设置其新的参数参数。
总结:不要为每次迭代添加它们,访问它们!
编辑:
为了更具体,请查看此MSDN条目:
答案 2 :(得分:2)
cmd
的范围超出了while
循环,因此您设置的所有内容(即参数)都会在迭代之间保持不变。
将其更改为此以便它可以正常工作:
while (dr.Read())
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = SqlConn;
// ... rest as before
}
您最好填写DataSet
然后循环,或者将其结果累加到一次发送到存储过程。对于任何大型结果集,这将是非常低效的。
答案 3 :(得分:1)
在重用命令对象时,您将在第二次迭代时添加另一组参数。
在循环外添加参数,并设置循环内更改的值。
此外,您应该在循环之前打开数据库连接并在之后关闭它。
答案 4 :(得分:0)
您不需要每次都打开和关闭连接。
您不需要每次都创建新的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();