2012-01-18 87 views
0

请问任何人请告诉我如何编码数据库是否存在或不在sql azure中?检查SQL Azure中是否存在数据库

+1

Downvotes作为其对搜索谷歌第一个答案 “SQL Azure的检查数据库是否存在” – Lloyd 2012-01-18 19:14:25

+0

HTTP:/ /mattgemmell.com/2008/12/08/what-have-you-tried/ – 2012-07-26 08:13:01

回答

5

你试过查询sys.databases表吗?这应该会给你你想要的。更多信息here

注意:您需要对Master数据库运行此查询。否则,你只会看到当前数据库(和主)的名称。

+0

这是一个与Azure安全指南相关的链接,它提供了一个清晰的示例。 http://msdn.microsoft.com/en-us/library/windowsazure/ff394108.aspx – Lloyd 2012-01-18 19:13:40

+0

这意味着我们需要使用以下运行aganist master数据库的代码: – DSKVP 2012-01-18 19:31:55

+0

ServerConnection serverConnection = new ServerConnection(connection); 服务器服务器=新服务器(serverConnection); //在这一行后面,默认数据库将被切换到主数据库 Database database = server.Databases [“MyDatabase”]; //你仍然可以使用这个数据库对象和服务器连接到 //对这个数据库做某些事情,比如添加数据库角色 //和用户 DatabaseRole role = new DatabaseRole(database,“NewRole”); role.Create(); – DSKVP 2012-01-18 19:33:05

1
Select count(*) from sysobjects where name = 'testdb' returns 0 if not found 

把你的数据库的名称,我们将编辑你的脚本..所有你需要做的是复制和粘贴好..? 这里有一些额外的东西,你可以尝试以及

Method 1: Use sys.sysdatabases view 



IF EXISTS(SELECT * FROM sys.sysdatabases where [email protected]) 

PRINT“数据库exists'elsePRINT‘数据库不存在’

Method 2: Use sysdatabases system table from master database 



IF EXISTS(SELECT * FROM master..sysdatabases WHERE [email protected]) 

PRINT”数据库exists'elseprint“数据库做不存在 '

Method 3: Using of sp_msforeachdb 


--If you dont get a message, the database doesn't exist 

DECLARE @sql VARCHAR(1000)SET @ SQL = '如果 ''?' '=' '' + @ TESTDB + ''' 打印 '' 数据库中存在 '''EXEC sp_msforeachdb @ sql

Method 4: Using sp_msforeachdb with information_schema.schemata 



--If you dont get a message, the database doesn't existDECLARE @sql varchar(1000)SET @sql='if exists(select * from ?.information_schema.schemata wherecatalog_name='''[email protected] testdb+''') 

打印 '' 数据库中不存在 '''EXEC sp_msforeachdb @sql

+0

这不会工作。 – 2012-01-18 18:52:45

+0

你是什么意思,它不会工作..?你的餐桌名是什么? – MethodMan 2012-01-18 19:13:58

+0

谢谢你的回复。但我认为它检查表是否存在或不存在。但我想检查数据库的工作与否 – DSKVP 2012-01-18 19:30:34

0
if exists (select * from master.sys.databases where name = '[enter name here]') 
+0

因此,我需要使用数据库名称更改[在此输入名称]吗? – DSKVP 2012-01-18 19:34:27

+0

谢谢你的回复 – DSKVP 2012-01-19 00:04:35