Page 1 of 1

check if the database exists

Posted: Mon 30 Oct 2006 09:46
by rhwang46
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

Posted: Mon 30 Oct 2006 13:10
by Jackson
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.

Posted: Fri 17 Nov 2006 17:19
by rhwang46
My problem is how to use SDAC in Delphi to check to see if the database exists or not. Not through using T-SQL

Posted: Mon 20 Nov 2006 08:52
by Jackson
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.