在Delphi中使用DBExpress创建数据库? [英] Creating a Database using DBExpress in Delphi?
问题描述
我需要使用DBExpress以编程方式创建Firebird数据库。我已经在SQL Server上做到了这一点,首先连接到Master,然后将Create脚本传递给查询,但是使用Firebird时,我遇到了一些小问题。
I need to create a Firebird Database programmatically using DBExpress. I have done this for SQL server, by first connecting to Master, then passing in the script for Create to a query, but with Firebird I have a little chicken and egg problem.
推荐答案
我从一个同事那里得到了一个很好的建议,该同事为Freepascal项目创建了一些代码。它没有使用DB Express,但是据他介绍,这是用代码创建数据库的唯一方法。该代码基于InterBase手册,并使用来自gdslib / fbclient dll的调用:
I got a good tip from a collegue that created some code for the Freepascal project. It doesn't use DB express, but according to him it is the only way to create a database with code. This code is based on the InterBase manual, and uses a call from the gdslib / fbclient dll:
procedure TIBConnection.CreateDB;
var ASQLDatabaseHandle,
ASQLTransactionHandle : pointer;
CreateSQL : String;
pagesize : String;
begin
CheckDisConnected;
{$IfDef LinkDynamically}
InitialiseIBase60;
{$EndIf}
ASQLDatabaseHandle := nil;
ASQLTransactionHandle := nil;
CreateSQL := 'CREATE DATABASE ';
if HostName <> '' then
CreateSQL := CreateSQL + ''''+ HostName+':'+DatabaseName + ''''
else
CreateSQL := CreateSQL + '''' + DatabaseName + '''';
if UserName <> '' then
CreateSQL := CreateSQL + ' USER ''' + Username + '''';
if Password <> '' then
CreateSQL := CreateSQL + ' PASSWORD ''' + Password + '''';
pagesize := params.Values['PAGE_SIZE'];
if pagesize <> '' then
CreateSQL := CreateSQL + ' PAGE_SIZE '+pagesize;
if isc_dsql_execute_immediate(@FStatus[0],@ASQLDatabaseHandle,@ASQLTransactionHandle,length(CreateSQL),@CreateSQL[1],Dialect,nil) <> 0 then
CheckError('CreateDB', FStatus);
if isc_detach_database(@FStatus[0], @ASQLDatabaseHandle) <> 0 then
CheckError('CreateDB', FStatus);
{$IfDef LinkDynamically}
ReleaseIBase60;
{$EndIf}
end;
窍门是isc_dsql_execute_immediate函数。希望这段代码对您有所帮助。以下是此代码来自的Freepascal源文件的链接:
The trick is the isc_dsql_execute_immediate function. I hope this code helps you. Here are the links to the Freepascal source files where this code comes from:
包含API调用isc_dsql_execute_immediate的单元
这篇关于在Delphi中使用DBExpress创建数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!