SqlCommand[] commandItem
=
new
SqlCommand[items.Length];
SqlParameter[] iparameters = {
new SqlParameter("@PriceCode",SqlDbType.VarChar,20),
new SqlParameter("@ProductID",SqlDbType.Int,4),
new SqlParameter("@Price",SqlDbType.SmallMoney),
new SqlParameter("@ID",SqlDbType.Int,4),
} ;
int i = 0 ;
foreach (SampleItemInfo item in items)
{
iparameters[0].Value = item.SampleCode;
iparameters[1].Value = item.ProductID;
iparameters[2].Value = item.Num;
iparameters[3].Direction = ParameterDirection.Output;
commandItem[i] = BuildQueryCommand( "sp_Client_InsertPriceDetail",iparameters);
i++;
}
private SqlCommand BuildQueryCommand( string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand( storedProcName, Connection );
command.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add( parameter );
}
return command;
}
SqlParameter[] iparameters = {
new SqlParameter("@PriceCode",SqlDbType.VarChar,20),
new SqlParameter("@ProductID",SqlDbType.Int,4),
new SqlParameter("@Price",SqlDbType.SmallMoney),
new SqlParameter("@ID",SqlDbType.Int,4),
} ;
int i = 0 ;
foreach (SampleItemInfo item in items)
{
iparameters[0].Value = item.SampleCode;
iparameters[1].Value = item.ProductID;
iparameters[2].Value = item.Num;
iparameters[3].Direction = ParameterDirection.Output;
commandItem[i] = BuildQueryCommand( "sp_Client_InsertPriceDetail",iparameters);
i++;
}
private SqlCommand BuildQueryCommand( string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand( storedProcName, Connection );
command.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add( parameter );
}
return command;
}
以上代码出现另一个 SqlParameterCollection 已包含带有 ParameterName“@PriceCode”的 SqlParameter错误,
用下面的代码就正确了。每次新建参数。
SqlCommand[] commandItem
=
new
SqlCommand[items.Length];
int i = 0 ;
foreach (SampleItemInfo item in items)
{
SqlParameter[] iparameters = {
new SqlParameter("@PriceCode",SqlDbType.VarChar,20),
new SqlParameter("@ProductID",SqlDbType.Int,4),
new SqlParameter("@Price",SqlDbType.SmallMoney),
new SqlParameter("@ID",SqlDbType.Int,4),
};
iparameters[0].Value = item.SampleCode;
iparameters[1].Value = item.ProductID;
iparameters[2].Value = item.Num;
iparameters[3].Direction = ParameterDirection.Output;
commandItem[i] = BuildQueryCommand( "sp_Client_InsertPriceDetail",iparameters);
i++;
}
int i = 0 ;
foreach (SampleItemInfo item in items)
{
SqlParameter[] iparameters = {
new SqlParameter("@PriceCode",SqlDbType.VarChar,20),
new SqlParameter("@ProductID",SqlDbType.Int,4),
new SqlParameter("@Price",SqlDbType.SmallMoney),
new SqlParameter("@ID",SqlDbType.Int,4),
};
iparameters[0].Value = item.SampleCode;
iparameters[1].Value = item.ProductID;
iparameters[2].Value = item.Num;
iparameters[3].Direction = ParameterDirection.Output;
commandItem[i] = BuildQueryCommand( "sp_Client_InsertPriceDetail",iparameters);
i++;
}