ado| Data uses the DataReader GetSchemaTable () method to obtain the structure information contained in the datasheet, which returns each row in a datatable,datatable (row) The ColumnName property value is a field name for the datasheet.
SqlConnection coredb=new SqlConnection ();
coredb.connectionstring= "Workstation id=\" gqa-eric-lv\ ";p acket size=4096;integrated Security=sspi;"
"Data source=\" gqa-eric-lv\ ";p ersist security info=false;initial catalog=coredb";
String myselectquery = "Select ID, Title from Mybbs ORDER by ID ASC";
SqlCommand mycommand = new SqlCommand (myselectquery,coredb);
Coredb.open ();
SqlDataReader Myreader=mycommand.executereader ();
To save database-defined information to the table schematable:
DataTable schematable=myreader.getschematable ();
Each row of the table schematable represents information about a field in a database table:
Response.Write ("<table border=1 align=center><tr>");
foreach (DataRow myrow in schemaTable.Rows)
{
Response.Write ("<td>");
Response.Write (myrow["ColumnName"]);
Response.Write ("</td>");
}
Response.Write ("</tr>");
while (Myreader.read ())
{
Response.Write ("<tr>");
Response.Write ("<td>" +myreader["ID"]. ToString () + "</td>");
Response.Write ("<td><a href=query.aspx?id=" +myreader["id"]+ ">" +myreader["title"]. ToString () + "</a></td>");
Response.Write ("</tr>");
}
Response.Write ("</table>");
Myreader.close ();
Coredb.close ();
The results shown are as follows:
The above example also shows the use of Sqlconnection,sqlcommand,datareader.
The table Mybbs in the database coredb is defined as follows:
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Mybbs] and OBJECTPROPERTY (ID, N ' isusertable ') = 1)
drop table [dbo]. [Mybbs]
Go
CREATE TABLE [dbo]. [Mybbs] (
[ID] [bigint] IDENTITY (1, 1) is not NULL,
[Title] [Char] (160) COLLATE chinese_prc_ci_as NULL,
[Author] [Char] (m) COLLATE chinese_prc_ci_as NULL,
[Date_of_created] [DateTime] Null
[Abstract] [Char] (COLLATE) Chinese_prc_ci_as NULL,
[Content] [ntext] COLLATE Chinese_prc_ci_as not NULL
) on [PRIMARY] textimage_on [PRIMARY]
Go