Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

determining if an SQL database exists, etc.

Status
Not open for further replies.

thedougster

Programmer
Jan 22, 2009
56
US
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.
 
Not sure if this is what you were looking for, but here goes (untested!)

1 -
Code:
IF db_id('AdventureWorks') IS NOT NULL
Print 'Database Exists'
ELSE
Print 'Database Does Not Exists'
2 - unsure what you mean - but will assume has the current user acces rights
Code:
SELECT [Name] as DatabaseName from master.dbo.sysdatabases
WHERE ISNULL(HAS_DBACCESS ([Name]),0)=1
ORDER BY [Name]
3 -
Code:
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U'))

"I'm living so far beyond my income that we may almost be said to be living apart
 
hmckillop:

Please pardon what I'm sure is a stupid question, but I'm a newbie stumbling around in the dark:

Are these SQL queries or VB snippets? If it's VB, do you know where I can find this in C#?
 
No, these are pure T-SQL queries.

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Hello "the dougster"

There is no C# "equivalent" of these. you can execute them from c# which will involve opening a database connection to your server.

If you need to do something like that then plenty of examples on the net.

try this one for starters


Its a fairly comprehensive beginners guide.



"I'm living so far beyond my income that we may almost be said to be living apart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top