【问题标题】:Error on accessing Rows property of DataSet访问 DataSet 的 Rows 属性时出错
【发布时间】:2013-02-20 12:10:06
【问题描述】:

我需要创建一个简单的登录页面并在 Rows 上收到错误...

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("server=AMR\\DEV1;UID=po;PWD=12W; database=POM;");
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from budget where id='" + TextBox1.Text + "' and amount='" + TextBox2.Text + "'", con);
    DataSet ds = new DataSet();
    ds.Clear();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(ds);

    if (ds.Rows.Count > 0) {
        Session["user"] = TextBox1.Text;
        Response.Redirect("Default2.aspx");
    } else {
        TextBox1.Text = "";
        TextBox2.Text = "";
        Label1.Text = "Invalid Login Details!";
    }
}

【问题讨论】:

  • 能否格式化您的代码?看不懂,贴出编译器给出的什么错误才能更好的帮助你..
  • 您遇到什么错误?在哪一行?你试过什么?

标签: c# datatable dataset


【解决方案1】:

DataSet 没有 Rows 属性。 DataTable 确实如此。将代码中的 ds.Rows 替换为 ds.Tables[0].Rows

还有一点:使用Parameterized SqlComamnd。您的代码容易受到 SQL 注入攻击

【讨论】:

    【解决方案2】:

    你的代码有很多问题。我在需要的地方改了

    protected void Button1_Click(object sender, EventArgs e)
    {
     SqlConnection con = new SqlConnection("server=AMR\\DEV1;UID=po;PWD=12W; database=POM;");
     con.Open();
     SqlCommand cmd = new SqlCommand("select * from budget where id=@id and amount=amount", con);
     cmd.Parameters.AddWithValue("@id",TextBox1.Text);
     cmd.Parameters.AddWithValue("@amount",TextBox2.Text);
     DataSet ds = new DataSet();
     ds.Clear();
     SqlDataAdapter da = new SqlDataAdapter(cmd);
     da.Fill(ds);
    
     if (ds.Tables[0].Rows.Count > 0) 
     {
        Session["user"] = ds.Tables[0].Rows[0][0].ToString();
        Response.Redirect("Default2.aspx");
     }
      else {
        TextBox1.Text = "";
        TextBox2.Text = "";
        Label1.Text = "Invalid Login Details!";
     }
    }
    

    按照 Andrey Gordeev 的说法,了解 DataSetDataTableParameterized SqlComamnd

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 2021-05-21
      • 2022-12-13
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      相关资源
      最近更新 更多