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" %>
<% ShowData();%>
编号 账号 姓名 年龄 性别 手机 电话 电子邮件

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');

 

上一篇:asp.net代码练习 work022 sqlDataAdapter和DataSet的使用
下一篇:asp.net代码练习 work020 ExecuteScalar 的使用

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月04日 19时00分45秒