当前位置:网站首页>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");
}
}
}
注意:要连接的数据库某张表中的用户名和密码数据就作为判断页面登陆是否成功的依据!
边栏推荐
猜你喜欢
随机推荐
51单片机手动自动智能窗户窗帘控制系统手动自动定时
LeetCode 1351.统计有序矩阵中的负数(简单)
LeetCode 面试题17.14 最小k个数(中等)
以STM32F103C6TA为例通过配置CubeMX实现GPIO输出完成点灯实例
LeetCode 剑指offer 21.调整数组顺序使奇数位于偶数前面(简单)
.NET操作Excel高效低内存的开源框架 - MiniExcel
pytorch-05. Implementing linear regression with pytorch
GUI_AWT
【简易笔记】PyTorch官方教程简易笔记 EP2
STM32单片机LORA无线远程火灾报警监控系统DS18B20MQ2火焰检测
Flutter的生命周期
力扣——情侣牵手
Test of the opposite sex what you look like?
STC12C5A60S2单片机WIFI信号扫描报警监视系统信号增强信号过低报警
The way for programmers to make money from a sideline business and increase their monthly income by 20K
VTK 初步 (1) ----- 可视化管线
.Net Core导入千万级数据至Mysql
pytorch-10.卷积神经网络(作业)
51单片机营养液自动配置搅拌系统TDS浓度采集自动加水加营养液
三种素数筛总结——(朴素筛,埃氏筛,线性筛)









