Ado.net: Get schema information for a datasheet from DataReader

Source: Internet
Author: User
Tags tostring
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





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.