ADO getting SQL Server database schema information

Source: Internet
Author: User

1. Determine the number of available fields

The SqlDataReader class provides the FieldCount property to determine how many fields the query has reneged on.

2. Determine the number of rows returned

There are no properties in SqlDataReader that indicate the available rows.

3. Determine the name of the field

Using the GetName method of SqlDataReader, the method takes an int integer, specifies the ordinal of the field, and returns its name in a field.

4. Confirm that the field is in the. The data types in net

To determine what is used to store content in a specific field. NET data type, use SqlDataReader's GetFieldType method, similar to the GetName method, accept an int integer type, specify the ordinal of the field,

The GetFieldType method returns its data type in the typed object.

5. Determine the database data type of the field

The Getdatatypename method of SqlDataReader, which takes an int integer, returns a string that has the data type of the field in the database.

C # code:
1String connstr =@"Datasource=zhang-c;initialcatalog=sq;integrated Security=true";2String strSQL ="SELECT * FROM T_code";3 SqlConnection conn =NewSqlConnection (CONNSTR);4Conn. Open ();5 SqlCommand cmd =NewSqlCommand ();6 cmd. Connection =Conn7 Cmd.commandtext =strSQL;89 SqlDataReader Read =Cmd. ExecuteReader (commandbehavior.schemaonly);10for (int i =0; I < read. FieldCount; i++)11{12 Console.WriteLine ( "  ,i); 13 Console.WriteLine ( " Field name: {0}  ",read. GetName (i)); 14 Console.WriteLine ( " ,read. GetFieldType (i). Name); 15 Console.WriteLine ( "  data type name in database: {0},read . Getdatatypename (i)); 16}             
View Code

Output

ADO getting SQL Server database schema information

Related Article

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.