protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string strsql = "select * from 用户编码表 where 用户编码='000' and 密码='111'"; LoginCheck p = new LoginCheck();
DataTable dt = p.UserLoginCheck(strsql);
if (dt == null)
{
Response.Write("<script>alert('无此用户');</script>");
return;
}
else
{
if (dt .Rows.Count == 0)
{
Response.Write("<script>alert('无记录');</script>");
return;
}
else
{
Session["username"] = dt .Rows[0]["用户名称"];
Response.Redirect("frame.aspx");
}
}
}
public DataTable UserLoginCheck(string strsql)
{
SqlConnection con = DB.Connection();
SqlCommand cmd = null;
SqlDataAdapter DA = null;
DataSet DS = new DataSet();
DataTable dt = new DataTable(); try
{
DA = new SqlDataAdapter(strsql,con);
DA.Fill(DS);
dt = DS.Tables[0];
}
catch { }
finally
{
if (con != null)
{
con.Close();
con.Dispose();
}
if (cmd != null)
{
cmd.Dispose();
}
}
return dt; }
总是显示"无记录",可数据库里有值.
{
string strsql = "select * from 用户编码表 where 用户编码='000' and 密码='111'"; LoginCheck p = new LoginCheck();
DataTable dt = p.UserLoginCheck(strsql);
if (dt == null)
{
Response.Write("<script>alert('无此用户');</script>");
return;
}
else
{
if (dt .Rows.Count == 0)
{
Response.Write("<script>alert('无记录');</script>");
return;
}
else
{
Session["username"] = dt .Rows[0]["用户名称"];
Response.Redirect("frame.aspx");
}
}
}
public DataTable UserLoginCheck(string strsql)
{
SqlConnection con = DB.Connection();
SqlCommand cmd = null;
SqlDataAdapter DA = null;
DataSet DS = new DataSet();
DataTable dt = new DataTable(); try
{
DA = new SqlDataAdapter(strsql,con);
DA.Fill(DS);
dt = DS.Tables[0];
}
catch { }
finally
{
if (con != null)
{
con.Close();
con.Dispose();
}
if (cmd != null)
{
cmd.Dispose();
}
}
return dt; }
总是显示"无记录",可数据库里有值.
查个用户吗
不要这么复杂把
command.executescalar()
就可以了
返回执行行数
不要dataset,dataadapter,浪费了
设断点看看的运行情况把
另外看看你的sql语句是否可以执行呢,在查询分析器里面
try
{
con.Open()
cmd = new SqlCommand(strsql);
command.CommandType = CommandType.Text;
DA = new SqlDataAdapter();
DA.SelectCommand = cmd
DataSet DS = new DataSet("Users");//可以不写
DA.Fill(DS);
dt = DS.Tables[0];
con.Close(); }
try
{
con.Open()
cmd = new SqlCommand(strsql,con);
command.CommandType = CommandType.Text;
DA = new SqlDataAdapter();
DA.SelectCommand = cmd
DataSet DS = new DataSet("Users");//可以不写
DA.Fill(DS);
dt = DS.Tables[0];
con.Close(); }