如何在SQL Server中过滤系统数据库? [英] How to filter system database in SQL server?
问题描述
我想在SQL服务器中使用枚举用户数据库,例如
从sys.databases中选择名称
返回以下内容:
master
tempdb
model
msdb
ReportServer $ SQLEXPRESS
ReportServer $ SQLEXPRESSTempDB
xsxk
MyDatabase2
前6个数据库是系统数据库,它们不是我想要的。
我只需要最后2个数据库。最后2个数据库是用户创建的。
请提供C#或SQL的源代码,以过滤系统数据库。
用户数据库名称没有规则。
我尝试过:
使用SMO :
Microsoft.SqlServer.Management.Smo.Server so = new Microsoft.SqlServer.Management.Smo.Server();
Microsoft .SqlServer.Management.Smo.DatabaseCollection dc = so.Databases;
它还返回所有数据库。
I want enum user database in SQL server, e.g.
select name from sys.databases
It returns below content:
master
tempdb
model
msdb
ReportServer$SQLEXPRESS
ReportServer$SQLEXPRESSTempDB
xsxk
MyDatabase2
The first 6 databases are system database, they are not my wanted.
I just need the last 2 databases. The last 2 databases are user created.
Please provide source code in C# or SQL, to filter system database.
The user database name has no rules.
What I have tried:
Using SMO:
Microsoft.SqlServer.Management.Smo.Server so = new Microsoft.SqlServer.Management.Smo.Server();
Microsoft.SqlServer.Management.Smo.DatabaseCollection dc = so.Databases;
It also return all databases.
推荐答案
SQLEXPRESS
ReportServer
SQLEXPRESS
ReportServer
SQLEXPRESSTempDB
xsxk
MyDatabase2
前6个数据库是系统数据库,它们不是我想要的。
我只需要最后2个数据库。最后2个数据库是用户创建的。
请提供C#或SQL的源代码,以过滤系统数据库。
用户数据库名称没有规则。
我尝试过:
使用SMO :
Microsoft.SqlServer.Management.Smo.Server so = new Microsoft.SqlServer.Management.Smo.Server();
Microsoft .SqlServer.Management.Smo.DatabaseCollection dc = so.Databases;
它还返回所有数据库。
SQLEXPRESSTempDB
xsxk
MyDatabase2
The first 6 databases are system database, they are not my wanted.
I just need the last 2 databases. The last 2 databases are user created.
Please provide source code in C# or SQL, to filter system database.
The user database name has no rules.
What I have tried:
Using SMO:
Microsoft.SqlServer.Management.Smo.Server so = new Microsoft.SqlServer.Management.Smo.Server();
Microsoft.SqlServer.Management.Smo.DatabaseCollection dc = so.Databases;
It also return all databases.
不幸的是,如果你将查询基于日期,然后tempdb
将(通常)仍然出现在您的列表中(取决于实例的管理方式)。
如果使用标准方法
Unfortunately if you base your query on a date thentempdb
will (usually) still appear in your list (depending on how the instance is being managed).
If you use the standard approach
select [name] from sys.databases where database_id > 4
然后(在你的情况下)你仍然会得到 ReportServer的条目
then (in your case) you will still get entries for ReportServer
这篇关于如何在SQL Server中过滤系统数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!