Sql server 如何检查数据库是否存在?
我正在尝试使用Delphi7连接SQLServer2008中的数据库。我有一个数组Sql server 如何检查数据库是否存在?,sql-server,sql-server-2008,delphi,database-connection,delphi-7,Sql Server,Sql Server 2008,Delphi,Database Connection,Delphi 7,我正在尝试使用Delphi7连接SQLServer2008中的数据库。我有一个数组arr\u foo,其中包含数据库的名称。这就是我的脚本的样子: for i:=3 to 5 do begin sql := 'SELECT * FROM sys.databases WHERE name = '+quotedstr(arr_foo[i]); adoquery1.close; adoquery1.sql.text := sql; adoquery1.open; if not ad
arr\u foo
,其中包含数据库的名称。这就是我的脚本的样子:
for i:=3 to 5 do begin
sql := 'SELECT * FROM sys.databases WHERE name = '+quotedstr(arr_foo[i]);
adoquery1.close;
adoquery1.sql.text := sql;
adoquery1.open;
if not adoquery1.isempty then begin
adoconnection1.close;
adoconnection1.defaultdatabase := arr_foo[i];
adoconnection1.open;
end;
end;
首先,adoquery1已经连接到adoconnection1。adoconnection1已连接到//192.168.5.211(这是我的服务器)
问题是,当我在Delphi7上运行上述脚本时,它返回一个错误“属性值无效”。请确保键入的值正确并停止在“adoquery1.open”处,但当我在SQL SERVER Management Studio 2008中从sys.databases(其中name='mydb3'运行查询时,它通常返回1条记录。我的脚本有什么问题?尝试使用
DB\u ID
函数-
IF DB_ID('AdventureWorks2008R2') IS NOT NULL
PRINT 'exists'
更改对此的查询:
sql := 'SELECT ID = DB_ID(' + quotedstr(arr_foo[i]) + ')';
并检查ID值是否不为空。谢谢。这就解决了问题。由于没有其他人回答,我会将此标记为正确答案。在SQL server中,当您使用management studio登录到服务器时,您会自动连接到“主”数据库,这就是查询工作的原因。我不认为adoconnection会那样做。可能是重复的