2010-08-04 67 views
0

問題是當我添加第二個datarow數據集。如果我刪除//意見,我只拿到1行補充,而不是80問題與SQLDataAdapter和更新

SqlDataAdapter indicatorsExp = new SqlDataAdapter(); 
string sqlExp = "SELECT * FROM BusinessApplications.tbl_WPI_Site_Indicators_Exp " + 
       "where Year = '" + year + "' and Month = '" + month + "'"; 
indicatorsExp.SelectCommand = new SqlCommand(sqlExp, conn); 
SqlCommandBuilder cbexp = new SqlCommandBuilder(indicatorsExp); 
indicatorsExp.InsertCommand = cbexp.GetInsertCommand(); 
DataSet dsExp = new DataSet(); 
indicatorsExp.Fill(dsExp, "explanations"); 
DataTable explanations = dsExp.Tables["explanations"]; 

//....... 
foreach (ISite site in sites) 
{ 
    DataRow drexp1 = explanations.NewRow(); 
    try 
    { 
     drexp1["PlantId"] = site.ID; 
     drexp1["Month"] = month; 
     drexp1["Year"] = year; 
    } 
    catch { } 
    DataRow drexp2 = explanations.NewRow(); 
    try 
    { 
     drexp2["PlantId"] = site.ID; 
     drexp2["Month"] = month; 
     drexp2["Year"] = year; 
    } 
    catch { } 

    explanations.Rows.Add(drexp1); 
    indicatorsExp.Update(dsExp, "explanations"); 
// explanations.Rows.Add(drexp2); 
// indicatorsExp.Update(dsExp, "explanations"); 

    } 
+0

你可以讓我們在你想要在這裏玩魔術嗎?它看起來很混亂,我無法弄清楚你想要做什麼 – Jeroen 2010-08-04 17:27:28

回答

0

1 - 如果這Select *是真的在你的代碼,解決它。
2 - 您確實需要在Try/Catch中包含Rows.Add()Update()語句以及您正在做什麼。我通常會使用一個try/catch來完成整個事情,而且我當然不會像你所做的那樣吞下異常。

有人說,從你的問題很難明白你在做什麼。使上述兩個補丁後,我也想改變這一點:

explanations.Rows.Add(drexp1); 
explanations.Update(dsExp, "explanations"); 
explanations.Rows.Add(drexp2); 
explanations.Update(dsExp, "explanations"); 

這樣:

explanations.Rows.Add(drexp1); 
explanations.Rows.Add(drexp2); 
explanations.Update(dsExp, "explanations"); 

隨着(特別是)最後的兩個變化(只調用一次更新,而實際上處理您的例外,即使你只是重新投擲它們),你可以解決這個問題。至少,您應該獲得有關實際問題的更好信息,以便找到並更正它。