当前位置:网站首页>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");
}
}
}
注意:要连接的数据库某张表中的用户名和密码数据就作为判断页面登陆是否成功的依据!
边栏推荐
- pytorch-11. Convolutional Neural Network (Advanced)
- 51单片机营养液自动配置搅拌系统TDS浓度采集自动加水加营养液
- generic notes()()()
- STM32单片机OLED经典2048游戏单片机小游戏
- LeetCode 94.二叉树的中序遍历(简单)
- LruCache与DiskLruCache结合简单实现ImageLoader
- The Principle of Union Search and API Design
- Day1 微信小程序-小程序代码的构成
- LeetCode 938.二叉搜索树的范围和(简单)
- STM32F407ZG GPIO输入相关实验
猜你喜欢

【fiddler3】使用fiddler设置弱网模式

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

LeetCode 剑指offer 10-I.斐波那契数列(简单)

LeetCode 938.二叉搜索树的范围和(简单)

LeetCode 162. Finding Peaks (Moderate)

51单片机智能蓝牙APP加油站火灾预警安防防控报警监控系统MQ2DHT11

pytorch-06.逻辑斯蒂回归

51单片机RS485远程双机多机温度采集主从机多节点蜂鸣器报警

树结构——2-3树图解

Convolutional Neural Network (CNN) for mnist handwritten digit recognition
随机推荐
pytorch-11. Convolutional Neural Network (Advanced)
Gradle学习 (一) 入门
栈和队列
PyTorch 之 可视化网络架构
Common class String overview
PyTorch之CV
Tkinter 模块学习
C陷阱与缺陷 个人阅读笔记
我不喜欢我的代码
以STM32F103C6TA为例通过配置CubeMX实现GPIO输出完成点灯实例
Convolutional Neural Network (CNN) for Clothing Image Classification
pytorch-10. Convolutional Neural Networks
[List Exercise] Traverse the collection and sort by price from low to high,
Day1 微信小程序-小程序代码的构成
2021-04-15 jacoco代码覆盖率统计和白盒测试
详解样条曲线(上)(包含贝塞尔曲线)
I don't like my code
PyTorch之模型定义
三种素数筛总结——(朴素筛,埃氏筛,线性筛)
Collection Map