以下是我用于批量更新(约14k条记录)我们的ZipCodeTerritory
表的代码。代码运行正常,没有任何例外,但是当我在connection
关闭后检查表时,我发现没有任何更改。
我持有需要在方法之间传递的List
对象中更新的所有记录。我使用此List
填充DataTable
并将其放入DataSet
以执行更新。
这是我尝试使用此技术的第一次尝试,所以任何建议都非常感谢。
private static string selectCommand =
"SELECT ChannelCode, DrmTerrDesc, IndDistrnId, StateCode, ZipCode, EndDate,
EffectiveDate, LastUpdateId, LastUpdateDate, Id, ErrorCodes, Status FROM
ZipCodeTerritory";
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(selectCommand, connection))
{
try
{
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);
connection.Open();
adapter.Fill(ds, "ZipCodeTerritory");
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[9] = zipcode.Id;
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;
}
修改
我已更改.Fill
和LoadDataSet()
方法的顺序,首先从数据库中加载当前所在的数据,然后将更改合并到DataSet
中。但是,我现在收到以下错误:
`Update unable to find TableMapping['ZipCodeTerritory'] or DataTable 'ZipCodeTerritory'`
我意识到我没有为表格命名,因此我将InitializeStructure()
方法更改为以下
private static DataTable InitializeStructure()
{
DataTable dt = new DataTable("ZipCodeTerritory");
然而....现在当我在这一行尝试.Update
时
adapter.Update(ds, "ZipCodeTerritory");
我得到了这个例外:
Update requires a valid InsertCommand when passed DataRow collection with new rows.
第二次编辑
我已将以下InsertCommand
添加到Update
方法(这是包含try
的{{1}}内的前几行代码:
updateCommand
但是我仍然没有看到数据库发生任何变化。