簡體   English   中英

如何將數據集值保存到數據庫

[英]How to save a dataset value to a database

我正在嘗試通過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”中進行了更改,但這些更改未保存在數據庫中。 我在網上搜索了解決方案,但是到處都可以找到類似的解決方案。 我陷入了這個問題,有人可以幫我解決我的問題嗎? 或對如何將數據集存儲到數據庫有任何新建議?

如果要對數據庫應用更改,則應在命令中設置條件,這非常重要,因為我剛剛意識到,一旦遇到此問題。 並向查詢中添加條件。我的意思是->“ update ... where id = 1”最好,數據集喜歡主鍵或唯一鍵來保存對數據庫的更改。 如果可以,請嘗試使用一個主鍵而不是組合鍵進行更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM