unit mainF;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ExtCtrls, StdCtrls, Buttons, Grids, DBGrids, DB,Registry;
type
Tmain = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure createdatabase;
function checked(users,pass:string):boolean;
var
main: Tmain;
implementation
{$R *.dfm}
uses datamodule2F,loginF;
procedure Tmain.FormCreate(Sender: TObject);
var
myreg:Tregistry;
str:string;
flag:boolean;
begin
myreg:=Tregistry.Create;
myreg.RootKey :=HKEY_LOCAL_MACHINE;
try
if myreg.OpenKey(‘\software\microsoft\microsoft SQL Server‘,false) then
begin
flag:=true;
end
else
flag:=false;
myreg.Free;
if flag=true then
begin
try
with datamodule1 do
begin
adocon.Close;
adocon.LoginPrompt :=false;
//adocon.ConnectionString:=‘Provider=SQLOLEDB.1;Persist Security Info=False;user ID=sa;Initial Catalog=master‘;
adocon.ConnectionString :=‘Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master‘;
adocon.KeepConnection :=true;
adocon.Connected :=true;
adocon.Open;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add(‘select name from sysdatabases where name=xcstationinfo‘) ;
adoquery1.Open;
end;
except
showmessage(‘连接数据库失败,‘+chr(13)+chr(13)+‘请核实是否已正确安装 MS SQL Server 2000!‘);
application.Terminate ;
end;
if datamodule1.adoquery1.IsEmpty then
begin
createdatabase;
login:=Tlogin.Create(nil);
login.ShowModal ;
end
else
begin
login:=Tlogin.Create(nil);
login.ShowModal ;
end;
datamodule1.ADOCon.Close;
datamodule1.ADOCon.LoginPrompt:=false;
datamodule1.ADOCon.ConnectionString :=‘Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=xcstationinfo‘;
datamodule1.ADOCon.KeepConnection :=true;
datamodule1.ADOCon.Connected:=true;
datamodule1.ADOCon.Open;
if login.ShowModal=mrok then //check user
begin
if checked(login.Edit1.Text ,login.Edit2.Text ) then
begin
datamodule1.ADOTable1.TableName :=‘tbluser‘;
datamodule1.ADOTable1.Open;
datamodule1.ADOTable1.Locate(‘username;password‘,vararrayof([login.Edit1.Text ,login.Edit2.Text ]),[]);
str:= datamodule1.ADOTable1.fieldbyname(‘flag‘).Value ;
if str=‘超级用户‘ then
begin
tabsheet1.Enabled :=true;
tabsheet2.Enabled :=true;
tabsheet4.Enabled :=true;
end
else if str=‘操作员‘ then
begin
tabsheet1.Enabled :=true;
tabsheet2.Enabled :=true;
tabsheet4.Enabled :=false;
end;
end
else
begin
showmessage(‘用户名或密码错误!‘);
tabsheet1.Enabled :=false;
tabsheet2.Enabled :=false;
tabsheet4.Enabled :=false;
end;
end
else
application.Terminate;
end
else
begin
showmessage(‘SQL Server 2000还没有安装,请安装后再装此软件!‘);
application.Terminate;
end;
finally
myreg.Free;
end;
end;
procedure createdatabase;
var
Myreg : TRegistry;
SQL_path : string;
s_sql : string;
begin
Myreg := TRegistry.create;
Myreg.rootkey := HKEY_LOCAL_MACHINE;
try
if Myreg.openkey(‘\SoftWare\Microsoft\MSSQLServer\Setup‘,False) then
begin
SQL_path := myreg.ReadString(‘SQLPath‘);
SQL_path := SQL_path + ‘\data‘;
end;
finally
myreg.free;
end;
//新建一数据库 student
s_sql := ‘create database xcstationinfo on (name=‘‘xcstationinfo_dat‘‘,filename=‘‘‘+SQL_path+‘\xcstationinfo_dat.mdf‘+‘‘‘,size= 5, maxsize = unlimited, filegrowth = 2) log on (name=‘‘xcstationinfo_log‘‘,filename=‘‘‘+SQL_path+‘\xcstationinfo_log.ldf‘+‘‘‘,size = 2MB, maxsize = unlimited, filegrowth = 2MB)‘;
with datamodule1.ADOCreate do
begin
commandtext := ‘‘;
commandtext := s_sql;
Execute;
end;
//恢复数据库(wifh Replace)
with datamodule1.ADOCreate do
begin
commandtext := ‘‘;
commandtext := ‘Restore database xcstationinfo from disk=:path with replace‘;
parameters.ParamByName(‘path‘).value := extractfilepath(application.ExeName )+‘data\dd1‘;
Execute;
end;
end;
function checked(users,pass:string):boolean;
begin
with datamodule1.ADOTable1 do
begin
TableName :=‘tbluser‘;
Active :=true;
if Locate (‘username;password‘,vararrayof([users,pass]),[]) then
checked:=true
else
checked:=false;
close;
end;
end;
end.
///////////////////////////////////////////////////
如何用delphi语句实现,检测该机是否安装sql server?
/////////////////////////////////////////
一般来说,计算机里面都有‘ntwdblib.dll‘链接库,里面有一个枚举函数,可以返回局域网安装SQL Server的计算机列表。我以前写了一个单元,用于检测局域网已安装SQL Server所有的计算机,或者某台计算机是否安装SQLServer,你可以试一试:先用GetComputerName获取本机计算机名,然后将计算机名传递给SQLServerAvailable。
源代码如下:
unit SQLAPI;
interface
uses Windows, Classes, Sysutils, Dialogs;
type UShort = Word;
type PShort = ^Short;
type LPShort = PShort;
type PUShort = ^UShort;
type LPUShort = PUShort;
const LOC_SEARCH = $0001;
const NET_SEARCH = $0002;
const ENUM_SUCCESS = $0000;
const MORE_DATA = $0001;
const NET_NOT_AVAIL = $0002;
const OUT_OF_MEMORY = $0004;
const NOT_SUPPORTED = $0008;
const ENUM_INVALID_PARAM = $0010;
function SQLServerAvailable(const ServerName: string): Boolean;
function GetSQLServerNames(const AList: TStrings): Integer;
implementation
function GetSQLServerNames(const AList: TStrings): Integer;
var Entries: UShort;
Buffer: array[0..4095] of Char; //4K缓冲区,我想足够了
nLoop: Integer;
pServer: PChar;
hInst: HModule;
_dbserverenum: function(SearchMode: UShort; szBuff: PChar;
cbSizeBuff: UShort; nEntries: LPUShort): Integer; stdcall;
begin
Result := NOT_SUPPORTED;
hInst := LoadLibrary(‘ntwdblib.dll‘);
if hInst <> 0 then
begin
Entries := Length(Buffer);
ZeroMemory(@Buffer, Entries);
try
@_dbserverenum := GetProcAddress(hInst, ‘dbserverenum‘);
if Assigned(_dbserverenum) then
Result := _dbserverenum(NET_SEARCH or LOC_SEARCH, Buffer, Entries, @Entries);
finally
FreeLibrary(hInst);
end;
if (Result = ENUM_SUCCESS) and (Entries > 0) then
begin
pServer := Buffer;
for nLoop := 1 to Entries do
begin
AList.Add(UpperCase(pServer));
Inc(pServer, StrLen(pServer) + 1);
end;
end else Result := NOT_SUPPORTED;
end else MessageBox(GetDesktopWindow, ‘可能没有安装SQL Server客户端程序‘, ‘错误提示‘ ,
MB_OK or MB_ICONWARNING or MB_TOPMOST or MB_SETFOREGROUND);
end;
function SQLServerAvailable(const ServerName: string): Boolean;
var AList: TStringList;
AServer: String;
begin
AServer := UpperCase(ServerName);
AList := TStringList.Create;
Result := GetSQLServerNames(AList) = ENUM_SUCCESS;
if Result then
try
Result := AList.IndexOf(AServer) <> -1;
finally
FreeAndNil(AList);
end;
end;
//////////////////////////////////////////////
你可以检测注册表信息。 HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\MICROSOFT SQL SERVER\80\Registration 下的CD_KEY 是否为空!
Procedure test_sql;
var
reg:Tregistry;
cdkey:String;
begin
reg:=Tregistry.Create;
reg.RootKey:=HKEY_LOCAL_MACHINE;
try
reg.OpenKey(‘SOFTWARE\MICROSOFT\MICROSOFT SQL SERVER\80\Registration‘,false);
cdkey:=reg.ReadString(‘CD_KEY‘);
reg.CloseKey;
if cdkey=NULL then showMessage(‘未安装SQL SERVER!‘)
else showMessage(‘安装了SQL SERVER!‘) ;
except
showMessage(‘未安装SQL SERVER!‘);
end;
reg.Free;
end;
另外,你还可以通过启动SQL SERVER可检测,我们知道,SQL SERVER下的当前用户下必有一个叫MASTER的DATABASE,我们可以通过一个ADOCONNECTION来测试是否能够连接上该DB。在这之前必须启动SQL SERVER,可以用下列语句实现:WinExec(‘net start mssqlserver‘,0);
启动SERVER后,还不能ADOCONNECTION打开出现异常,我们就可以认为是没有SQL SERVER。
建议使用第一种方式。