using System; using System.Data; using System.Data.SqlClient; namespace Northwind { class Program { static void Main(string[] args) { SqlConnection sqlConn = null; SqlCommand sqlCmd = null; SqlDataReader sqlDR = null; try { //创建连接对象,使用集成安全方式连接,更安全 sqlConn = new SqlConnection(@"data source=localhost; Integrated Security=SSPI;Initial Catalog=northwind"); //创建命令对象,参数1是存储过程名 string strSql = @"select categoryid, categoryname from categories;" + @"select employeeId, lastname from employees"; sqlCmd = new SqlCommand(strSql, sqlConn); //打开数据库 sqlConn.Open(); //执行查询,并将结果集返回给SqlDataReader sqlDR = sqlCmd.ExecuteReader(); //返回结果集中表的架构信息 DataTable schemaTable = sqlDR.GetSchemaTable(); //遍历所有的行,直到结束 foreach (DataRow r in schemaTable.Rows) { foreach (DataColumn c in schemaTable.Columns) Console.WriteLine(c.ColumnName + " = " + r[c]); Console.WriteLine(); } /* do { Console.WriteLine(@"-------------------------------"); Console.WriteLine("{0, -15}{1,-15}", sqlDR.GetName(0), sqlDR.GetName(1)); Console.WriteLine(@"-------------------------------"); while (sqlDR.Read()) { Console.WriteLine("{0, -15}${1,-15}", sqlDR.GetInt32(0), sqlDR.GetString(1)); } Console.WriteLine(); } while (sqlDR.NextResult()); */ } catch (System.Exception e) { Console.WriteLine(e.Message); } finally { //关闭SqlDataReader对象 sqlDR.Close(); //断开数据库连接 sqlConn.Close(); } } } }
ColumnName = categoryid
ColumnOrdinal = 0
ColumnSize = 4
NumericPrecision = 10
NumericScale = 255
IsUnique = False
IsKey =
BaseServerName =
BaseCatalogName =
BaseColumnName = categoryid
BaseSchemaName =
BaseTableName =
DataType = System.Int32
AllowDBNull = False
ProviderType = 8
IsAliased =
IsExpression =
IsIdentity = True
IsAutoIncrement = True
IsRowVersion = False
IsHidden =
IsLong = False
IsReadOnly = True
ProviderSpecificDataType = System.Data.SqlTypes.SqlInt32
DataTypeName = int
XmlSchemaCollectionDatabase =
XmlSchemaCollectionOwningSchema =
XmlSchemaCollectionName =
UdtAssemblyQualifiedName =
NonVersionedProviderType = 8
IsColumnSet = False
ColumnName = categoryname
ColumnOrdinal = 1
ColumnSize = 15
NumericPrecision = 255
NumericScale = 255
IsUnique = False
IsKey =
BaseServerName =
BaseCatalogName =
BaseColumnName = categoryname
BaseSchemaName =
BaseTableName =
DataType = System.String
AllowDBNull = False
ProviderType = 12
IsAliased =
IsExpression =
IsIdentity = False
IsAutoIncrement = False
IsRowVersion = False
IsHidden =
IsLong = False
IsReadOnly = False
ProviderSpecificDataType = System.Data.SqlTypes.SqlString
DataTypeName = nvarchar
XmlSchemaCollectionDatabase =
XmlSchemaCollectionOwningSchema =
XmlSchemaCollectionName =
UdtAssemblyQualifiedName =
NonVersionedProviderType = 12
IsColumnSet = False
请按任意键继续. . .