0

您好我正在尝试将数据库中的数据存储到变量中,然后尝试处理它。但不知何故,该变量不会从数据库中读取任何数据并给出其初始值。这是代码

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]);
        }
    }    

任何人都可以帮助解决这个问题。我几天前使用了相同的代码。它工作正常。但现在我不知道它有什么问题。我需要尽快解决,有人可以帮忙吗?

4

2 回答 2

0

您需要在使用前填写数据表

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

其余的适配器依此类推。

但是,让我展示一种不同的方法:

string sqlText = "Select Amount from addpro where SSN=@ssn";
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 注入攻击。尽管在您的代码中有点不可能,但在任何地方使用都是一个好习惯。

有了上面的代码,很容易构建一个接收组合框控件的通用方法,应用所需的逻辑并返回整数值

 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);
}
于 2013-04-08T12:40:17.267 回答
0

使用 fill() 方法先用 dataadapter 填充数据表,然后尝试访问数据行

例如:d.fill(dt);

于 2013-04-08T12:41:34.803 回答