C# 访问数据集的Rows属性时出错
我需要创建一个简单的登录页面,并得到一个关于行的错误C# 访问数据集的Rows属性时出错,c#,datatable,dataset,C#,Datatable,Dataset,我需要创建一个简单的登录页面,并得到一个关于行的错误 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 budg
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!";
}
}
没有财产。做将代码中的ds.Rows
替换为ds.Tables[0]。Rows
还有一点:使用。您的代码容易受到SQL注入攻击您的代码中存在许多问题。我在需要的地方改了
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!";
}
}
学习,正如安德烈·戈迪夫所说 你能把你的代码格式化吗?这是不可理解的,请公布编译器给出的错误,以便更好地帮助您。您遇到了什么错误?在哪条线上?你试过什么?