之前写的不够严谨,贴上新的
登录:
string DriUserName1 = DriUserName.Text.Trim(); string DriPassWord1 = DriPassWord.Text.Trim(); SqlConnection conn = new SqlConnection();//uid=;pwd=; 创建一个连接数据库的对象 conn.ConnectionString = @"server=ACER\SQLEXPRESS;Integrated Security=true;Initial Catalog=GradeManage;database=vs_test"; //用这行登录访问所要的数据库 string sqlString = "select * from [vdriver] where DriUserName='" + DriUserName1 + "' and DriPassWord='" + DriPassWord1 + "'"; //在数据库里面执行的语句 string sqlCheck = "select count(DriUserName) from vdriver where DriUserName='" + DriUserName1 + "'"; conn.Open(); SqlCommand check = new SqlCommand(sqlCheck, conn); if (Convert.ToInt16(check.ExecuteScalar()) == 0) { Response.Write("<script>alert('用户名不存在!');</script>"); } else { SqlDataAdapter sda = new SqlDataAdapter(sqlString, conn);//创建DataAdapter数据适配器实例 DataSet ds = new DataSet();//创建DataSet实例,里面可以放很多的table sda.Fill(ds);//使用DataAdapter的Fill方法(填充),调用SELECT命令 if (ds.Tables[0].Rows.Count == 1)//ds里面的第一个表存在这一行数据 匹配成功 { // Response.Write("<script language='javascript'>alert('登录成功!');history.back();</script>"); //Response.Write("<script language='javascript'>alert('登录成功!');history.back();</script>;window.location.href='学生界面.aspx'</script>"); Response.Write("<script>location.href='查看订单.aspx';</script>"); //alert('登录成功!'); // TextBox1.Text = "登录成功"; } else { Response.Write("<script language='javascript'>alert('密码错误!');location.href='初始界面.aspx';</script>"); } } }
注册:
string NewDriUserName1 = Request.Form["NewDriUserName"];//获取窗体内的变量 string NewDriPassWord1 = Request.Form["NewDriPassWord"];//获取窗体内的变量 //string userName = Request.Form["textRegisterName"];//获得注册帐号 // string userRealName = Request.Form["real_name"];//真实名 SqlConnection conn = new SqlConnection();//创建一个连接数据库的对象 conn.ConnectionString = @"server=ACER\SQLEXPRESS;Integrated Security=true;Initial Catalog=GradeManage;database=vs_test"; conn.Open(); string sqlCheck = "select count(DriUserName) from vdriver where DriUserName='" + NewDriUserName1 + "'"; string sqlString = "Insert into vdriver(DriUserName,DriPassWord) values ('" + NewDriUserName1 + "','" + NewDriPassWord1 + "')"; // conn.Open(); // 打开所连接的数据库 SqlCommand check = new SqlCommand(sqlCheck, conn); //object CekNum = check.ExecuteScalar(); if (Convert.ToInt16(check.ExecuteScalar())>0) { Response.Write("<script>alert('注册失败,该用户名数据库已存在!');</script>"); } else { SqlCommand cmd = new SqlCommand(sqlString, conn); cmd.ExecuteNonQuery(); sqlString = "select* from vdriver where DriUserName = '" + NewDriUserName1 + "'"; SqlDataAdapter sda = new SqlDataAdapter(sqlString, conn);//适配器放数据集的 DataSet ds = new DataSet();//数据集 sda.Fill(ds, "Table");//表 if (ds.Tables[0].Rows.Count == 1) { // Response.Write("<script language='javascript'>alert('登录成功!');history.back();</script>"); //Response.Write("<script language='javascript'>alert('登录成功!');history.back();</script>;window.location.href='界面.aspx'</script>"); Response.Write("<script>alert('注册成功!');location.href='初始界面.aspx';</script>"); } else { Response.Write("<script language='javascript'>alert('注册失败!');history.back();</script>"); } }
原文地址:http://blog.csdn.net/hhooong/article/details/45826417