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. error: templates may not be ‘virtual’

     模板函数不能是虚函数,原因如下:   首先呢,模板函数并不是函数,他需要特定的类型去实例化成为函数.你定义一个函数模板,是不生成任何函数的,只有当你用代码去调用它时,才会根据你的类型去实例化成为特定 ...

  2. ASP.NET Core会议管理平台实战_汇总贴

    ASP.NET Core会议管理平台实战 课程地址:https://ke.qq.com/course/389673?from=800004097#term_id=100464670 ASP.NET C ...

  3. E20190402-hm

    porxy n. 代理服务器; 代表权; 代理人,代替物; 委托书; enroll  v. 招收; 注册; 登记; 加入; enrollment n. 注册; 登记; 入会;

  4. Qt测试代码执行时间

    ////////////////////////////////////////////////////////////////// QString _GetIntervalTime(QTime&am ...

  5. VC中使用GDI+实现为按钮加载Png图片

    http://blog.csdn.net/flyfish1986/article/details/5381605 VC中使用GDI+实现为按钮加载Png图片 http://www.codeprojec ...

  6. OPENGL1_环境

    补充说明: 1 vs2010自带opengl的库,不需要单独下载或做任何配置,(但是lib文件,dll文件放的位置过于分散,用的时候感觉不好用) 使用时直接用 #include <gl/GL.h ...

  7. python 函数 之 用户注册register()

    db_path='db.txt' #定义默认文件路径,方便修改def get_uname(): while True: uname=input('请输入用户名:').strip() if uname. ...

  8. go系列(4)- go环境和docker容器的使用

    这篇文章讲述把go环境及beego框架装进容器.docker的安装该篇不讲述,请自行查阅.本篇是基于docker已经安装的前提. 跟据前三篇系列,然后写Dockerfile,一般是到项目的根目录下 1 ...

  9. windows下写的shell脚本到linux上不能运行

    win上是dos模式,需要改成unix模式 方法是: 在linux上vim 打开脚本,然后:set ff=unix

  10. C# 可空类型(Nullable)

    C# 提供了一个特殊的数据类型,nullable 类型(可空类型),可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值. 例如,Nullable< Int32 >,读作& ...