ASP.NET连接SQL Server的步骤

1、请创建一个简单的网站,有一个login.htm页面,为主页面(起始页面),两个普通页面WebForm1.aspx和WebForm2.aspx。要求:

1)WebForm1.aspx页面接收login.htm页面传过来的用户信息且做出判断,如果登陆成功则转到WebForm2.aspx页面,且在WebForm2.aspx页面中显示出用户的登陆信息。如果登陆失败则,转到login.htm页面。

2)在login.htm页面中至少应有两个,类型为text输入文本框,一个用于输入用户的姓名,name值为userName,一个用于输入用户的密码,name值为userPwd,有一个提交按钮,用于对用户输入信息的提交,还有一个重置按钮。

3)用数据库查询的方法,实现从查询数据库后,来判断该用户是否合法。

4)后台用C#语言进行编写。
在这里插入图片描述

login.htm页面代码(主页面(起始页面)):

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form method="post" action="WebForm1.aspx">
        <table>
            <tr>
                <td>
                    用户名:
                </td>
                <td colspan="2">
                    <input type="text" id="userName" name="userName" value="" />
                </td>
            </tr>
            <tr>
                <td>
                    密&nbsp;码:
                </td>
                <td colspan="2">
                    <input type="text" id="userPwd" name="userPwd" value="" />
                </td>

            </tr>
            <tr>
                <td></td>

                <td>
                    <input type="submit" name="submit" value="提交" />
                </td>
                <td>
                    <input type="reset" name="reset" value="重置" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

普通页面WebForm1.aspx后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using request_response.App_Code;
using System.Data.SqlClient;

namespace request_response
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
       	    //request页面间请求方式,post用 form
            string userName = this.Request.Form["userName"].ToString();
            string userPwd = this.Request.Form["userPwd"].ToString();
            //if (userName == "xudixin" && userPwd == "123")
            //{
                //Response.Write("您登陆成功!");
            //第一步,生成数据库连接
            SqlConnection sqlcon =DB.sqlcon();
            //第二步,打开连接 
            sqlcon.Open();
            //第三步,生成查询方法,填写查询命令
            SqlCommand sqlcom = new SqlCommand("select  count(*) from  login where userName='"+userName+"' and userPwd='"+userPwd+"' ", sqlcon);
            //第四步,执行查询命令
          int count=Convert.ToInt32(sqlcom.ExecuteScalar());//指向前的游标,返回的是一个对象
          if (count > 0)
          {
          	  //重定向,转到其它页面
              Response.Redirect("WebForm2.aspx?userName=" + userName + "&userPwd=" + userPwd);
          }
               
            //}
            else
            {
               // Response.Write("您登陆失败!");
                Response.Redirect("login.html");
            }

          sqlcon.Close();
        }
    }
}

普通页面WebForm2.aspx后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace request_response
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string userName = this.Request.QueryString["userName"].ToString();
            //get方式,用QueryString
            string userPwd = this.Request.QueryString["userPwd"].ToString();
            Response.Write("欢迎" + userName + "光临本网站,你的登陆密码为:" + userPwd);
        }
    }
}

用来连接数据库的DB类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

namespace request_response.App_Code
{
    public class DB
    {
    	//创建一个连接方法
        public static SqlConnection sqlcon()
        {
        //这种连接数据库的方法要把登陆数据库的方式改为SQL Server身份验证
        //server="服务器名称";database="要连接的数据库名称"
        //uid="登录名";pwd="密码"
            return new SqlConnection("server=.\\mysql2008;database=login;uid=sa;pwd=xu760309");
        }
    }
}

注意:要连接的数据库某张表中的用户名和密码数据就作为判断页面登陆是否成功的依据!

猜你喜欢

转载自blog.csdn.net/jianjianshini/article/details/105824991