asp.net代码练习 work023 DataTable的使用
发布日期:2021-05-06 21:17:47 浏览次数:16 分类:精选文章

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

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work023.WebForm1" %>
ID user_name real_name

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 work023{    public partial class WebForm1 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {        }        private void CreateDataTable()        {            System.Data.DataTable table1 = new System.Data.DataTable();            System.Data.DataColumn column1 = new System.Data.DataColumn("ID", typeof(int));            column1.AllowDBNull = false;            column1.AutoIncrement = true;            column1.AutoIncrementSeed = 1;            column1.AutoIncrementStep = 1;            table1.Columns.Add(column1);            System.Data.DataColumn column2 = new System.Data.DataColumn("user_name", typeof(string));            column2.Unique = true;            column2.MaxLength = 20;            table1.Columns.Add(column2);            System.Data.DataColumn column3 = new System.Data.DataColumn("real_name", typeof(string));            column3.MaxLength = 8;            table1.Columns.Add(column3);            System.Data.DataRow row1 = table1.NewRow();            row1[1] = "zhangfei";            row1["real_name"] = "张飞";            table1.Rows.Add(row1);            System.Data.DataRow row2 = table1.NewRow();            row2["user_name"] = "guanyu";            row2[2] = "关羽";            table1.Rows.Add(row2);            Session["Table"] = table1;        }        public void ShowData()        {            if (Session["Table"] == null)            {                CreateDataTable();            }            System.Data.DataTable dt = Session["Table"] as System.Data.DataTable;            for (int i = 0; i < dt.Rows.Count; i++)            {                Response.Write("");                Response.Write(string.Format("{0}", dt.Rows[i][0].ToString()));                Response.Write(string.Format("{0}", dt.Rows[i]["user_name"].ToString()));                Response.Write(string.Format("{0}", dt.Rows[i]["real_name"].ToString()));                Response.Write("");            }        }    }}

 

上一篇:win7 IIS配置php过程的总结
下一篇:asp.net代码练习 work022 sqlDataAdapter和DataSet的使用

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年04月08日 21时31分15秒