日期:2014-05-19  浏览次数:20459 次

急求!(登录注册)ASP页面的C#代码!
我在编程中遇到以下问题:
主页面有两个按钮分别是:注册和登陆      
对应的登录按钮和注册按钮我已连接了Access数据库,连接代码如下:

      //   建立数据库连接对象
      OleDbConnection   conn   =   new   OleDbConnection();
      conn.ConnectionString   =   "Provider=Microsoft.Jet.OLEDB.4.0; "  
        +   "Data   Source= "   +   Server.MapPath( "Registration.mdb ");
      //OleDbConnection:该对象管理与可通过OLEDB访问的任何数据存储区的连接,创建对象conn,连接数据库Registration.mdb
      conn.Open();//打开连接conn

      //   关闭数据库连接
      conn.Close();

在登录按钮中,我希望可以验证用户输入的用户名和密码,如果用户名不存在则提示“不存在”,如果密码不正确则提示“密码不正确”,否则进入相应的页面。

在注册页面中,我希望可以验证用户输入的用户名在Access数据库中是否已存在,如果存在则提示“此用户名已存在”,否则提示“此用户名未被使用,可以注册”,并进入相应的页面。
我试了很多方法都不能实现要求!

------解决方案--------------------
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection myconn=db.createconnection();
// string sql = string.Format( "Provider={0};Data Source={1} ", ConfigurationSettings.AppSettings[ "provider "], Server.MapPath(ConfigurationSettings.AppSettings[ "datasource "]));
// myconn.ConnectionString = sql;
myconn.Open(); //打开数据库连接
string adminsql = "select * from admin where admin_name= ' " + text1.Text.Replace( " ' ", " ' ' ") + " 'and admin_pwd= ' " + text2.Text.Replace( " ' ", " ' ' ") + " ' ";
OleDbCommand comm = new OleDbCommand(adminsql, myconn); //存储过程
OleDbDataReader reader = comm.ExecuteReader();
if (reader.Read())
if(Request[ "vali_box "]==Session[ "ValidateCode "].ToString())
{
Session[ "admin "] = text1;
Session[ "pwd "] = text2;
// Response.Write( " <script> alert( '全部正确 ') </script> ");
Response.Redirect( "Default.aspx ");

}
else
{
text1.Text= " ";
text2.Text= " ";
Response.Write( " <script> alert( '验证码错误 ') </script> ");
}

else
{
text1.Text = " ";
text2.Text = " ";
Response.Write( " <script> alert( '密码错误 ') </script> ");
}

reader.Close();
myconn.Close();
}

}

这是我的,有验证码.你自己改改.