[英]Invalid enumeration value and SqlDataAdapter.Update(DataTable)
I'm getting the ArgumentOutOfRangeException "The StatementType enumeration value, 4, is invalid" when trying to update a data table through SqlDataAdapter.Update(DataTable)
. 尝试通过
SqlDataAdapter.Update(DataTable)
更新数据表时,我收到ArgumentOutOfRangeException“ StatementType枚举值4,无效”。 According to http://msdn.microsoft.com/en-us/library/system.data.statementtype(v=vs.100).aspx , the possible enum values are Select, Insert, Update, Delete, and Batch, and I'm guessing Batch is value 4. 根据http://msdn.microsoft.com/zh-CN/library/system.data.statementtype(v=vs.100).aspx ,可能的枚举值为Select,Insert,Update,Delete和Batch,以及我猜Batch是值4。
I'm not really sure how this value is being set, as it belongs to the SqlRowUpdatedEventArgs object I'm getting in the SqlRowUpdatedEventHandler, or why it is invalid. 我不太确定如何设置此值,因为它属于我在SqlRowUpdatedEventHandler中获取的SqlRowUpdatedEventArgs对象,或者为什么无效。
Basically I'm building a DataTable from an XmlNode being returned from a web service call, sorting the data view, and then issuing the update in batches of 200. 基本上,我是通过从Web服务调用返回的XmlNode来构建DataTable的,对数据视图进行排序,然后以200个批次的价格发布更新。
XmlNode result = null;
using (LeadService.ClientService service = new LeadService.ClientService())
{
const string UserName = "SomeUserName";
const string Password = "SomePassword";
const int ChildReportId = 746;
result = service.GetReportResults(UserName, Password, ChildReportId, null);
}
if (result.InnerXml != "")
{
DataTable leads = new DataTable();
leads.Columns.Add("ID1", typeof(int));
leads.Columns.Add("ID2", typeof(string));
leads.Columns.Add("CREATEDDATE", typeof(DateTime));
foreach (XmlNode n in result.ChildNodes)
{
DataRow row = leads.NewRow();
row["ID1"] = n["Id1"].InnerText;
row["ID2"] = n["Id2"].InnerText;
row["CREATEDDATE"] = n["DateAdded"].InnerText;
leads.Rows.Add(row);
}
leads.DefaultView.Sort = "CREATEDDATE";
DataTable newTable = leads.DefaultView.ToTable();
newTable.AcceptChanges();
foreach (DataRow r in newTable.Rows)
{
r.SetModified();
}
const string Update = "UPDATE SOMETHING SET ID1 = @ID1 WHERE ID2 = @ID2";
using (SqlConnection con = new SqlConnection(Dal.GetConnectionString("CONNECTIONSTRING")))
{
SqlDataAdapter adap = new SqlDataAdapter();
adap.UpdateCommand = new SqlCommand(Update);
adap.UpdateCommand.Parameters.Add("@ID1", SqlDbType.NVarChar, 75, "ID1");
adap.UpdateCommand.Parameters.Add("@ID2", SqlDbType.VarChar, 18, "ID2");
adap.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
adap.UpdateBatchSize = 200;
adap.RowUpdated += new SqlRowUpdatedEventHandler(adap_RowUpdated);
adap.Update(newTable);
}
}
Here is the event handler: 这是事件处理程序:
private static void adap_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
_rowsProcessed += e.RowCount;
_recordsAffected += e.RecordsAffected;
}
Doh! 卫生署! I hadn't set the connection in the SqlCommand object!
我没有在SqlCommand对象中设置连接! Not sure about the reasoning behind the weird error, but changing the constructor overload cleared things up:
不知道奇怪错误背后的原因,但是更改构造函数重载可以清除所有内容:
using (SqlConnection con = new SqlConnection(Dal.GetConnectionString("CONNECTIONSTRING")))
{
SqlDataAdapter adap = new SqlDataAdapter();
adap.UpdateCommand = new SqlCommand(Update, con);
// ...
adap.Update(newTable);
}
It may be thinking you're adding rows instead of updating . 可能是您在添加行而不是在更新 。 Try moving
SetModified
to your loop: 尝试将
SetModified
移到循环中:
foreach (XmlNode n in result.ChildNodes)
{
DataRow row = leads.NewRow();
row["ID1"] = n["Id1"].InnerText;
row["ID2"] = n["Id2"].InnerText;
row["CREATEDDATE"] = n["DateAdded"].InnerText;
leads.Rows.Add(row);
row.SetModified();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.