check if the database exists

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
rhwang46
Posts: 3
Joined: Mon 30 Oct 2006 09:38

check if the database exists

Post by rhwang46 » Mon 30 Oct 2006 09:46

I am the owner of SDAC.
Is there any method of SDAC to check if a specified Database exists on the SQL server?
The purpose of doing this is to create the Database automatically if it doesn't exist on the server

Jackson
Posts: 512
Joined: Thu 26 Jan 2006 10:06

Post by Jackson » Mon 30 Oct 2006 13:10

You can connect to master database and then use the following statement:
IF DB_ID (N'mytest') IS NULL
CREATE DATABASE mytest;
For more information please see MSDN.

rhwang46
Posts: 3
Joined: Mon 30 Oct 2006 09:38

Post by rhwang46 » Fri 17 Nov 2006 17:19

My problem is how to use SDAC in Delphi to check to see if the database exists or not. Not through using T-SQL

Jackson
Posts: 512
Joined: Thu 26 Jan 2006 10:06

Post by Jackson » Mon 20 Nov 2006 08:52

You can use something like following:

Code: Select all

MSQuery1.SQL.Text := 'SET :p1 = DB_ID (N''MyDatabase'')';
MSQuery1.ParamByName('p1').ParamType := ptInputOutput;
MSQuery1.ParamByName('p1').DataType := ftSmallInt;
MSQuery1.Execute;
if MSQuery1.ParamByName('p1').IsNull then begin
  // Database doesn't exist.
end
else
  // Database exists.

Post Reply