当前位置:网站首页>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 292.Nim 游戏(简单)

C陷阱与缺陷 个人阅读笔记

pytorch-10.卷积神经网络

卷积神经网络(CNN)实现mnist手写数字识别

Pytorch - 07. Multidimensional characteristics of input processing

Notes for SVM

LeetCode refers to offer 10-I. Fibonacci sequence (simple)

LeetCode 1720.解码异或后的数组(简单)

.Net Core imports tens of millions of data to Mysql

LruCache与DiskLruCache结合简单实现ImageLoader
随机推荐
Flutter的生命周期
pytorch-11.卷积神经网络(高级篇)
以STM32F103C6T6为例通过配置CubeMX实现EXIT外部中断
序列化、编码、requests库json和data参数
R语言聚类分析——代码解析
离散数学的学习记录
I don't like my code
pytorch-06. Logistic regression
LeetCode 1894. Find the student number that needs to be supplemented with chalk
学生管理系统以及其简单功能的实现
STM32F407ZG GPIO输入相关实验
一个基于.Net Core 开源的物联网基础平台
51单片机智能远程遥控温控PWM电风扇系统红外遥控温度速度定时关机
pytorch-09. Multi-classification problem
LeetCode 162.寻找峰值(中等)
(Flutter报错)Cannot run with sound null safety, because the following dependencies
PyTorch之训练技巧
LeetCode 938. Range Sum of Binary Search Trees (Simple)
LeetCode 94.二叉树的中序遍历(简单)
Notes for RNN