
asp.net代码练习 work021 DataReader的使用
发布日期:2021-05-06 21:17:46
浏览次数:10
分类:技术文章
本文共 3700 字,大约阅读时间需要 12 分钟。
webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work021.WebForm1" %>
webform1.aspx.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace work021{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public void ShowData() { System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(); builder.DataSource = "(local)"; builder.InitialCatalog = "test"; builder.UserID = "sa"; builder.Password = "123456"; string conString = builder.ConnectionString; System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(conString); con.Open(); string sql = "select * from userinfo where sex=1"; System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con); //行为,关闭reader时自动关闭con System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); while (reader.Read()) { Response.Write(""); //根据列号,获取数值型 Response.Write(string.Format("{0}",reader.GetInt32(0))); //根据列号,获取字符串型 Response.Write(string.Format("{0}", reader.GetString(1))); Response.Write(string.Format("{0}", reader.GetString(2))); //根据列号,获取数值型 Response.Write(string.Format("{0}", reader.GetByte(3))); //根据列号,获取布尔型 Response.Write(string.Format("{0}", reader.GetBoolean(4) == true?"男":"女")); //根据列号,获取 Response.Write(string.Format("{0}", reader[5].ToString())); //根据列名,获取 Response.Write(string.Format("{0}", reader["phone"].ToString())); Response.Write(string.Format("{0}", reader["email"].ToString())); Response.Write(""); } reader.Close(); } }}
sql
create database test;create table UserInfo( user_id int identity(1,1) not null, user_name varchar(20) not null unique, real_name nvarchar(8) not null, age tinyint not null, sex bit not null, mobile varchar(14), phone varchar(14), email varchar(50) not null, primary key(user_id) );insert into UserInfo values('zhangfei','张飞',36,1,'13011110001','8845996','zf@qq.com');insert into UserInfo values('guanyu','关羽',38,1,'13011110002','8845995','gy@qq.com');insert into UserInfo values('liubei','刘备',42,1,'13011110003','8845994','lb@qq.com');insert into UserInfo values('zhaoyun','赵云',32,1,'13011110004','8845993','zy@qq.com');insert into UserInfo values('huangzhong','黄忠',50,1,'13011110005','8845992','hz@qq.com');insert into UserInfo values('caocao','曹操',48,1,'13011110006','8845991','cc@qq.com');insert into UserInfo values('sunquan','孙权',33,1,'13011110007','8845990','sq@qq.com');insert into UserInfo values('diaochan','貂蝉',20,0,'13011110008','8845880','dc@qq.com');insert into UserInfo values('daqiao','大乔',21,0,'13011110009','8845881','dq@qq.com');insert into UserInfo values('xiaoqiao','小乔',20,0,'13011110010','8845882','xq@qq.com');
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月04日 19时00分45秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
android基础知识回顾--ContentProvider简单用法
2019-03-03
压缩解压
2019-03-03
js try{}catch(){}finally{}语句
2019-03-03
ES6 函数模块(四)
2019-03-03
JavaScript入门
2019-03-03
R3 PRO 3200G和r7 3700u 哪个好
2019-03-03
入手评测 联想小新Pro14和Air14Plus哪个好?区别对比
2019-03-03
程序人生:没有伞的孩子要学会奔跑
2019-03-03
Express Animate for mac(动画特效制作软件)
2019-03-03
macOS Big Sur系统中如何开启设置触控板三指拖拽功能?
2019-03-03
修复苹果Mac中的快速视频播放错误的方法
2019-03-03
苹果HomePod智能音箱怎么使用广播功能?
2019-03-03
Mac系统投屏到电视机的方法
2019-03-03
【Docker&ARM】ARM架构服务器上docker的安装
2019-03-03
【Tinyproxy】CentOS7.X http代理tinyproxy的安装配置与使用
2019-03-03
php-foreach遍历一维数组
2019-03-03
php--自定义错误处理函数的使用方法
2019-03-03