惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Latest news
Latest news
G
GRAHAM CLULEY
P
Privacy International News Feed
Know Your Adversary
Know Your Adversary
N
Netflix TechBlog - Medium
C
CERT Recently Published Vulnerability Notes
O
OpenAI News
T
Tenable Blog
Attack and Defense Labs
Attack and Defense Labs
S
Schneier on Security
The GitHub Blog
The GitHub Blog
M
MIT News - Artificial intelligence
PCI Perspectives
PCI Perspectives
博客园 - 司徒正美
Microsoft Azure Blog
Microsoft Azure Blog
MyScale Blog
MyScale Blog
Martin Fowler
Martin Fowler
S
Secure Thoughts
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
T
The Exploit Database - CXSecurity.com
U
Unit 42
The Register - Security
The Register - Security
Google DeepMind News
Google DeepMind News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
WordPress大学
WordPress大学
B
Blog
Project Zero
Project Zero
NISL@THU
NISL@THU
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
I
Intezer
L
Lohrmann on Cybersecurity
Webroot Blog
Webroot Blog
P
Proofpoint News Feed
C
Check Point Blog
Schneier on Security
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Hugging Face - Blog
Hugging Face - Blog
I
InfoQ
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
Spread Privacy
Spread Privacy
The Hacker News
The Hacker News

博客园 - gzboy

一个flex例子(自定义download progress bar的) AS3的中文学习资料 终于可以让Flash基于C#完美操作数据库了,想开发全flash的,全部读取数据库的个人BLOG... 基于.NET + FMS + SQLSERVER2005 + Flash Remoting(技术)的FLASH播放器开发成功 基于FMS的在线录制例子... - gzboy - 博客园 loadrunner8.0 问个关于SQL 2005同步的问题 Flex2下载地址及注册码 让IIS 6.0支持FLV的方法 残念,我决定放弃Flash Remoting!!! 在Flash中接收来自页面(.NET)的值的方法. 大概看了一下《Flash MX 2004 -- 数据库应用开发 - 基于.NET架构》,感觉有点迷惘了! 在flash remoting for .net中,AS代码是怎样调用aspx中的具体方法呢? 通过Flash Remoting,怎么通过页面主动传递值给Flash player呢? 调试Flash Remoting中出现的问题. 又出现一个技术难关:Flash remoting的应用 大文件上传组件终于调试成功了... 还是大文件上传的问题,超级郁闷,有相关经验的进来说说... .NET中大文件上传遇到的问题.
获取数据库名称与数据库中表名的方法
gzboy · 2006-12-30 · via 博客园 - gzboy

在很多情况下我们需要将指定的数据库中的所有表都列出来。在使用c#进行软件开发时,我们有哪些方法可是实现这个目的呢?本人对此进行概要的总结,有以下6中方式可以实现这个目的。

1、sqldmo
SQLDMO是操作SQLServer的理想的方式,如果您的数据库是SQLServer就可以考虑使用这种方式。在C#中使用SQLDMO需要添加SQLDMO的引用,然后在当前的文件中using SQLDMO;即可以使用SQLDMO。SQLDMO的对象模型大家可以在SQLServer的帮助中获得。

        private void GetTabels_DMO(string strServerName,string strUser,string strPWD,string strDatabase)
        {
            SQLDMO.SQLServer Server = new SQLDMO.SQLServerClass();                
            //连接到服务器
            Server.Connect(strServerName,strUser,strPWD);
            //对所有的数据库遍历,获得指定数据库
            for(int i=0;i<Server.Databases.Count;i++)
            {
                //判断当前数据库是否是指定数据库
                if(Server.Databases.Item(i+1,"dbo").Name ==strDatabase)
                {
                    //获得指定数据库
                    SQLDMO._Database db= Server.Databases.Item(i+1,"dbo");
                    //获得指定数据库中的所有表
                    for(int j=0;j<db.Tables.Count;j++)
                    {
                        MessageBox.Show(db.Tables.Item(j+1,"dbo").Name);
                    }
                }
            }
        }

2、adox

adox是ado Extensions for DDL and Security,是微软对ADO技术的扩展,使用它我们可以操作数据库的结构。它是一个COM组件,估计以后在ADO.NET中会增加ADOX的一些功能。如果大家需要ADOX的一些资料,我可以提供。下面的一个例子就是使用ADOX来获得当前数据库的所有表。

        private void GetTables_ADOX()
        {
            //ADO的数据库连接
            ADODB.ConnectionClass cn=new ADODB.ConnectionClass();
            string ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=Test;Data Source=HBXP";
            cn.Open(ConnectionString,"sa","",0);
            //操作ADOX的Catalog对象
            CatalogClass cat=new CatalogClass();
            cat.ActiveConnection=cn;
            for(int i=0;i<cat.Tables.Count;i++)
            {
                MessageBox.Show(cat.Tables[i].Name);
            }
        }
