using(SqlConnection conn = new SqlConnection(ConfigHelper.IagentDBConnectString))
{
if(conn.State!=ConnectionState.Open)
{
conn.Open();
}
SqlDataAdapter sda = new SqlDataAdapter("select * from TProductPrice where pid="+p_ProductPriceArr[0].PID,conn);
DataSet ds = new DataSet();
sda.Fill(ds);
//清除PID的价格记录
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
ds.Tables[0].Rows[i].Delete();
}
//增加新记录
for(int i=0;i<p_ProductPriceArr.Count;i++)
{
DataRow dr = ds.Tables[0].NewRow();
dr["PID"] = p_ProductPriceArr[i].PID;
dr["LevelID"] = p_ProductPriceArr[i].LevelID ;
dr["TimeType"] = p_ProductPriceArr[i].TimeType ;
dr["TimeInt"] = p_ProductPriceArr[i].TimeInt ;
if(p_ProductPriceArr[i].UnitPrice.Length<=0)
{
dr["UnitPrice"] = System.DBNull.Value;
}
else
{
dr["UnitPrice"] = p_ProductPriceArr[i].UnitPrice ;
}
ds.Tables[0].Rows.Add(dr);
}
sda.InsertCommand = new SqlCommand();
sda.InsertCommand.Connection = conn;
sda.DeleteCommand = new SqlCommand();
sda.DeleteCommand.Connection = conn;
sda.InsertCommand.CommandText = SQL_INSERT_ALL_PRODUCTPRICE;
sda.DeleteCommand.CommandText = SQL_DELETE_ONE_PRODUCTPRICE;
sda.InsertCommand.Parameters.Add(PARM_PID,SqlDbType.SmallInt,4,"PID");
sda.DeleteCommand.Parameters.Add(PARM_PID,SqlDbType.SmallInt,4,"PID");
sda.InsertCommand.Parameters.Add(PARM_LEVELID,SqlDbType.SmallInt,2,"LevelID");
sda.InsertCommand.Parameters.Add(PARM_TIMETYEP,SqlDbType.Bit,1,"TimeType");
sda.InsertCommand.Parameters.Add(PARM_TIMEINT,SqlDbType.SmallInt,2,"TimeInt");
sda.InsertCommand.Parameters.Add(PARM_UNITPRICE,SqlDbType.VarChar,20,"UnitPrice");
int Rint = 0;
try
{
Rint = sda.Update(ds);
}
catch
{
//ds.Tables[0].RejectChanges();
}
if(Rint<=0)
{ds.Tables[0].RejectChanges();}
else
{ds.Tables[0].AcceptChanges();}
return Rint;
}
ds中我先删除所有行...然后新增行...
sda.Update(ds);的时候抛出了异常,但是数据库中的所有行已经被删除了,新增的行没有成功。。
///update没有用事务控制吗?怎么解决这个问题?
异常信息:
System.Data.DBConcurrencyException: 并发冲突: DeleteCommand 影响 0 个记录。 at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet) at Booksir.Iagent.SQLServerDAL.ProductPriceDAL.EditProductPrice(ProductPriceModelCollection p_ProductPriceArr) in e:\iagent\project\sqlserverdal\productpricedal.cs:line 189 at Booksir.Iagent.BLL.ProductPriceBLL.EditProductPrice(ProductPriceModelCollection p_ProductPriceArr) in E:\Iagent\project\BLL\ProductPriceBLL.cs:line 75
不知道问题出在那里?或者有什么好的解决方案/////请牛人指教