How to: Use the DataReader GetSchematable method and Visual C # .NET search column architecture (from MSDN)

xiaoxiao2021-03-06  75

The release number of this article has been CHS310107

For Microsoft Visual Basic .NET versions of this article, see

310108.

For Microsoft Visual C .NET versions of this article, see

309683.

This article references the following Microsoft .NET Framework Class Bank Name Space:

System.Data.Oledb

This task content

summary

When to use the getSchematable method Use the OLE DB .NET Provider Search column architecture using SQL .NET Provider Search Column Architecture Reference

Summary This article demonstrates how to use ADO.NET

DataReader object

GetSchematable method Search column architecture information. Another name of the column architecture is the field attribute of the column. Column architecture information includes the following information about the column:

Name Data Type Size This Column is whether the primary key field This column is the auto number (automatic increase) field

GetSchematable method Returns a included

DataReader's column architecture

DataTable properties.

The rows in the DATATABLE correspond to each field of the result set. Each column maps an attribute of the result set field, respectively.

Datarable column

The columnName property is the properties of the field (such as

Columnname,

DataType,

Columnize,

IsKeyColumn or

The name of the isautoincrement property).

The value of the DataTable column is the value of the properties of the field, such as

ColumnName properties

FigStName value.

You can

The GetSchematable method is used for OLE DB .NET provider or SQL .NET provider.

OLEDBDATADER.GETSCHEMATABLE method maps to OLE DB

Icolumnsrowset :: getColumnsRowSet method.

The SqlDataReader.getschematable method does not use the OLE DB Provider.

It should be noted that unless explicitly use

GetSchematable method, otherwise

DataReader will not return column architecture. Also, if used

GetSchematable Search column architecture, you will not be updated

DataReader.

DataReader always retrieves read-only, only forward data streams from the database.

Back to top

When to use the getSchematable method

SQLConnection objects do not support searching SQL Server architecture information using methods similar to the getoledbschematable method of the OLEDBCONNECTION object. The GetSchematable method for the SQLDataReader class provides a more straightforward way to get the column architecture information from SQL Server. Although the GetoledBschematable method of the OLEDBConnection object can return databases, tables, and column architecture information,, if you only want to search columnship information, you will find that the getSchematable method of the DataReader object is easier to use. When you customize the column name and other column properties, you can use the GetSchematable method based on an architecture of an existing DataTable property to create a new DataTable. See "Visual Studio sample: Fitch and Mather 7.0 Run SQL Query" this topic, which is included in the Microsoft Visual Studio .NET online help documentation in Microsoft Visual Studio .NET.

Back to top

Use the OLE DB .NET Provider Search Column Architecture This example lists the schema information of the column (field properties) of the Employees table in the SQL Server Northwind database. Note that when you use the OLE DB .NET provider, it will be used.

OledbDataReader object

GetSchematable method.

Start Visual Studio .NET and create a Visual C # console application project. By default, class1.cs is created. Open the Class1 code window. Paste the following code to the top of the code window, on the Namespace declaration: use system.data;

Using system.data.oledb; in the code window, paste the following code into the main function: OLEDBCONNECTION CN = New OLEDBCONNECTION ();

OLEDBCOMMAND CMD = New OLEDBCommand ();

DataTable Schematable;

OLEDBDATAREADER MyReader;

// Open a connection to the sql server northwind database.

Cn.connectionstring = "provider = sqloledb; data source = server; user id = login;

Password = password; initial catalog = northwind ";

Cn.open ();

// Retrieve Records from The Employees Table Into a DataReader.

CMD.Connection = CN;

cmd.comMandtext = "SELECT * from Employees";

MyReader = cmd.executeReader ();

// Retrieve Column Schema Into a DataTable.

Schematable = myReader.getschematable ();

// for Each Field in The Table ...

Foreach (DataRow myfield in schematable.rows) {

// for Each Property of the Field ...

Foreach (Datacolumn MyProperty In schement.column) {

// Display The Field Name and Value.

Console.writeline (MyProperty.Columnname "=" myfield [myproperty] .tostring ());

}

Console.writeLine ();

// pause.

Console.readline ();

}

// ALWAYS Close The DataReader and Connection.

MyReader.Close ();

Cn.close (); Modify the parameters of the Connectionstring property to properly connect to your SQL Server computer. Press F5 to compile and run the project. You will notice that the properties of each field are listed in the console window. Press ENTER to scroll through the list, end the console application, and return to the integrated development environment (IDE).

Back to top

Search using SQL .NET Provider Search Column Architecture This example lists the schema information of the column (field properties) of the Employees table in the SQL Server Northwind database. Note that when you use the SQL .NET provider, it will be used.

SqlDataReader object

GetSchematable method.

Start Visual Studio .NET and create a Visual C # console application. By default, class1.cs is created. Open the Class1 code window. Paste the following code to the top of the code window, on the Namespace declaration: use system.data;

Using system.data.sqlclient; in the code window, paste the following code into the main function: SqlConnection CN = New SqlConnection ();

SQLCommand cmd = new sqlcommand ();

DataTable Schematable;

SqlDataReader MyReader;

// Open a connection to the sql server northwind database.

Cn.connectionstring = "data source = server; user ID = login;

Password = password; initial catalog = northwind ";

Cn.open ();

// Retrieve Records from The Employees Table Into a DataReader.

CMD.Connection = CN;

cmd.comMandtext = "SELECT * from Employees";

MyReader = cmd.executeReader ();

// Retrieve Column Schema Into a DataTable.

Schematable = myReader.getschematable ();

// for Each Field in The Table ...

Foreach (DataRow myfield in schematable.rows) {

// for Each Property of the Field ...

Foreach (Datacolumn MyProperty In schement.column) {

// Display The Field Name and Value.

Console.writeline (MyProperty.Columnname "=" myfield [myproperty] .tostring ());

}

Console.writeLine ();

// pause.

Console.readline ();

}

// ALWAYS Close The DataReader and Connection.

MyReader.Close ();

Cn.close (); Modify the parameters of the Connectionstring property to properly connect to your SQL Server computer. Press F5 to compile and run the project. You will notice that the properties of each field are listed in the console window. Press ENTER to scroll through the list, end the console application, and return to the IDE.

Back to top

Reference

DataReader and

For more information on getSchematable methods, see the following topics for the Visual Studio .NET online help documentation:

Use a DataReader data http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcontheadonetdatareader.asp IDataReader.GetSchemaTable Method http://msdn.microsoft.com/ Library / default.asp? URL = / library / en-us / cpref / html / frsrfsystemDataidaTaDERClassGetschematabletopic.asp Note: This topic contains a complete list of all columns in the returned data table. OleDbDataReader.GetSchemaTable method http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOleDbOleDbDataReaderClassGetSchemaTableTopic.asp SqlDataReader.GetSchemaTable method http://msdn.microsoft.com/ Library / default.asp? url = / library / en-us / cpref / html / frlrfsystemDataSqlclientsqlDataReaderclassgetschematabletopic.asp About using OLEDBConnection objects

The getoledbschematable method retrieves additional information about the architecture information, click the article number below to view the article in the Microsoft Knowledge Base:

309681 HOW TO: Use Getoledbschematable and Visual C # .NET search schedule information

Back to top

The information in this article applies to:

Microsoft ADO.NET (provided with .NET Frame) Microsoft Visual C # .NET (2002)

Recent Updated: 2002-6-18 (1.0) Keyword KBDatabase Kbhowto KbhowTomaster Kboledb KbsqlClient KbsystemData KB310107

转载请注明原文地址:https://www.9cbs.com/read-108999.html

New Post(0)