visual studio 2005 和ms sql 2000 做一个用户登录的程序 visual studio 2005 和ms sql 2000 做一个用户登录的程序,只需要一个用户名和密码,一个登录的按钮的用户登陆程序,越详细越好,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 做一个登录界面,两个文本框,一个设置成密码输入框,用来让用户填写用户名和密码。再加上一个登录按钮,用于点击之后查找数据库是否存在用户名和对应密码的记录。string password="";//这里获取用户填写的密码string userName="";//这里获取用户填写的用户名bool success=false;//是否存在对应的用户名和密码string sql="select userName,password from 表名 where userName='"+userName+"' and password='"+password+"'";//实例化Connection对象 SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa"); //实例化Command对象 SqlCommand command = new SqlCommand(sql, connection); //打开Connection对象 connection.Open(); //得到DataReader的实例,注意使用了CommandBehavior这个参数,以便同时关闭Connection SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); //如果当前记录还有下一条记录,则循环不会终止 if(reader.Read()){success=true;//表示存在对应的用户名和密码} reader.Close(); //关闭reader对象 } asp.net夜话之七:ADO.NET介绍ADO.NET是对Microsoft ActiveX Data Objects (ADO)一个跨时代的改进,它提供了平台互用性和可伸缩的数据访问。由于传送的数据都是XML格式的,因此任何能够读取XML格式的应用程序都可以进行数据处理。事实上,接受数据的组件不一定要是ADO .NET组件,它可以是基于一个Microsoft Visual Studio的解决方案,也可以是任何运行在其它平台上的任何应用程序。以前做数据库访问的时候,需要一直与数据库保持连接,直到获取完所有满足需要的数据之后才会断开数据库连接,这种数据库访问方式称之为连接式数据访问技术。相比于以前的连接式数据访问技术,ADO.NET除了提供连接式数据访问技术之外,还提供了另一种断开式解决方案,那就是在内存中模拟一个数据库,也就是内存中的数据库。我们知道在实际的数据库技术中,每个数据库就是一个业务逻辑单元,一般来说这个数据库包含了实现一个应用软件或者一个网站所需要的全部数据。 加入你的用户表是如下结构(t_user)id;name;pwd里面有如下的数据:1 user1 pwd12 user2 pwd23 user3 pwd3这个时候你定义好连接之后,可以做如下的查询.SqlConnection conn=new SqlConnection(strconn);conn.Open();string strsql="select * form t_user where username='"+username+"' and '"+userpwd+"'";SqlCommand comm=new SqlCommand(conn);Dataset ds=comm.Execute(strsql);if (ds.Tables[0].Rows>0)return true;else return false; string name = TxtName.Text.Trim(); string pwd = TxtPwd.Text.Trim(); DataTable dt = ObjAdmin.GetLogin(name,pwd);//查询方法自己写 if (dt.Rows.Count>0) { Session["wo_Id"] = dt.Rows[0]["wo_id"].ToString(); Response.Write("<script>alert('登陆成功!');</script>"); } else { Response.Write("<script>alert('登陆失败,用户名或密码错误!');</script>"); } SqlConnection conn=new SqlConnection("server=.;database=aa;uid=sa;pwd="); conn.Open(); SqlCommand cmd=new SqlCommand("login",conn); cmd.CommandType=CommandType.StoredProcedure; cmd.Parameters.Add("@name",this.name.Text); cmd.Parameters.Add("@pwd",this.pwd.Text); int k=Convert.ToInt32(cmd.ExecuteScalar()); if(k==1) { Response.Redirect("default.aspx"); } else { Response.Write("<script>alert('您输入的用户名或者密码错误!')</script>"); } conn.Close();随手写了个 create database AA --创建数据库 go create table UserTable ( username varchar(20) primary key , --用户名 usepwd nvarchar(10) -- 密码 ) C# default .aspx.cs 代码: 写一个方法: bool Login(string Uname, string Upwd) { bool r = false; using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=sa;database=AA")) { conn.Open(); SqlCommand cmd = new SqlCommand("select count(*) from UserTable where username=@name and usepwd=@pwd", conn); cmd.Parameters.AddWithValue("@name", Uname); cmd.Parameters.AddWithValue("@pwd", Upwd); 呵呵这种方式 是参数的格式化输出 这种方法的好处是 可以有效的防止SQL注入 式攻击 int inta = Convert.ToInt32(cmd.ExecuteScalar().ToString()); ExecuteScalar()返回首行首列的值 if(inta > 0) { r=true ; } } return r; } protected void Button1_Click(object sender, EventArgs e) // 按钮的单击事件 { if (login(textbox1.text, textbox2.text)) { respone.write("成功!") } else { respone.write("失败!") } } protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { User_info inf = new User_info(); common com = new common(); SystemBLL systemBLL = new SystemBLL(); inf.Username = com.htmlencode(username.Text); inf.Pwd = com.GetSHA1(pwd.Text.ToString()); if (systemBLL.CheckUser(inf) > 0) { if (inf.Pwd == systemBLL.CheckPwd(inf)) { FormsAuthentication.SetAuthCookie(inf.Username, false); Response.Redirect("Default.aspx"); } else { com.alert("密码不正确", "-1"); } } else { com.alert("该用户名不存在", "-1"); } }public int CheckUser(string username) { SqlHelper getdata = new SqlHelper(); string sql = "select * from users where username='" + username + "'"; return getdata.ReturnRowCount(sql); }public string CheckPwd(string username) { string sql = "select pwd from users where username='" + username + "'"; return ShowExcScalar(sql); } create database AA --创建数据库 go create table UserTable ( username varchar(20) primary key , --用户名 usepwd nvarchar(10) -- 密码 ) C# code C# default .aspx.cs 代码: 写一个方法: bool Login(string Uname, string Upwd) 登陆验证的方法 { bool r = false; using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=sa;database=AA")) { conn.Open(); SqlCommand cmd = new SqlCommand("select count(*) from UserTable where username=@name and usepwd=@pwd", conn); cmd.Parameters.AddWithValue("@name", Uname); cmd.Parameters.AddWithValue("@pwd", Upwd); 呵呵这种方式 是参数的格式化输出 这种方法的好处是 可以有效的防止SQL注入 式攻击 int inta = Convert.ToInt32(cmd.ExecuteScalar().ToString()); ExecuteScalar()返回首行首列的值 if(inta > 0) { r=true ; } } return r; } protected void Button1_Click(object sender, EventArgs e) // 按钮的单击事件 { if (login(textbox1.text, textbox2.text)) 登陆方法的调用 { respone.write("成功!") } else { respone.write("失败!") } } protected void ButtonLogin_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["DBConnectionString"]); //创建连接对象 SqlCommand cmd = new SqlCommand("select * from Users where userId='" + txtUserName.Text + "'", conn); //创建查询用户名是否存在数据对象 try { conn.Open(); //打开连接 SqlDataReader sdr = cmd.ExecuteReader(); if (sdr.Read()) //如果用户名输入正确 { if (sdr["userPwd"].ToString() == txtPassword.Text)//密码正确 { conn.Close(); Session["userID"] = txtUserName.Text.Trim();//存储用户名 Response.Redirect("AddressList.aspx"); //进入系统 } else { Response.Write("<script language=javascript>alert('您输入的密码错误!')</script>"); } } else { Response.Write("<script language=javascript>alert('您输入的用户名错误或该用户名不存在!')</script>"); } } catch (System.Exception ee) { Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>"); } finally { conn.Close(); } }写个简单的 到网上搜一个开源的简单的项目看看最直接。http://www.51aspx.com/ 很多开源项目 在线等!一个简单的出库问题 页面布局 关于asp伪静态最好的介绍 我做的AJAX上传文件功能,在本机正常使用,但是上传到服务器后就不再起作用,请高手帮我看看!!!!!!! 哪位大哥知道js 怎么给freetextbox赋值啊 用FindControl为什么找不到用New创建的控件? 光标离开TextBox时,怎么执行一个函数呢? 关于remove的问题 问一个编程思路 dao的问题 Asp.net中GridView的数据源是DataTable,能否通过CommandField更新DataTable中的数据 gridview 显示隐藏层的问题
再加上一个登录按钮,用于点击之后查找数据库是否存在用户名和对应密码的记录。string password="";//这里获取用户填写的密码
string userName="";//这里获取用户填写的用户名
bool success=false;//是否存在对应的用户名和密码
string sql="select userName,password from 表名 where userName='"+userName+"' and password='"+password+"'";
//实例化Connection对象
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa");
//实例化Command对象
SqlCommand command = new SqlCommand(sql, connection);
//打开Connection对象
connection.Open();
//得到DataReader的实例,注意使用了CommandBehavior这个参数,以便同时关闭Connection
SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
//如果当前记录还有下一条记录,则循环不会终止
if(reader.Read())
{
success=true;//表示存在对应的用户名和密码
}
reader.Close(); //关闭reader对象
}
ADO.NET是对Microsoft ActiveX Data Objects (ADO)一个跨时代的改进,它提供了平台互用性和可伸缩的数据访问。由于传送的数据都是XML格式的,因此任何能够读取XML格式的应用程序都可以进行数据处理。事实上,接受数据的组件不一定要是ADO .NET组件,它可以是基于一个Microsoft Visual Studio的解决方案,也可以是任何运行在其它平台上的任何应用程序。以前做数据库访问的时候,需要一直与数据库保持连接,直到获取完所有满足需要的数据之后才会断开数据库连接,这种数据库访问方式称之为连接式数据访问技术。相比于以前的连接式数据访问技术,ADO.NET除了提供连接式数据访问技术之外,还提供了另一种断开式解决方案,那就是在内存中模拟一个数据库,也就是内存中的数据库。我们知道在实际的数据库技术中,每个数据库就是一个业务逻辑单元,一般来说这个数据库包含了实现一个应用软件或者一个网站所需要的全部数据。
id;name;pwd
里面有如下的数据:
1 user1 pwd1
2 user2 pwd2
3 user3 pwd3这个时候你定义好连接之后,可以做如下的查询.
SqlConnection conn=new SqlConnection(strconn);
conn.Open();
string strsql="select * form t_user where username='"+username+"' and '"+userpwd+"'";
SqlCommand comm=new SqlCommand(conn);
Dataset ds=comm.Execute(strsql);
if (ds.Tables[0].Rows>0)
return true;
else return false;
string pwd = TxtPwd.Text.Trim();
DataTable dt = ObjAdmin.GetLogin(name,pwd);//查询方法自己写
if (dt.Rows.Count>0)
{
Session["wo_Id"] = dt.Rows[0]["wo_id"].ToString();
Response.Write("<script>alert('登陆成功!');</script>");
}
else
{
Response.Write("<script>alert('登陆失败,用户名或密码错误!');</script>");
}
SqlConnection conn=new SqlConnection("server=.;database=aa;uid=sa;pwd=");
conn.Open();
SqlCommand cmd=new SqlCommand("login",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@name",this.name.Text);
cmd.Parameters.Add("@pwd",this.pwd.Text);
int k=Convert.ToInt32(cmd.ExecuteScalar());
if(k==1)
{
Response.Redirect("default.aspx");
}
else
{
Response.Write("<script>alert('您输入的用户名或者密码错误!')</script>");
}
conn.Close();随手写了个
go
create table UserTable
(
username varchar(20) primary key , --用户名
usepwd nvarchar(10) -- 密码
)
C# default .aspx.cs 代码:
写一个方法: bool Login(string Uname, string Upwd)
{
bool r = false;
using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=sa;database=AA"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from UserTable where username=@name and usepwd=@pwd", conn);
cmd.Parameters.AddWithValue("@name", Uname);
cmd.Parameters.AddWithValue("@pwd", Upwd); 呵呵这种方式 是参数的格式化输出 这种方法的好处是 可以有效的防止SQL注入 式攻击
int inta = Convert.ToInt32(cmd.ExecuteScalar().ToString()); ExecuteScalar()返回首行首列的值
if(inta > 0)
{
r=true ;
}
} return r;
}
protected void Button1_Click(object sender, EventArgs e) // 按钮的单击事件
{
if (login(textbox1.text, textbox2.text))
{
respone.write("成功!")
}
else
{
respone.write("失败!") }
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
User_info inf = new User_info();
common com = new common();
SystemBLL systemBLL = new SystemBLL();
inf.Username = com.htmlencode(username.Text);
inf.Pwd = com.GetSHA1(pwd.Text.ToString());
if (systemBLL.CheckUser(inf) > 0)
{
if (inf.Pwd == systemBLL.CheckPwd(inf))
{
FormsAuthentication.SetAuthCookie(inf.Username, false);
Response.Redirect("Default.aspx");
}
else
{
com.alert("密码不正确", "-1");
}
}
else
{
com.alert("该用户名不存在", "-1");
}
}
public int CheckUser(string username)
{
SqlHelper getdata = new SqlHelper();
string sql = "select * from users where username='" + username + "'";
return getdata.ReturnRowCount(sql);
}
public string CheckPwd(string username)
{
string sql = "select pwd from users where username='" + username + "'";
return ShowExcScalar(sql);
}
go
create table UserTable
(
username varchar(20) primary key , --用户名
usepwd nvarchar(10) -- 密码
)
C# code
C# default .aspx.cs 代码:
写一个方法: bool Login(string Uname, string Upwd) 登陆验证的方法
{
bool r = false;
using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=sa;database=AA"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from UserTable where username=@name and usepwd=@pwd", conn);
cmd.Parameters.AddWithValue("@name", Uname);
cmd.Parameters.AddWithValue("@pwd", Upwd); 呵呵这种方式 是参数的格式化输出 这种方法的好处是 可以有效的防止SQL注入 式攻击
int inta = Convert.ToInt32(cmd.ExecuteScalar().ToString()); ExecuteScalar()返回首行首列的值
if(inta > 0)
{
r=true ;
}
} return r;
}
protected void Button1_Click(object sender, EventArgs e) // 按钮的单击事件
{
if (login(textbox1.text, textbox2.text)) 登陆方法的调用
{
respone.write("成功!")
}
else
{
respone.write("失败!") }
}
protected void ButtonLogin_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["DBConnectionString"]); //创建连接对象
SqlCommand cmd = new SqlCommand("select * from Users where userId='" + txtUserName.Text + "'", conn); //创建查询用户名是否存在数据对象
try
{
conn.Open(); //打开连接
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read()) //如果用户名输入正确
{
if (sdr["userPwd"].ToString() == txtPassword.Text)//密码正确
{
conn.Close();
Session["userID"] = txtUserName.Text.Trim();//存储用户名
Response.Redirect("AddressList.aspx"); //进入系统
}
else
{
Response.Write("<script language=javascript>alert('您输入的密码错误!')</script>");
}
}
else
{
Response.Write("<script language=javascript>alert('您输入的用户名错误或该用户名不存在!')</script>");
}
}
catch (System.Exception ee)
{
Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>");
}
finally
{
conn.Close();
}
}写个简单的
http://www.51aspx.com/ 很多开源项目