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

Source: Internet
Author: User
Use the DataReader GetSchemaTable () method to obtain the structure information contained in a datasheet that returns the ColumnName property value of each row (row) in a datatable,datatable that 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)

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.