By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,575 Members | 1,948 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,575 IT Pros & Developers. It's quick & easy.

determining if an SQL database exists, etc.

P: 93
Could someone please give me the simplest possible C# code snippets / SQL queries to determine whether:

* a given SQL database exists,
* a given existing SQL database is accessible, and
* a given table exists within a given existing SQL database?

Thanks for whatever help anyone can provide.
Apr 12 '09 #1
Share this Question
Share on Google+
2 Replies


P: 3
List all accessible databases on server:
Expand|Select|Wrap|Line Numbers
  1. EXEC master..sp_databases
List all system tables, tables, views in current database:
Expand|Select|Wrap|Line Numbers
  1. EXEC sp_tables
List all tables with given name in specific database:
Expand|Select|Wrap|Line Numbers
  1. USE database_name
  2. EXEC sp_tables @table_name = '_user'
Apr 14 '09 #2

ck9663
Expert 2.5K+
P: 2,878
There are system tables that you can use to check if it's existing master.sysdatabases; sysobjects - for the tables...to check if it's accessible, let your front-end connection handle it. Establish your connection and just throw an error. "Accessible" is a relative. It might be there but depending on the user account, it might not be "accessible"


--- CK
Apr 15 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.