ASP

本类阅读TOP10

·asp常用数据库连接方法和技巧
·无组件生成BMP验证码
·一些常用的辅助代码 (网络收藏)
·JavaScript实现的数据表格:冻结列、调整列宽和客户端排序
·VisualStudio.NET_2003及其 MSDN 下载地址
·ASP模拟MVC模式编程
·图片以二进制流输出到网页
·MD5加密算法 ASP版
·ASP.NET编程中的十大技巧
·改进 ASP 的字符串处理性能

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
ADO.Net:从DataReader中获取数据表的Schema信息

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

使用DataReader的GetSchemaTable()方法可以获得数据表中包含的结构信息,此方法返回一个DataTable,DataTable中的每一行(Row)的ColumnName属性值即为数据表的一个字段名。

   SqlConnection coreDB=new SqlConnection();
   coreDB.ConnectionString= "workstation id=\"GQA-ERIC-LV\";packet size=4096;integrated security=SSPI;" +
    "data source=\"gqa-eric-lv\";persist security info=False;initial catalog=CoreDB";

   string mySelectQuery = "SELECT ID, Title FROM myBBS order by id asc";
   SqlCommand myCommand = new SqlCommand(mySelectQuery,coreDB);
   coreDB.Open();
   SqlDataReader myReader=myCommand.ExecuteReader();

   //将数据库定义的信息保存到表SchemaTable中:
   DataTable SchemaTable=myReader.GetSchemaTable();
   //表SchemaTable的每一行表示数据库表一个字段的信息:
   Response.Write("<table border=1 align=center><tr>");  
   foreach(DataRow myRow in SchemaTable.Rows)
   {
    Response.Write("<td>");
    Response.Write(myRow["ColumnName"]);
    Response.Write("</td>");
   }
   Response.Write("</tr>");
   while(myReader.Read())
   {
    Response.Write("<tr>");
    Response.Write("<td>"+myReader["ID"].ToString()+"</td>");
    Response.Write("<td><a href=query.aspx?id="+myReader["ID"]+">"+myReader["title"].ToString()+"</a></td>");
    Response.Write("</tr>");
   }
   Response.Write("</table>");
   myReader.Close();
   coreDB.Close();

显示的结果如图:

以上亦例示了SqlConnection,SqlCommand,DataReader的使用。

数据库coreDB中的表myBBS定义如下:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[myBBS]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[myBBS]
GO

CREATE TABLE [dbo].[myBBS] (
 [ID] [bigint] IDENTITY (1, 1) NOT NULL ,
 [Title] [char] (160) COLLATE Chinese_PRC_CI_AS NULL ,
 [Author] [char] (20) COLLATE Chinese_PRC_CI_AS NULL ,
 [Date_of_Created] [datetime] NULL ,
 [Abstract] [char] (480) COLLATE Chinese_PRC_CI_AS NULL ,
 [Content] [ntext] COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

 




相关文章

相关软件