DataTable control

zhaozj2021-02-16  78

DataTable control

Author: Wawe Time: 2002-9-28 Recommended Level: ★ View author information and author anthology

Author: Herd First, the line aspx file join a DataGrid control, where an id DataGrid1; Second, create a new database dotasp, create a new table UserInfoCREATE TABLE [dbo] [UserInfo] ([id] [int] IDENTITY (. 1, 1) Not null, [username] [25) collate chinese_prc_ci_as null, [PWD] [CHAR] (10) Collate Chinese_PRC_CI_AS NULL, [EMAIL] [VARCHAR] (50) collate Chinese_prc_ci_as null, [addtime] [ DateTime] NULL) ON [primary] then insert some data in this table; three, add the following code in the page_load method of the aspx.cs file: DataTable DT = New DataTable (); // New a DataTable instance DT DataRow DR ; // Declare a DataRow instance DR DT.COLUMNS.ADD (New Datacolumn (INT32))); / / Description What fields Dt.columns.Add in the DataTable table contains DT.columns.add ("User Name" ", TypeOf (String))); DT.Columns.Add (New Datacolumn (" Password ", TypeOf (String)))); DT.COLUMNS.ADD (New Datacolumn (" Email ", TypeOf (String))); DT .Columns.add (New Datacolumn ("Add Time", TypeOf (DateTime))); String StrConn = "User ID = SA; Password =; Initial Catalog = Dotasp; Data Source = Jeff"; // Defines the database's Connection SQLConnection Conn = new sqlconnection (string strsql = "select * from userinfo order by addtime desc"; sqlcommand cmd = new sq LCOMMAND (strsql, conn); cmd.connection.open (); SqlDataReader DR = cmd.executeReader (); // Newly built an instance Dr WHILE (DR.READ ()) {DR = DT.NEWROW (); // Instance DR DR DR DR DR DR DR DR DR DR DR DR DR DR [0] = DR ["Id"] with DATATABLE (); // The ID field in // database is filled in the first column DR [1] = DR ["" ""]. ToString (); // The username field in the database fills in the second column DR [2] = DR ["PWD"] = DR ["PWD"]. Tostring (); DR [3] = " " DR [" email "]. toString () " "; DR [4] = DR [" Addtime "] .Tostring (); dt.Rows.Add (DR);

转载请注明原文地址:https://www.9cbs.com/read-18496.html

New Post(0)