这是我第一次尝试使用SqlAdapter
对SQL执行批量更新。我正在尝试关注演示on this page,但是adapter.Fill(ds, "ZipCodeTerritory");
行不断给我错误
Must declare the scalar variable \"@ChannelCode\"
我正在尝试将List
的内容加载到DataSet
对象中,然后使用.Update()
上的SqlAdapter
方法执行批量更新( 14k记录)。
private static string updateCommand = "UPDATE ZipCodeTerritory SET ChannelCode = @ChannelCode, DrmTerrDesc = @DrmTerrDesc, IndDistrnId = @IndDistrnId," +
"StateCode = @StateCode, ZipCode = @ZipCode, EndDate = @EndDate, EffectiveDate = @EffectiveDate," +
"LastUpdateId = @LastUpdateId, LastUpdateDate = @LastUpdateDate, ErrorCodes = @ErrorCodes," +
"Status = @Status " +
"WHERE Id = @Id";
public static void Update(List<ZipCodeTerritory> updates, Dictionary<ZipCodeTerritory, string> errorList)
{
using (SqlConnection connection = new SqlConnection(connString))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(updateCommand, connection))
{
try
{
DataTable dataTable = LoadData(updates);
SqlCommand updateCmd = connection.CreateCommand();
updateCmd.CommandText = updateCommand;
updateCmd.Parameters.Add(new SqlParameter("@ChannelCode", SqlDbType.Char, 1, "ChannelCode"));
updateCmd.Parameters.Add(new SqlParameter("@DrmTerrDesc", SqlDbType.Char, 1, "DrmTerrDesc"));
updateCmd.Parameters.Add(new SqlParameter("@IndDistrnId", SqlDbType.Char, 1, "IndistrnId"));
updateCmd.Parameters.Add(new SqlParameter("@StateCode", SqlDbType.Char, 1, "StateCode"));
updateCmd.Parameters.Add(new SqlParameter("@ZipCode", SqlDbType.Char, 1, "ZipCode"));
updateCmd.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.Char, 1, "EndDate"));
updateCmd.Parameters.Add(new SqlParameter("@EffectiveDate", SqlDbType.Char, 1, "EffectiveDate"));
updateCmd.Parameters.Add(new SqlParameter("@LastUpdateId", SqlDbType.Char, 1, "LastUpdateId"));
updateCmd.Parameters.Add(new SqlParameter("@LastUpdateDate", SqlDbType.Char, 1, "LastUpdateDate"));
updateCmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.Char, 1, "Id"));
updateCmd.Parameters.Add(new SqlParameter("@ErrorCodes", SqlDbType.Char, 1, "ErrorCodes"));
updateCmd.Parameters.Add(new SqlParameter("@Status", SqlDbType.Char, 1, "Status"));
updateCmd.UpdatedRowSource = UpdateRowSource.None;
adapter.UpdateCommand = updateCmd;
adapter.AcceptChangesDuringUpdate = true;
DataSet ds = LoadDataSet(updates);
adapter.Fill(ds, "ZipCodeTerritory");
connection.Open();
adapter.Update(ds, "ZipCodeTerritory");
connection.Close();
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
}
}
private static DataSet LoadDataSet(List<ZipCodeTerritory> zipcodeList)
{
DataSet ds = new DataSet();
DataTable data = LoadData(zipcodeList);
ds.Tables.Add(data);
return ds;
}
private static DataTable LoadData(List<ZipCodeTerritory>zipCodeList)
{
DataTable dataTable = InitializeStructure();
foreach (var zipcode in zipCodeList)
{
DataRow row = dataTable.NewRow();
try
{
row[0] = zipcode.ChannelCode.Trim();
row[1] = zipcode.DrmTerrDesc.Trim();
row[2] = zipcode.IndDistrnId.Trim();
row[3] = zipcode.StateCode.Trim();
row[4] = zipcode.ZipCode.Trim();
row[5] = zipcode.EndDate.Date;
row[6] = zipcode.EffectiveDate.Date;
row[7] = zipcode.LastUpdateId;
row[8] = DateTime.Now.Date;
row[10] = zipcode.ErrorCodes;
row[11] = zipcode.Status;
}
catch (Exception ex)
{
}
dataTable.Rows.Add(row);
}
return dataTable;
}
private static DataTable InitializeStructure()
{
DataTable dt = new DataTable();
dt.Columns.Add("ChannelCode", typeof (string));
dt.Columns.Add("DrmTerrDesc", typeof (string));
dt.Columns.Add("IndDistrnId", typeof (string));
dt.Columns.Add("StateCode", typeof (string));
dt.Columns.Add("ZipCode", typeof (string));
dt.Columns.Add("EndDate", typeof (DateTime));
dt.Columns.Add("EffectiveDate", typeof (DateTime));
dt.Columns.Add("LastUpdateId", typeof (string));
dt.Columns.Add("LastUpdateDate", typeof (DateTime));
dt.Columns.Add("Id", typeof (int));
dt.Columns.Add("ErrorCodes", typeof (string));
dt.Columns.Add("Status", typeof (string));
return dt;
}
答案 0 :(得分:1)
问题是这一行:
using (SqlDataAdapter adapter = new SqlDataAdapter(updateCommand, connection))
SqlDataAdapter构造函数需要 Select 命令,Update命令将不起作用。当您尝试执行Fill
方法时,它会失败,因为它没有要应用的.SelectCommand
参数。