当前位置:网站首页>ASP.NET连接SQL Server的步骤
ASP.NET连接SQL Server的步骤
2022-08-10 05:35:00 【三和尚】
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>
密 码:
</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");
}
}
}
注意:要连接的数据库某张表中的用户名和密码数据就作为判断页面登陆是否成功的依据!
边栏推荐
- LeetCode 94.二叉树的中序遍历(简单)
- PyTorch之CV
- 【目标检测】相关指标的引入与解析
- STM32F407ZG PWM
- Explain the principle of MySql index in detail
- Day1 微信小程序-小程序代码的构成
- pytorch-09. Multi-classification problem
- 以STM32F103C6T6为例通过配置CubeMX实现EXIT外部中断
- 51单片机营养液自动配置搅拌系统TDS浓度采集自动加水加营养液
- LeetCode 1351. Counting Negative Numbers in Ordered Matrices (Simple)
猜你喜欢
随机推荐
自定义View的流程总结学习
LeetCode 面试题17.14 最小k个数(中等)
栈和队列
The Principle of Union Search and API Design
基于 .NET Core MVC 的权限管理系统
LeetCode 162.寻找峰值(中等)
51单片机智能远程遥控温控PWM电风扇系统红外遥控温度速度定时关机
深度学习TensorFlow入门环境配置
pytorch-06. Logistic regression
Pytorch - 07. Multidimensional characteristics of input processing
I don't like my code
LeetCode 1894.找到需要补充粉笔的学生编号
51单片机手动自动智能窗户窗帘控制系统手动自动定时
LeetCode 162. Finding Peaks (Moderate)
系统架构和问题定位
LeetCode 剑指offer 21.调整数组顺序使奇数位于偶数前面(简单)
解决错误 Could not find method leftShift() for arguments
【fiddler2】使用fiddler mock response 数据
STM32F407ZG PWM
符号表









