如何使用c#获取服务器实例中的所有数据库 [英] how to get all databases in a server instance using c#

查看:55
本文介绍了如何使用c#获取服务器实例中的所有数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c#

使用(foreach)获取服务器实例中的所有数据库



谢谢

how to get all databases in a server instance using c#
using (foreach)

thanks

推荐答案

你看过谷歌吗?我相信你没有,否则你现在就回答。



无论如何,看看这个:



http://www.geekpedia.com/KB42_How -to-get-a-list-of-SQL-Server-databases.html [ ^ ]
Did you look at Google? I am sure you did not, else you would had answer by now.

Anyway, look at this:

http://www.geekpedia.com/KB42_How-to-get-a-list-of-SQL-Server-databases.html[^]


尝试

http://msdn.microsoft.com/en-us/library/a6t1z9x2.aspx [ ^ ]

http://www.c-sharpcorner.com/ UploadFile / iersoy / get-all-instances-of-sql-server-in-C-Sharp / [ ^ ]

http://www.ehsanenaloo.com/index.php/200/programming/csharp/how -to-find-all-sql-server-instance-running-in-local-network-c.html [ ^ ]

http://www.emrekurubas.com/blog/?p=147 [ ^ ]
Try
http://msdn.microsoft.com/en-us/library/a6t1z9x2.aspx[^]
http://www.c-sharpcorner.com/UploadFile/iersoy/get-all-instances-of-sql-server-in-C-Sharp/[^]
http://www.ehsanenaloo.com/index.php/200/programming/csharp/how-to-find-all-sql-server-instance-running-in-local-network-c.html[^]
http://www.emrekurubas.com/blog/?p=147[^]


hii,

以下命令将给出数据库列表

以下所有存储过程列出服务器上的所有数据库。



我个人使用EXEC sp_databases,因为它给出了与其他结果相同的结果,但它是自我解释的。

---- SQL SERVER 2005系统程序

EXEC sp_databases

EXEC sp_helpdb

---- SQL 2000方法仍然有效在SQL Server 2005中

SELECT name

FROM sys.databases

SELECT name

FROM sys.sysdatabases

---- SQL SERVER未记录的程序

EXEC sp_msForEachDB'PRINT''?'''



只需使用ado.net调用任何一个并获取如下代码的数据



System.Data.SqlClient.SqlConnection SqlCon = new System.Data.SqlClient.SqlConnection(server = 192.168.0.1; uid = sa; pwd = 1234);

SqlCon.Open();



System.Data.SqlClient .SqlCommand SqlCom = new System.Data.SqlClient.SqlCommand();

SqlCom.Connection = SqlCon;

SqlCom.CommandType = CommandType.StoredProcedure;

SqlCom.CommandText =sp_databases;



System.Data.SqlClient.SqlDataReader SqlDR;

SqlDR = SqlCom.ExecuteReader( );



while(SqlDR.Read())

{

MessageBox.Show(SqlDR.GetString(0));

}
hii,
following commands will give database list
All the following Stored Procedure list all the Databases on Server.

I personally use EXEC sp_databases because it gives the same results as other but it is self explaining.
----SQL SERVER 2005 System Procedures
EXEC sp_databases
EXEC sp_helpdb
----SQL 2000 Method still works in SQL Server 2005
SELECT name
FROM sys.databases
SELECT name
FROM sys.sysdatabases
----SQL SERVER Un-Documented Procedure
EXEC sp_msForEachDB 'PRINT ''?'''

just call any one with ado.net and get data like below code

System.Data.SqlClient.SqlConnection SqlCon = new System.Data.SqlClient.SqlConnection("server=192.168.0.1;uid=sa;pwd=1234");
SqlCon.Open();

System.Data.SqlClient.SqlCommand SqlCom = new System.Data.SqlClient.SqlCommand();
SqlCom.Connection = SqlCon;
SqlCom.CommandType = CommandType.StoredProcedure;
SqlCom.CommandText = "sp_databases";

System.Data.SqlClient.SqlDataReader SqlDR;
SqlDR = SqlCom.ExecuteReader();

while(SqlDR.Read())
{
MessageBox.Show(SqlDR.GetString(0));
}


这篇关于如何使用c#获取服务器实例中的所有数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