注意:在上面的代码中cat.ActiveConnection不能是ADO.Net中的Connection,而应该是ADO的Connection。

3、ado.net中的oledbconnection

在c#中我们首先会考虑使用ado.net来解决问题,如果没有方法才会考虑使用adox或者sqldmo来解决这个问题。虽然adox和sqldmo也能够解决这个问题,但是他们毕竟是com组件,在.net中使用起来和在非.net平台会有一些差异,不是很顺手。下面的示例就显示了在ado.net中的oledbconnection的方法getoledbschematable来获得数据库的架构。大家可以在msdn中看到这个方法的说明:

public DataTable GetOleDbSchemaTable(
   Guid schema,
   object[] restrictions);
参数
schema
OleDbSchemaGuid 的值之一,它指定要返回的架构表。
restrictions
限制值的 Object 数组。这些值按照限制列的顺序来应用。即,第一个限制值应用于第一个限制列,第二个限制值应用于第二个限制列,依此类推。
返回值
包含请求的架构信息的 DataTable。
更多的信息大家可以查询MSDN,下面将示例如何实现。


        private void GetTables_ADONET()
        {
            //处理OleDbConnection
            string strConnectionString=@"Integrated Security=SSPI;Data Source=HBXP;Initial Catalog=Test;Provider=SQLOLEDB.1";
            OleDbConnection cn=new OleDbConnection(strConnectionString);
            cn.Open();
            //利用OleDbConnection的GetOleDbSchemaTable来获得数据库的结构
            DataTable dt = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] {null, null, null, "TABLE"});
            foreach (DataRow dr in  dt.Rows)
            {
                MessageBox.Show((String)dr["TABLE_NAME"]);
            }
           
        }

4、信息架构视图

信息架构视图是sql-92 标准中定义的架构视图,这些视图独立于系统表。信息架构视图的最大优点是,即使我们对系统表进行了重要的修改,应用程序也可以正常地使用这些视图进行访问。下面的示例使用信息架构视图来工作。


        private void GetTables_INFORMATION_SCHEMA()
        {
            //打开连接
            string strConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
            sqlcn=new SqlConnection(strConnectionString);
            sqlcn.Open();
            //使用信息架构视图
            SqlCommand sqlcmd=new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'",sqlcn);
            SqlDataReader dr=sqlcmd.ExecuteReader();
            while(dr.Read())
            {
                MessageBox.Show(dr.GetString(0));
            }
        }

5、使用系统表

如果您的数据库系统是sqlserver,就可以使用如下的方式来获得当前数据库的所有表:

        private void GetTables_SystemTable()
        {
            //打开连接
            string strConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
            sqlcn=new SqlConnection(strConnectionString);
            sqlcn.Open();
            //使用信息架构视图
            SqlCommand sqlcmd=new SqlCommand("SELECT OBJECT_NAME (id) FROM sysobjects WHERE xtype = 'U' AND OBJECTPROPERTY (id, 'IsMSShipped') = 0",sqlcn);
            SqlDataReader dr=sqlcmd.ExecuteReader();
            while(dr.Read())
            {
                MessageBox.Show(dr.GetString(0));
            }
        }

6、使用sqlserver的存储过程“sp_tables”
下面是“盛国军”朋友提出的使用存储过程的方法的补充代码。

        public void GetTables_StoredProcedure()
        {
            //处理OleDbConnection
            string strConnectionString=@"Integrated Security=SSPI;Data Source=HBXP;Initial Catalog=Test;Provider=SQLOLEDB.1";
            OleDbConnection cn=new OleDbConnection(strConnectionString);
            cn.Open();
            //执行存储过程
            OleDbCommand cmd=new OleDbCommand("sp_tables",cn);
            cmd.CommandType=CommandType.StoredProcedure;
            OleDbDataReader dr=cmd.ExecuteReader();
            while(dr.Read())
            {
                MessageBox.Show(dr["TABLE_NAME"].ToString());
            }
        }

获取数据库名称的方法

以下示例返回当前数据库的名称。

SELECT DB_NAME() AS [Current Database];
GO
B. 返回指定数据库 ID 的数据库名称

以下示例返回数据库 ID 3 的数据库名称。

USE master;
GO
SELECT DB_NAME(3)AS [Database Name];
GO