站内搜索
分类列表
本类阅读排行
本类推荐文章
- ASP技巧:让Len,Left,Right函数识...
- 用ASP实现IE地址栏参数的判断
- 如何实现ASP.NET网站个性化
- 如何在Asp.net中使用HtmlArea编辑...
- 用ASP.Net实现在线压缩和解压缩
- ASP实现不存在的网页就自动发送邮...
- 用ASP取出HTML里面的图片地址的函...
- asp.net跳转页面的三种方法比较
- 一个Winsock组件
- 用WinSock设计Chat程序(转)
- 在MFC应用中深入定制WebBrowser控...
- 这几天有人问关于编应.dll的问题,...
- 在Visual J++中编写ASP COM组件(转...
- 一个显示Grid的VBScript对象
- 好东西,快来看呀:(转载自中华网...
广告
ADO.Net:从DataReader中获取数据表的Schema信息
作者: 来源: 点击: 日期:2007-2-21 23:28:08
使用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
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
ADO.Net:从DataReader中获取数据表的Schema信息 评论
