本文介绍如何在 ADO.NET 中使用
DataReader对象的
GetSchemaTable方法,以检索列的架构信息。
另一个列的架构名称是其字段属性。
列的架构信息包括有关列的以下信息:
注意要获取主键信息,其中包括域是否是主键的一部分,是一个自动增量字段,您必须设置为CommandBehavior.KeyInfo的DataReader的CommandBehavior值。" _mstchunk="true" _msthash="21361">Note " _mstchunk="true" _msthash="21361">注意AutoIncrement field, you must set the CommandBehavior value of the DataReader to CommandBehavior.KeyInfo" _mstchunk="true" _msthash="42722">若要获取主键信息,其中包括一个字段是否为主键字段和自动增量字段,是否属于必须将DataReader的CommandBehavior值设置为CommandBehavior.KeyInfo
GetSchemaTable方法。NET 提供程序或 SQL。NET提供程序。OleDbDataReader.GetSchemaTable方法映射的 OLE DB IColumnsRowset::GetColumnsRowset方法。SqlDataReader.GetSchemaTable方法不使用 OLE DB 提供程序层。" _mstchunk="true" _msthash="21362">GetSchemaTable method with either the OLE DB .NET Provider or the SQL .NET Provider. " _mstchunk="true" _msthash="21362">与 OLE DB.NET 提供程序或 SQL.NET 提供程序,可以使用GetSchemaTable方法。OleDbDataReader.GetSchemaTable method maps to the OLE DB IColumnsRowset::GetColumnsRowset method. " _mstchunk="true" _msthash="42724">OleDbDataReader.GetSchemaTable方法映射到 OLE DB IColumnsRowset::GetColumnsRowset方法。SqlDataReader.GetSchemaTable method does not use an OLE DB Provider layer." _mstchunk="true" _msthash="64086">SqlDataReader.GetSchemaTable方法不使用 OLE DB 提供程序层。
GetSchemaTable方法, DataReader不返回列的架构。此外,如果您要检索的列模式使用GetSchemaTable ,您无法更新DataReader。DataReader始终检索数据的只读、 只进流数据库。" _mstchunk="true" _msthash="21363">GetSchemaTable method, the DataReader does not return column schema. " _mstchunk="true" _msthash="21363">注意,除非您显式地使用GetSchemaTable方法中, DataReader不返回列的架构。GetSchemaTable to retrieve the column schema, you cannot update the DataReader. " _mstchunk="true" _msthash="42726">此外,如果您使用GetSchemaTable来检索列模式,不能更新DataReader。DataReader always retrieves a read-only, forward-only stream of data from a database." _mstchunk="true" _msthash="64089">DataReader总是从数据库中检索数据的只读、 只进流。
GetSchemaTable method of the OleDbDataReader object. " _mstchunk="true" _msthash="21365">请注意当使用 OLE DB.NET 提供程序时,使用OleDbDataReader对象的GetSchemaTable方法。
- Name
- 数据类型
- 大小
- 列是否是主键字段
- 列是否是自动编号 (自动增量) 字段
注意要获取主键信息,其中包括域是否是主键的一部分,是一个自动增量字段,您必须设置为CommandBehavior.KeyInfo的DataReader的CommandBehavior值。" _mstchunk="true" _msthash="21361">Note " _mstchunk="true" _msthash="21361">注意AutoIncrement field, you must set the CommandBehavior value of the DataReader to CommandBehavior.KeyInfo" _mstchunk="true" _msthash="42722">若要获取主键信息,其中包括一个字段是否为主键字段和自动增量字段,是否属于必须将DataReader的CommandBehavior值设置为CommandBehavior.KeyInfo
GetSchemaTable方法。NET 提供程序或 SQL。NET提供程序。OleDbDataReader.GetSchemaTable方法映射的 OLE DB IColumnsRowset::GetColumnsRowset方法。SqlDataReader.GetSchemaTable方法不使用 OLE DB 提供程序层。" _mstchunk="true" _msthash="21362">GetSchemaTable method with either the OLE DB .NET Provider or the SQL .NET Provider. " _mstchunk="true" _msthash="21362">与 OLE DB.NET 提供程序或 SQL.NET 提供程序,可以使用GetSchemaTable方法。OleDbDataReader.GetSchemaTable method maps to the OLE DB IColumnsRowset::GetColumnsRowset method. " _mstchunk="true" _msthash="42724">OleDbDataReader.GetSchemaTable方法映射到 OLE DB IColumnsRowset::GetColumnsRowset方法。SqlDataReader.GetSchemaTable method does not use an OLE DB Provider layer." _mstchunk="true" _msthash="64086">SqlDataReader.GetSchemaTable方法不使用 OLE DB 提供程序层。
GetSchemaTable方法, DataReader不返回列的架构。此外,如果您要检索的列模式使用GetSchemaTable ,您无法更新DataReader。DataReader始终检索数据的只读、 只进流数据库。" _mstchunk="true" _msthash="21363">GetSchemaTable method, the DataReader does not return column schema. " _mstchunk="true" _msthash="21363">注意,除非您显式地使用GetSchemaTable方法中, DataReader不返回列的架构。GetSchemaTable to retrieve the column schema, you cannot update the DataReader. " _mstchunk="true" _msthash="42726">此外,如果您使用GetSchemaTable来检索列模式,不能更新DataReader。DataReader always retrieves a read-only, forward-only stream of data from a database." _mstchunk="true" _msthash="64089">DataReader总是从数据库中检索数据的只读、 只进流。
何时使用 GetSchemaTable 方法
- SqlConnection对象不支持检索架构信息的 SQL Server在类似于OleDbConnection对象的GetOleDbSchemaTable方法的方法。SqlDataReader类的GetSchemaTable方法,可以轻松获取列模式SQL Server 中的信息。
- 虽然OleDbConnection对象的GetOleDbSchemaTable方法可以返回数据库、 表和列的架构信息,您可能会发现DataReader对象的GetSchemaTable方法是易于使用,如果您要检索仅列架构信息。
- 您可以使用GetSchemaTable方法来创建新的数据表时,自定义的列名称和其他基于现有数据表属性架构列属性。有关代码示例演示如何使用GetSchemaTable来定义新表,请参阅"Visual Studio 示例: Fitch和 Mather 7.0 运行 SQL 查询"Microsoft Visual Studio 中的主题。NET 联机帮助文档。
检索列使用的 OLE DB.NET 提供程序的架构
此示例列出了 SQL Server 罗斯文数据库中雇员表中的列 (字段属性) 的架构信息。GetSchemaTable method of the OleDbDataReader object. " _mstchunk="true" _msthash="21365">请注意当使用 OLE DB.NET 提供程序时,使用OleDbDataReader对象的GetSchemaTable方法。
- 开始 Visual Studio。NET,并创建新 Visual C#控制台应用程序项目。Class1.cs 是默认创建的。
- 打开代码窗口的 Class1。粘贴以下代码在上面的命名空间声明的代码窗口的顶部:
using System.Data;
using System.Data.OleDb;
3. 在代码窗口中,将粘贴的Main函数中添加以下代码:
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(CommandBehavior.KeyInfo);
//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 schemaTable.Columns) {
//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();
4. 修改要正确地连接到您的 SQL Server 的连接字符串属性的参数计算机。
5. 按 F5 键编译并运行该项目。请注意控制台窗口中列出的每个字段的属性。
6. 按 ENTER 键以滚动列表,结束控制台应用程序,并返回到集成开发环境(IDE)。
检索列架构与 SQL.NET 提供程序
此示例列出了 SQL Server 罗斯文数据库中雇员表中的列 (字段属性) 的架构信息。
GetSchemaTable method of the SqlDataReader object. " _mstchunk="true" _msthash="21367">请注意当使用 SQL.NET 提供程序时,使用SqlDataReader对象的GetSchemaTable方法。
- 开始 Visual Studio。NET,并创建新 Visual C#控制台应用程序项目。Class1.cs 是默认创建的。
- 打开代码窗口的 Class1。粘贴以下代码在上面的命名空间声明的代码窗口的顶部:
using System.Data;
using System.Data.SqlClient;
3. 在代码窗口中,将粘贴的Main函数中添加以下代码:
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(CommandBehavior.KeyInfo);
//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 schemaTable.Columns) {
//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();
4.修改要正确地连接到您的 SQL Server 的连接字符串属性的参数计算机。
5.按 f5 键编译并运行该项目。请注意,在控制台窗口中列出的每个字段的属性。
6.按 ENTER 键以滚动列表,结束控制台应用程序,并返回到 IDE 中。