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