This writing is like an encyclopedia for the SQL statements and stored procedures used to enumerate, attach, and detach databases on a SQL Server instance. It lists the statements and stored procedures that you can use and discusses them in simple examples.
Introduction
Today we are going to talk about how to programmatically enumerate databases on a SQL Server instance and how you can attach and detach SQL Server databases.
Enumerating Databases on a Server
You can get a list of databases on a server using one of many ways:
* INFORMATION_SCHEMA.SCHEMATA system view (SQL Server 2000 only)
* sys.sysdatabases system table (a view in SQL Server 2005 and higher versions)
* sys.databases system view (SQL Server 2005 and higher versions)
* sys.sp_databases stored procedure
INFORMATION_SCHEMA.SCHEMATA System View
If you are using SQL Server 2000, you can query the system view INFORMATION_SCHEMA.SCHEMATA to get information about current databases on the server.
Read more: C# Corner