没有用过你说的Sql-dmo连接sql-server,我使用如下的方法,其中文件Datapath中的内容为:
Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=SQLDBTest;Data Source=SQL_Server
你需要更改DataPath文件中的信息为你自己的SQL Server信息
该例的示例代码下载地址:http://wolfsoft.nugoo.com/srcdetail.asp?flag=2&id=3
单元的内容为:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, ADODB;
type
TDataModule2 = class(TDataModule)
ADOConn: TADOConnection;
ads_Test: TADODataSet;
cmd_Test: TADOCommand;
procedure DataModuleCreate(Sender: TObject);
private
{ Private declarations }
FMacineName: string;
FDBName: string;
isServer:Boolean;
public
{ Public declarations }
end;
var
DataModule2: TDataModule2;
implementation
{$R *.DFM}
procedure TDataModule2.DataModuleCreate(Sender: TObject);
procedure ExtractStr(const ConStr: string);
var
s: string;
mPos: Integer;
begin
s := Constr + ';';
with TStringlist.Create do
begin
try
while Pos(';', s) > 0 do
begin
mPos := Pos(';', s);
Add(Copy(s, 1, mPos - 1));
System.Delete(s, 1, mPos);
end;
FMacineName := Values['Data Source'];
FDBName := Values['Initial Catalog'];
finally
Free;
end;
end;
end;
function DBExists: Boolean;
begin
with ads_Test do
begin
Close;
CommandText := 'SELECT * FROM sysdatabases WHERE name = ''' + FDBName + '''';
Open;
if isEmpty then
Result := False
else
Result := True;
end;
end;
function CreateSQLServerDB: Boolean;
begin
cmd_Test.CommandText := 'Create database ' + FDBName;
try
cmd_Test.Execute;
Result := True;
except
Result := False;
end;
end;
var
TableSt: TStrings;
sfile: string;
begin
SCreen.Cursor := crHourGlass;
try
TableSt := TStringList.Create;
Sfile := ExtractFilePath(ParamStr(0)) + 'Doc/Datapath';
isServer := False;
if FileExists(sfile) then
begin
TableSt.LoadFromFile(Sfile);
ExtractStr(TableSt.Text);
ADOConn.ConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;' +
'User ID=sa;Initial Catalog=master;Data Source=' + FMacineName;
ADOConn.Connected := True;
if not DBExists then
if not CreateSQLServerDB then
begin
Application.MessageBox('数据库创建失败!', '系统信息', 0);
Exit;
end;
ADOConn.Connected := False;
ADOConn.ConnectionString := TableSt.Text;
isServer := True;
end
else
begin
//如果没有Datapath文件,处理自己的过程,可以连接到别的数据库
end;
try
ADOConn.Connected := True;
except
Application.MessageBox('数据库连接失败!', '系统信息', 0);
Exit;
end;
//在这里可以添加创建表格的Statement
finally
Screen.Cursor := crDefault;
end;
end;
end.