日期:2014-05-17  浏览次数:20477 次

asp.net连接SQL实现用户登陆
本帖最后由 Chriswu9010 于 2013-01-01 11:27:27 编辑
    protected void btn_login_Click1(object sender, EventArgs e)
    {

        try
        {
            string username = txt_username.Text.Trim().ToString();
            string password = txt_password.Text.Trim().ToString();
            SqlConnection conn = new SqlConnection();

            conn.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=tongxunlu;Integrated Security=True";
            conn.Open();

            string sql = "select * from yonghu where name='" + username + "' and password='" + password + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            int n = Convert.ToInt32(cmd.ExecuteScalar());
            Response.Write(n);

            //int n = (int)cmd.ExecuteScalar();
            //dataAdapter.Fill(result);

            if (n > 0)//判断是否有匹配的用户名和密码
            {

                Response.Redirect("main.aspx");
            }
            else
            {
                Response.Write("用户名或密码错误");
            }
            conn.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
   }

以上代码想是实现asp.net连接SQL实现用户如下登陆界面:

但是,总是提示用户名和密码不正确,我SQL中的yonghu表中有用户a和密码a。通过单步调试,发现:

username和password根本就没放进去值,只想请问高人,到底哪里出现错误?

还有,我自己做好的一个静态网页用浏览器打开后,地址栏是:
file:///D:/nanchangdaxue/%E5%8D%97%E6%98%8C%E5%A4%A7%E5%AD%A6.html
以上代码用户验证通过是跳转到main.aspx。如果我想跳转到以上静态网页,怎么实现?麻烦了
asp.net sql

------解决方案--------------------
引用:
本帖最后由 Chriswu9010