asp.net代码练习 work022 sqlDataAdapter和DataSet的使用
发布日期:2021-05-06 21:17:46 浏览次数:25 分类:精选文章

本文共 3739 字,大约阅读时间需要 12 分钟。

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work022.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 work022{    public partial class WebForm1 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {        }        public void ShowData()        {            System.Data.SqlClient.SqlConnectionStringBuilder bu = new System.Data.SqlClient.SqlConnectionStringBuilder();            bu.DataSource = "(local)";            bu.InitialCatalog = "test";            bu.UserID = "sa";            bu.Password = "123456";            string conString = bu.ConnectionString;                        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(conString);            con.Open();            string sql = "select * from userinfo where sex=0";            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con);            System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(cmd);            System.Data.DataSet ds = new System.Data.DataSet();            adapter.Fill(ds);            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)            {                Response.Write("");                Response.Write(string.Format("{0}",ds.Tables[0].Rows[i][0].ToString()));                Response.Write(string.Format("{0}", ds.Tables[0].Rows[i][1].ToString()));                Response.Write(string.Format("{0}", ds.Tables[0].Rows[i]["real_name"].ToString()));                Response.Write(string.Format("{0}", ds.Tables[0].Rows[i]["age"].ToString()));                Response.Write(string.Format("{0}", bool.Parse(ds.Tables[0].Rows[i]["sex"].ToString()) == true?"男":"女"));                Response.Write(string.Format("{0}", ds.Tables[0].Rows[i]["mobile"].ToString()));                Response.Write(string.Format("{0}", ds.Tables[0].Rows[i]["phone"].ToString()));                Response.Write(string.Format("{0}", ds.Tables[0].Rows[i][7].ToString()));                Response.Write("");            }            con.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代码练习 work023 DataTable的使用
下一篇:asp.net代码练习 work021 DataReader的使用

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年03月30日 01时10分58秒