为什么我的下列的代码不能进入数据库读取呢?
高手帮帮忙...谢谢!
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=localhost;database=oooooool;uid=sa;pwd=sa;");
con.Open();
SqlCommand cmd = new SqlCommand("select count(*) from login where txtName='"+txtName +"'and txtpwd='"+txtPwd +"'", con);
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count>0)
{
Response.Redirect("amin.aspx");
}
else
{
Response.Redirect("error.aspx");
}
con.Close();
}
高手帮帮忙...谢谢!
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=localhost;database=oooooool;uid=sa;pwd=sa;");
con.Open();
SqlCommand cmd = new SqlCommand("select count(*) from login where txtName='"+txtName +"'and txtpwd='"+txtPwd +"'", con);
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count>0)
{
Response.Redirect("amin.aspx");
}
else
{
Response.Redirect("error.aspx");
}
con.Close();
}
改成int count = cmd.ExecuteNonquery()
然后看看
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=localhost;database=oooooool;uid=sa;pwd=sa;");
con.Open();
SqlCommand cmd = new SqlCommand("select count(*) from login where txtName='"+txtName +"' and txtpwd='"+txtPwd +"'", con);
DataSet ds =new DataSet();
int count = cmd.Fill(ds));
if (count>0)
{
Response.Redirect("amin.aspx");
}
else
{
Response.Redirect("error.aspx");
}
con.Close();
}
或者
int count = cmd.ExecuteScalar() as int;
试试看
{
try
{
cmd.Parameters.Clear();
cmd.CommandType=CommandType.Text;
cmd.CommandText=sql;
if(cmd.Connection.State!=ConnectionState.Open)
cmd.Connection.Open();
DataTable dt=new DataTable();
SqlDataAdapter da=new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;
}
catch(Exception ex)
{
ex.ToString();
}
finally
{
if(cmd!=null)
if(cmd.Connection.State!=ConnectionState.Closed)
cmd.Connection.Close();
}
return null;
}DataTable dt=SelectTextCommand("select count(*) from login where txtName='"+txtName +"'and txtpwd='"+txtPwd +"'");
if(dt!=null)
count=Convert.ToInt32(dt.Rows[0][0]);
返回的那个count值是多少???
你把那个sql语句直接去数据库里执行一下,看有没有结果
string str =(string)cmd.ExecuteScalar();
if (str != null)
{
Response.Redirect("amin.aspx");
}