【发布时间】:2012-01-09 12:22:23
【问题描述】:
我正在尝试通过 UpdateCommand 将我的数据集更改保存到数据库中。
我正在使用此代码:
SqlConnection con = new SqlConnection();
constr = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
con.ConnectionString = constr;
SqlCommand UpdateCommand = con.CreateCommand();
// SqlCommand locationstock = con.CreateCommand();
SqlCommand UpdateLocationStock = con.CreateCommand();
UpdateCommand.CommandText = "update tblCurrentGameData set sales=@sales,GameStatus=@gameStatus,QOH=@qoh, SalesRatio=@sr,PeriodSalesRatio=@psr,AvgAdjustmentRatio=@avgajdr,NewForeCastQty=@newQty,totalsales=@totSales,MAForCastQty=@movingAvgQty where locationid=@locationId and stockid=@stockId and IntervalTimeId=@periodId";
////UpdateCommand.CommandText = "update tblCurrentGameData set sales=@sales,GameStatus=@gameStatus,QOH=@qoh, SalesRatio=@sr,PeriodSalesRatio=@psr,AvgAdjustmentRatio=@avgajdr,NewForeCastQty=@newQty,totalsales=@totSales,MAForCastQty=@movingAvgQty where RecordId=@rid";
UpdateCommand.Parameters.Add("@sales", SqlDbType.Decimal, 18, "sales");
UpdateCommand.Parameters.Add("@gameStatus", SqlDbType.Bit, 1, "GameStatus");
UpdateCommand.Parameters.Add("@qoh", SqlDbType.Decimal, 18, "QOH");
UpdateCommand.Parameters.Add("@sr", SqlDbType.Decimal, 18, "SalesRatio");
UpdateCommand.Parameters.Add("@psr", SqlDbType.Decimal, 18, "PeriodSalesRatio");
UpdateCommand.Parameters.Add("@avgajdr", SqlDbType.Decimal, 18, "AvgAdjustmentRatio");
UpdateCommand.Parameters.Add("@newQty", SqlDbType.Decimal, 18, "NewForeCastQty");
UpdateCommand.Parameters.Add("@totSales", SqlDbType.Decimal, 18, "TotalSales");
UpdateCommand.Parameters.Add("@movingAvgQty", SqlDbType.Decimal, 18, "MAForCastQty");
UpdateCommand.Parameters.Add("@stockId", SqlDbType.Int, 16, "StockId");
UpdateCommand.Parameters.Add("@locationId", SqlDbType.Int, 16, "LocationId");
UpdateCommand.Parameters.Add("@periodId", SqlDbType.Int, 16, "IntervalTimeId");
UpdateLocationStock.CommandText = "update tblLocationSpecificStock set CurrentSalesAverage=@sav, SalesRatioAverage=@sravg where locationid=@locationId and stockid=@stockId ";
UpdateLocationStock.Parameters.Add("@sav", SqlDbType.Decimal, 16, "CurrentSalesAverage");
UpdateLocationStock.Parameters.Add("@sravg", SqlDbType.Decimal, 16, "SalesRatioAverage");
UpdateLocationStock.Parameters.Add("@stockId", SqlDbType.Int, 16, "StockId");
UpdateLocationStock.Parameters.Add("@locationId", SqlDbType.Int, 16, "LocationId");
da.UpdateCommand = UpdateCommand;
da.Update(gameData,"GameData");
da.UpdateCommand = UpdateLocationStock;
da.Update(gameData.Tables["LocationStockData"]);
gameData.AcceptChanges();
我的数据集“GameData”中有更改,但这些更改未保存在数据库中。 我在网上搜索了解决方案,但到处都找到了类似的解决方案。我被这个问题困住了 有人可以帮助我哪里出错了吗?或者如果对如何将我的数据集存储到数据库有任何新的建议。?
【问题讨论】:
-
如果您将其减少到最低限度的工作(或在您的情况下,非工作)示例,这可能会有所帮助。您当然可以编写一些测试代码来构建正确的
DataSet实例,您可以尝试将其保存到数据库中。 -
我尝试使用测试代码更新一些值,并成功更新了数据库。但同一部分不适用于这段代码。
-
在这种情况下,我首先要检查的是您尝试更新的 DataSet 实例中每一行的 RowState。
标签: c# sql sql-server-2008