List of SQL-Server Databases

  • Thread starter Thread starter Matthew Smith
  • Start date Start date
M

Matthew Smith

Is it possible to get a list of SQL-Server databases if I know the
server, using C#? I want to provide a drop-down list of them.

Or is there a mechanism for selecting a database?
 
You can do this by executing the T-SQL below using a SqlCommand.

SELECT DbName = [Name] FROM SysDatabases
 
Sure. All you need is to create a connection to the sqlserver and execute
this t-sql.

SELECT CATALOG_NAME
FROM INFORMATION_SCHEMA.SCHEMATA
WHERE has_dbaccess(CATALOG_NAME) = 1

I would highly recommend using this instead of hitting the system table
(sysdatabases) directly. This statement would do 3 things for you.
1. It takes care of security issue - it only returns the name of the
database that the user has access to.
2. It's ANSI compliant.
3. It will work in the next version of sqlserver.
 
But for both the solutions you will need the sa (Sys Admin) login or a
login that has access to all the databases(which is not a good thing to
do ) ..... so that you can list all the databases on the server
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top