vs2005+ sql2000的连接

来源:百度知道 编辑:UC知道 时间:2024/05/24 17:15:31
SqlConnection con = new SqlConnection ("server = .; database=table;uid = sa ;pwd = ;");
con.Open();
SqlCommand cmd = new SqlCommand ("select count (*) from table where UserName ='"+UserName+"' and UserPwd ='"+UserPwd +"'",con );
int count = Convert.ToInt32(cmd.ExecuteScalar ());
if (count > 0)
{
Response.Redirect("main.aspx");

}
else
{
Response.Redirect("loginlife.aspx");
}
帮我解释一下上面代码的具体意思,有错的帮我修改下...

上面这段代码就是做登陆用的

//着句是连接数据库的数据库名字叫table
SqlConnection con = new SqlConnection ("server = .; database=table;uid = sa ;pwd = ;");
//打开数据连接
con.Open();
//创建一个执行的COMMAND其中SQL语句的意思是根据用户填写的用户名字和密码作为条件去数据表中查询是否有该数据
SqlCommand cmd = new SqlCommand ("select count (*) from table where UserName ='"+UserName+"' and UserPwd ='"+UserPwd +"'",con );
//执行SQL语句并且接受了返回的值
int count = Convert.ToInt32(cmd.ExecuteScalar ());
//判断如果返回的值比0大证明有这个一个用户存在
if (count > 0)
{
//重定向到显示页面
Response.Redirect("main.aspx");

}
//反之情况就是没有找到这么一个用户
else
{
//重定向到登陆界面
Response.Redirect("loginlife.aspx");
}

不过好象这段代码有问题 数据库的名字和表的名字相同了理论在编译的时候会出现错误