2012-01-09 59 views
1

我想通过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 [email protected],[email protected],[email protected], [email protected],[email protected],[email protected],[email protected],[email protected],[email protected] where [email protected] and [email protected] and [email protected]"; 
////UpdateCommand.CommandText = "update tblCurrentGameData set [email protected],[email protected],[email protected], [email protected],[email protected],[email protected],[email protected],[email protected],[email protected] where [email protected]"; 
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 [email protected], [email protected] where [email protected] and [email protected] "; 
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”的变化,但这些变化都不会保存在数据库:

我使用这个代码。 我在网上搜索解决方案,但到处都找到类似的解决方案。我被困在这个问题 有人可以帮助我,我哪里出错了吗?或者如果有任何关于如何将数据集存储到数据库的新建议?

+1

如果你把这个降到最低限度的工作(或者你的情况,非工作)的例子,它可能会有所帮助。您当然可以编写一些测试代码来构建适当的'DataSet'实例,您可以尝试将其保存到数据库中。 – 2012-01-09 12:26:09

+0

我尝试使用测试代码来更新一些值,并成功更新了数据库。但是相同的部分不适用于这段代码。 – Surbhi 2012-01-09 12:28:07

+0

在这种情况下,我要检查的第一件事是您尝试基于更新的DataSet实例中每行的RowState。 – 2012-01-09 12:29:20

回答

0

如果你想对数据库应用更改,你应该在你的命令上有标准,这是非常重要的,因为我只是意识到,一旦我有这个问题。我的意思是添加标准。我的意思是 - >“update ... where id = 1” 最好,数据集喜欢主键或唯一键来保存对数据库的更改。 如果可以,请尝试使用一个主键来更新而不是组合键。

相关问题