2013-04-08 83 views
0

您好我試圖將數據庫中的數據存儲到變量,然後嘗試處理它。 但不知何故變量不會從數據庫中讀取任何數據並給出其初始值。 繼承人的代碼將數據從數據庫檢索到變量與C#

int c1=0,c2=0.c3=0,c4=0,sum; 

if (rbFour.Checked == true) 
     { 
      proce = cmb1.Text + "," + cmb2.Text + "," + cmb3.Text + "," + cmb4.Text; 
      SqlDataAdapter d = new SqlDataAdapter("Select Amount from addpro where SSN=" + cmb1.ValueMember, Program.con); 
      DataTable t = new DataTable(); 
      foreach (DataRow row in t.Rows) 
      { 

      c1 = Convert.ToInt32(row[0]); 
     } 
     SqlDataAdapter qd = new SqlDataAdapter("Select Amount from addpro where SSN=" + cmb2.ValueMember, Program.con); 
     DataTable qt = new DataTable(); 
     foreach (DataRow row in qt.Rows) 
     { 

      c2 = Convert.ToInt32(row[0]); 
     } 
     SqlDataAdapter wd = new SqlDataAdapter("Select Amount from addpro where SSN=" + cmb3.ValueMember, Program.con); 
     DataTable wt = new DataTable(); 
     foreach (DataRow row in wt.Rows) 
     { 

      c3 = Convert.ToInt32(row[0]); 
     } 
     SqlDataAdapter ed = new SqlDataAdapter("Select Amount from addpro where SSN=" + cmb4.ValueMember, Program.con); 
     DataTable et = new DataTable(); 
     foreach (DataRow row in et.Rows) 
     { 

      c4 = Convert.ToInt32(row[0]); 
     } 
    } 
    else if (rbThree.Checked == true) 
    { 
     proce = cmb1.Text + "," + cmb2.Text + "," + cmb3.Text; 
     SqlDataAdapter d = new SqlDataAdapter("Select Amount from addpro where SSN=" + cmb1.ValueMember, Program.con); 
     DataTable t = new DataTable(); 
     foreach (DataRow row in t.Rows) 
     { 

      c1 = Convert.ToInt32(row[0]); 
     } 
     SqlDataAdapter qd = new SqlDataAdapter("Select Amount from addpro where SSN=" + cmb2.ValueMember, Program.con); 
     DataTable qt = new DataTable(); 
     foreach (DataRow row in qt.Rows) 
     { 

      c2 = Convert.ToInt32(row[0]); 
     } 
     SqlDataAdapter wd = new SqlDataAdapter("Select Amount from addpro where SSN=" + cmb3.ValueMember, Program.con); 
     DataTable wt = new DataTable(); 
     foreach (DataRow row in wt.Rows) 
     { 

      c3 = Convert.ToInt32(row[0]); 
     } 
    } 
    else if (rbTwo.Checked == true) 
    { 
     proce = cmb1.Text + "," + cmb2.Text; 
     SqlDataAdapter d = new SqlDataAdapter("Select Amount from addpro where SSN=" + cmb1.ValueMember, Program.con); 
     DataTable t = new DataTable(); 

     foreach (DataRow row in t.Rows) 
     { 

      c1 = Convert.ToInt32(row[0]); 
     } 
     SqlDataAdapter qd = new SqlDataAdapter("Select Amount from addpro where SSN=" + cmb2.ValueMember, Program.con); 
     DataTable qt = new DataTable(); 
     qd.Fill(qt); 
     foreach (DataRow row in qt.Rows) 
     { 

      c2 = Convert.ToInt32(row[0]); 
     } 

    } 
    else 
    { 
     proce = cmb1.Text; 
     SqlDataAdapter d = new SqlDataAdapter("Select Amount from addpro where SSN=" + cmb1.ValueMember, Program.con); 
     DataTable t = new DataTable(); 
     foreach (DataRow row in t.Rows) 
     { 

      c1 = Convert.ToInt32(row[0]); 
     } 
    }  

誰能幫助this.i使用相同的代碼幾天ago.it fine.but工作,現在我不知道什麼不妥的地方。 我需要解決方案儘快任何人都可以幫助PLZ?

+0

不使用。在c2 = 0之後,c3 = 0「int c1 = 0,c2 = 0,c3 = 0,c4 = 0,sum;」,你得到的錯誤是什麼... – Rahul 2013-04-08 12:39:31

+0

你錯過了填充DataTable的代碼'。有關示例,請參閱[此處](https://www.google.com/search?q=dataadapter+datatable)。 – mbeckish 2013-04-08 12:39:38

+0

thanx您的評論與填充()字段。桌子沒有填滿田地。 問題解決thanx @mbeckish – user2257581 2013-04-08 12:58:25

回答

0

你使用它

SqlDataAdapter d = new SqlDataAdapter("...", con); 
    DataTable t = new DataTable(); 
    d.Fill(t); // Here 

等爲剩餘的適配器之前需要Fill的DataTable。

然而,讓我告訴一個不同的方法:

string sqlText = "Select Amount from addpro where [email protected]"; 
SqlCommand cmd = new SqlCommand(sqlText, Program.con); 
cmd.Parameters.AddWithValue("@ssn", cmb1.ValueMember); 
object result = cmd.ExecuteScalar(); 
if(result != null) 
    c1 = Convert.ToInt32(result); 

您在返回單行的單個值(如果找到)。這是SqlCommand的ExecuteScalar方法提供的使用場景。沒有必要創建一個SqlAdapter,一個DataTable並用一行/值填充它。

此外,我已經刪除了您的字符串連接,並使用參數化查詢來避免Sql注入攻擊。儘管在你的代碼中有點不太可能,但在任何地方使用都是一個好習慣。

與上面的代碼來代替它是很容易建立接收ComboBox控件的常用方法,應用所需的邏輯,並返回整數值

private int GetComboValue(ComboBox cbo) 
{ 
     // all the code above replacing cmb1 with cbo and c1 with ssnNumber 
     ..... 
     return ssnNumber; 
} 

,現在你可以使用

if (rbFour.Checked == true) 
{ 
    c1 = GetComboValue(cmb1); 
    c2 = GetComboValue(cmb2); 
    c3 = GetComboValue(cmb3); 
    c4 = GetComboValue(cmb4); 
} 
+0

問題解決。 thanx的額外代碼。我會在我的申請中使用它 thant a tonne – user2257581 2013-04-08 12:59:57

0

使用fill()方法首先用dataadapter填充數據表,然後嘗試訪問數據行

ex:d.fill(dt);