2010-08-04 3 views
0

데이터 집합에 두 번째 datarow를 추가 할 때 문제가 발생합니다. 내가 // 주석을 제거하면 나는 단지 1 행 대신 80SQLDataAdapter 및 업데이트 문제

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

답변

0

1의 추가 취득 - 그 Select * 그것을 수정, 코드에서 정말합니다.
2 - 시도/캐치와 수행중인 작업을 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"); 

(특히) 마지막 두 변경 (한 번만 업데이트를 호출하고 실제로 예외를 처리 , 비록 당신이 그들을 다시 던지고있다하더라도) 문제를 해결할 수 있습니다. 적어도 실제 문제 인 정보를 찾아서 수정해야합니다.

관련 문제