您好我試圖將數據庫中的數據存儲到變量,然後嘗試處理它。 但不知何故變量不會從數據庫中讀取任何數據並給出其初始值。 繼承人的代碼將數據從數據庫檢索到變量與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?
不使用。在c2 = 0之後,c3 = 0「int c1 = 0,c2 = 0,c3 = 0,c4 = 0,sum;」,你得到的錯誤是什麼... – Rahul 2013-04-08 12:39:31
你錯過了填充DataTable的代碼'。有關示例,請參閱[此處](https://www.google.com/search?q=dataadapter+datatable)。 – mbeckish 2013-04-08 12:39:38
thanx您的評論與填充()字段。桌子沒有填滿田地。 問題解決thanx @mbeckish – user2257581 2013-04-08 12:58:25