Answered by:
How to check database weather already exist with C#

Question
-
Hi, all
There is a question puzzle me.I will create a database instance,but i certain not the database weather already exist.Is anybody tell me the way to figure out the question.then such as table,field,procedure.
notes: If provider the ADO object out of box to solve the question is best way.With C# codeFriday, June 12, 2009 4:28 AM
Answers
-
The query that you would to determine if database exists before creating it is as follows
if not exists(select * from sys.databases where name = 'test') create database test
You can then use SqlConnection and SqlCommand to open a connection and then execute the above query to create the database. These following links should be able to help you with using SqlConnection and SqlCommand
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson02.aspx
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson03.aspx
For checking if table exists use this query : select * from sys.tables WHERE name = 'testtable'
For checking if store procedure exists use this query : select name FROM sysobjects where name = 'testproc' AND type = 'P'
Hope this helps
This posting is provided "AS IS" with no warranties, and confers no rights- Proposed as answer by Raj Kasi - MSFT Friday, June 12, 2009 7:11 AM
- Marked as answer by John C GordonMicrosoft employee Monday, June 15, 2009 11:21 PM
Friday, June 12, 2009 7:09 AM -
If you want to do it in C# without generating T-SQL directly, try this overview:
http://www.simple-talk.com/dotnet/.net-framework/schema-and-metadata-retrieval-using-ado.net/
Hope that helps,
John
This post is provided 'as is' and confers no express or implied warranties or rights.- Marked as answer by John C GordonMicrosoft employee Monday, June 15, 2009 11:21 PM
Monday, June 15, 2009 11:21 PM
All replies
-
The query that you would to determine if database exists before creating it is as follows
if not exists(select * from sys.databases where name = 'test') create database test
You can then use SqlConnection and SqlCommand to open a connection and then execute the above query to create the database. These following links should be able to help you with using SqlConnection and SqlCommand
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson02.aspx
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson03.aspx
For checking if table exists use this query : select * from sys.tables WHERE name = 'testtable'
For checking if store procedure exists use this query : select name FROM sysobjects where name = 'testproc' AND type = 'P'
Hope this helps
This posting is provided "AS IS" with no warranties, and confers no rights- Proposed as answer by Raj Kasi - MSFT Friday, June 12, 2009 7:11 AM
- Marked as answer by John C GordonMicrosoft employee Monday, June 15, 2009 11:21 PM
Friday, June 12, 2009 7:09 AM -
If you want to do it in C# without generating T-SQL directly, try this overview:
http://www.simple-talk.com/dotnet/.net-framework/schema-and-metadata-retrieval-using-ado.net/
Hope that helps,
John
This post is provided 'as is' and confers no express or implied warranties or rights.- Marked as answer by John C GordonMicrosoft employee Monday, June 15, 2009 11:21 PM
Monday, June 15, 2009 11:21 PM