schematable.cs

来自「Csharp实例编程百例.rar」· CS 代码 · 共 51 行

CS
51
字号
using System;
using System.Data;
using System.Data.OleDb;

namespace SchemaTableApp
{
	class SchemaTable
	{
		static void Main(string[] args)
		{
			string ConStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\C#Program\C#100\Chapter3\NWIND.mdb";

			// Pass connection string to the connection object 
			OleDbConnection connection = new OleDbConnection(ConStr);

			try
			{
				connection.Open();

				OleDbCommand command = new OleDbCommand("SELECT * FROM Employees",connection);
				OleDbDataReader dataReader = command.ExecuteReader();

				// Store the data structure of the Employees table in a DataTable
				DataTable schemaTable = dataReader.GetSchemaTable();

				/* Display data structure info of each row in the 
				 * returned DataTable, which describes
				 * one column in the original table */
				foreach (DataRow dRow in schemaTable.Rows)
				{
					foreach (DataColumn dCol in schemaTable.Columns)
						Console.WriteLine(dCol.ColumnName + " = " + dRow[dCol]);
					Console.WriteLine("----------------");
				}

				dataReader.Close();
			}
			catch(Exception ex)
			{
				Console.WriteLine("Error Orccured: " + ex.Message);
			}
			finally
			{
				connection.Close();
				Console.ReadLine();
			}
		}  
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?