1. 新建Web窗体

客户端控件:html控件

服务器控件:用的比较少

2. 数据库连接

        protected void btnLogin_Click(object sender, EventArgs e)
{
try //这里做检测,是因为如果没有检测,连接错误后会把数据库里的内容显示在出错网页中
{
string username = txtUserName.Text.Trim(); //删除空白字符
string pwd = txtPwd.Text.Trim();
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(pwd))
{
//Response.Write("用户名或者密码不能为空");
Response.Write("<script>alert('用户名或者密码不能为空');</script>");
}
else
{
//写Sql语句
string sSql = string.Format("select UserID from TestDB.dbo.UserInfor Where UserName='{0}' and Pwd='{1}'", username, pwd); //数据库连接语句
//DataBase:要连接的数据库名称; Server:服务器名称,'.'代表本机服务器,如果服务器在其他地方,这里输入IP地址
//Integrated Security: ture代表windows身份验证,登录名和密码就不需要了;false代表SQL Server身份验证
//Uid用户名;Password密码
//string connStr = "Database=TestDB;Server=.;Integrated Security=false;Uid=sa;Password=123;"; //读取配置文件中的数据库连接语句
string connStr = ConfigurationManager.ConnectionStrings["TestDB"].ToString(); //普通写法,需要自己释放资源
{
//和数据库建立连接,并打开连接
SqlConnection con = new SqlConnection(connStr);
con.Open(); //执行语句,要传两个参数,一个是SQL语句,一个是数据库连接类
SqlCommand cmd = new SqlCommand(sSql, con); //把查询结果赋值给SqlDataReader对象
SqlDataReader read = cmd.ExecuteReader(); //查看是否有记录
if (read.HasRows)
{
Response.Redirect("UserInforM.aspx");
}
else
{
Response.Write("用户名或者密码错误");
} //释放资源,并关闭
read.Dispose();
read.Close();
con.Dispose();
con.Close();
} //用Using可以自动释放资源
{
using (SqlConnection con = new SqlConnection(connStr)) //自动释放con
{
con.Open();
SqlCommand cmd = new SqlCommand(sSql, con);
using (SqlDataReader read = cmd.ExecuteReader()) //自动释放read
{
if (read.HasRows)
{
Response.Write("登录成功");
}
else
{
Response.Write("用户名或者密码错误");
}
}
}
}
}
}
catch (Exception ex)
{
Response.Write("网站正在维修中");
}
}

配置文件:

<connectionStrings>
<add name="TestDB" connectionString="Database=TestDB;Server=.;Integrated Security=false;Uid=sa;Password=123;" providerName="System.Data.SqlClient"/>
</connectionStrings>

3. 绑定服务端控件显示数据库内容

 public partial class UserInforM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
string sSql = string.Format("select UserID,UserName,RealName,ClassName,Major from TestDB.dbo.UserInfor");
string connStr = ConfigurationManager.ConnectionStrings["TestDB"].ToString(); using (SqlConnection con = new SqlConnection(connStr)) //自动释放con
{
con.Open();
SqlCommand cmd = new SqlCommand(sSql, con);
using (SqlDataReader read = cmd.ExecuteReader()) //自动释放read
{
GridView1.DataSource = read;
GridView1.DataBind();
}
}
}
catch (Exception ex)
{ Response.Write("错误");
}
}
}

ADO1的更多相关文章

  1. django之form表单验证

    django中的Form一般有两种功能: 输入html 验证用户输入 #!/usr/bin/env python # -*- coding:utf- -*- import re from django ...

  2. ADO数据库操作方式

    微软公司的ADO (ActiveX Data Objects) 是一个用于存取数据源的COM组件.它提供了编程语言和统一数据访问方式OLE DB的一个中间层.允许开发人员编写访问数据的代码而不用关心数 ...

  3. ADO.NET DBHelper 类库

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

随机推荐

  1. 3.1 HiveServer2.Beeline JDBC使用

    https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients 一.HiveServer2.Beeline 1.HiveSer ...

  2. Eclipse+Maven+TestNg+ReportNg 生成测试报告

    http://blog.csdn.net/a542551042/article/details/46729585

  3. https协议(4)

    架构层次 HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTT ...

  4. 学习RadonDB源码(二)

    1. 为我新的一天没有放弃而喝彩 学习是一件很容易放弃的事情,因为就算是不学,我也能在现在的岗位上发光发热.可是人不就是一个热爱折腾的种群吗? 今天没有放弃不代表明天没有放弃,也许放弃的可能性大于坚持 ...

  5. Scut

    这是一款免费开源的游戏服务器引擎,适用于开发AVG.SLGRPG.MMOG等类型的网络游戏,同时支持Http.WebSocket和Socket协议通讯,支持Window.Mac和Linux多种平台部署 ...

  6. TopJUI通过简单的代码实现复杂的批量提交功能

    业务系统的批量提交是常用的操作功能,使用传统的EasyUI开发时需要写不少代码才能实现,该功能在TopJUI中是如何实现的呢?本篇我们将通过简单的代码,把批量操作的具体实现分享给大家参考. <a ...

  7. C 语言实例 - 查找字符在字符串中出现的次数

    C 语言实例 - 查找字符在字符串中出现的次数 C 语言实例 C 语言实例 查找字符在字符串中的起始位置(索引值从 开始). 实例 #include <stdio.h> int main( ...

  8. bzoj1139:[POI2009]Wie

    传送门 状压dp,最短路 spfa似乎特别慢 代码: #include<cstdio> #include<iostream> #include<algorithm> ...

  9. physics(2018.10.27)

    这道题可以推出\(O(1)\)的算法,但是实际上暴力模拟就可以过了. 代码(暴力模拟): #include<cstdio> #include<algorithm> #inclu ...

  10. 关于JS中的call()方法和apply() 暂时只接触到call() 等接触到apply()再回头来看

    1. 每个函数都包含两个非继承而来的方法:call()方法和apply()方法. 2. 相同点:这两个方法的作用是一样的. 都是在特定的作用域中调用函数,等于设置函数体内this对象的值,以扩充函数赖 ...