ADO1
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的更多相关文章
- django之form表单验证
django中的Form一般有两种功能: 输入html 验证用户输入 #!/usr/bin/env python # -*- coding:utf- -*- import re from django ...
- ADO数据库操作方式
微软公司的ADO (ActiveX Data Objects) 是一个用于存取数据源的COM组件.它提供了编程语言和统一数据访问方式OLE DB的一个中间层.允许开发人员编写访问数据的代码而不用关心数 ...
- ADO.NET DBHelper 类库
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
随机推荐
- 5.oracle中一个字段中存储'a','b'与'a'与a的写法,存储过程中与之对应
select '''a'',''b''' from dual; --'a','b' select '''a''' from dual; --'a' select 'a' from dual; --a
- 面试6-----11 const和静态变量那些事儿
6 看看const和指针的那些事儿 const在int*左边 const在int*右边 const在int*两边------>请看代码注释 (1)代码 #include <stdio.h& ...
- c++重载输入输出运算符
1 最好打断点看看哦 2例子 #include <iostream> using namespace std; class Complex2 { public: Complex2(, ) ...
- Flutter实战视频-移动电商-38.路由_Fluro中Handler编写方法
38.路由_Fluro中Handler编写方法 在main.dart中初始化Fluro 编写handler 在lib下新建routers文件夹,表示里面要很多路由相关的文件 我们声明一个Handler ...
- Flutter实战视频-移动电商-43.详细页_补充首页跳转到详细页
43.详细页_补充首页跳转到详细页 首页轮播点击到详细页 修改我们轮播这里的代码:SwiperDiy这个类这里的代码 return InkWell( onTap: (){ Application.ro ...
- lua中文教程【基本语法】
代码例子:http://www.inf.puc-rio.br/~roberto/book/code.html 注意:没有“:” 1.特点:可扩展.简单.高效.跨平台 2.使用方式:嵌入程序.独立使用. ...
- c#重写 重载
重写:当一个子类继承一父类,而子类中的方法与父类中的方法的名称,参数个数.类型都完全一致时,就称子类中的这个方法重写了父类中的方法. 重写:通常,派生类继承基类的方法.因此,在调用对象继承方法的时候, ...
- 《剑指offer面试题4》替换空格——实现函数把字符串中每个空格替换成“%20”
思路: 例如把we are happy这个字符串中所有空格替换成"%20",最直接的做法是从头开始扫苗,遇到空格就替换,并且把空格后面的字符都顺序后移.复杂度O(n^2). 重要思 ...
- 利用ant 和 Junit 生成测试报告
我们除了使用java来直接运行junit之外,我们还可以使用junit提供的junit task与ant结合来运行. 涉及的几个主要的ant task如下: <junit>,定义一个jun ...
- js引用类型的赋值
在开发中,有时候需要将数组或者对象的值赋予其他另一个变量,但是两个变量之间会相互影响,因为在将引用类型的值赋给其他变量时,赋予的其实是内存中的存储地址 var arr = [1,2,3,4,5] va ...