怎么用Delphi实现创建一个SQL数据库的功能?(100分)

  • 主题发起人 主题发起人 mrzj
  • 开始时间 开始时间
M

mrzj

Unregistered / Unconfirmed
GUEST, unregistred user!
  用Delphi创建SQL库中一个表很容易实现,怎样实现创建一个SQL库呢?
 
用CREATE DATABASE
1。通过BDE动态连接
-------------------------
// 动态连接数据库 (SQL from DBE)
// 参数描述: DyncData:连接的数据库的对象
// sComputer:服务器名称 ,sUser:连接用户名
// sPassword:连接用密码,
// sDataBaseName:别名要指定的 DyncData.Databasename

function DynConnectDatabase(var DyncData: TDataBase;
sComputer,sUser,sPassword,sDataBase,sDataBaseName: String): Boolean;
var
ServerName:String;
mDatabase:String;
muser,mPassWord:String;
begin
DyncData.Close;
ServerName:='SERVER NAME='+sComputer;
mDataBase:='DATABASE NAME='+sDataBase;
mUser:='USER NAME='+sUser;
mPassWord:='PASSWORD='+'';
with Dyncdata.Params do
begin
Clear;
Add(ServerName);
Add(mDatabase);
Add(mUser);
Add('OPEN MODE=READ/WRITE');
Add('SCHEMA CACHE SIZE=8');
Add('BLOB EDIT LOGGING=');
Add('LANGDRIVER=');
Add('SQLQRYMODE=');
Add('SQLPASSTHRU MODE=SHARED AUTOCOMMIT');
Add('DATE MODE=0');
Add('SCHEMA CACHE TIME=-1');
Add('MAX QUERY TIME=300');
Add('MAX ROWS=-1');
Add('BATCH COUNT=200');
Add('ENABLE SCHEMA CACHE=FALSE');
Add('SCHEMA CACHE DIR=');
Add('HOST NAME=');
Add('APPLICATION NAME=');
Add('NATIONAL LANG NAME=');
Add('ENABLE BCD=FALSE');
Add('TDS PACKET SIZE=4096');
Add('BLOBS TO CACHE=64');
Add('BLOB SIZE=32');
Add(mPassWord);
end;
With DyncData do
begin
LoginPrompt:=False;
DataBaseName:=sDataBaseName;
DriverName:='MSSQL';
try
Application.ProcessMessages;
Open;
Result:=True;
except
Application.MessageBox('无法连接到数据库服务器!','系统提示',
MB_OK +MB_ICONSTOP+MB_DEFBUTTON1);
Result:=False;
end;
end;
end;
2。创建数据库
--------------------------------------------------------------------
//生成数据库
//里面包含判断SQL是否存在同名数据库的问题(这里不详细描述)
//Function SQLCreate(const sName:String):string; 生成数据库的SQL
//Function CreateDatabase(const sname:string;ntype:integer):Boolean; 生成数据库

function SQLCreate(const sName: String): string;
var
mSQL:String;
begin
mSQL:='CREATE DATABASE ['+sName+']'+#13
+' ON PRIMARY (NAME = N'''+sName+'_Data'','+#13
+' FILENAME = N'''+'C:/'+sname+'_Data.MDF'','+#13
+' SIZE = 1, FILEGROWTH = 10%)'+#13
+' LOG ON (NAME = N'''+sname+'_Log'','+#13
+' FILENAME = N'''+'C:/'+sname+'_Log.LDF'','+#13
+' SIZE = 1,'+#13
+' FILEGROWTH = 10%);'+#13;

mSQL:=mSQL+#13+
'exec sp_helpreplicationdboption ;'+#13;

mSQL:=mSQL+#13+
'exec sp_MSdbuseraccess N''perm'' , N'''+sname+''';'+#13;

mSQL:=mSQL+#13+
'exec sp_MSdbuseraccess N''db'', N'''+sname+''';'+#13;
mSQL:=mSQL+#13+
'exec sp_MSdbuseraccess N''db'', N''%'';'+#13;
mSQL:=mSQL+#13+
'select name, DATABASEPROPERTY(name, N''IsDetached''), '+
'DATABASEPROPERTY(name, N''IsShutdown''),'+
' DATABASEPROPERTY(name, N''IsSuspect''),'+
' DATABASEPROPERTY(name, N''IsOffline''),'+
'DATABASEPROPERTY(name, N''IsInLoad''), ' +
'DATABASEPROPERTY(name, N''IsInRecovery''),'+
' DATABASEPROPERTY(name, N''IsNotRecovered''),'+
' DATABASEPROPERTY(name, N''IsEmergencyMode''),'+
' DATABASEPROPERTY(name, N''IsInStandBy''),'+
' status, category, status2 from master..sysdatabases;'+#13;

Result:=MSQL;
end;

function Tfrm_DataBaseini.CreateDatabase(const sname: string;ntype:integer): Boolean;
begin

with Query1 do //这里的query的DATABASENAME要指定由动态连接产生的连接
begin
Close;
SQL.Text:=SQLCreate(sname);
try
ExecSQL;
if CreateTable(sname,ntype) then
Result:=True
else
begin
Query1.Close;
Query1.SQL.Text:='Drop database ['+sname+']';
Query1.ExecSQL;
end;
except
result:=False;
end;
end;
end;

-----------------------------------------------------------
 
将创建设备的语句和创建数据库的语名放在SQL的语句里!
 
多人接受答案了。
 
后退
顶部