library cntsql;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
{ 关于DLL内存管理的重要注释:}
uses
SysUtils,
Classes,
SQLCnt in 'SQLCnt.pas' {frmSQLCnt},
global in 'global.pas';
{$R *.res}
exports
info,
ShowConnectionSQLServer;
begin
end.
=====================================
unit SQLCnt;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,IniFiles, DB, ADODB, ComCtrls ;
type
TfrmSQLCnt = class(TForm)
Label1: TLabel;
EdtServerAddress: TEdit;
GroupBox1: TGroupBox;
RBWinNT: TRadioButton;
RBSQLServer: TRadioButton;
EdtUserName: TEdit;
Label2: TLabel;
EdtPassword: TEdit;
Label3: TLabel;
Label4: TLabel;
CmbDBName: TComboBox;
btnTestConnect: TButton;
BtnSaveSetup: TButton;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
btnCreateDB: TButton;
BtnCancel: TButton;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
ADOCommand1: TADOCommand;
procedure BtnSaveSetupClick(Sender: TObject);
procedure RBWinNTClick(Sender: TObject);
procedure RBSQLServerClick(Sender: TObject);
procedure btnTestConnectClick(Sender: TObject);
procedure CmbDBNameDropDown(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure BtnCancelClick(Sender: TObject);
procedure btnCreateDBClick(Sender: TObject);
private
{ Private declarations }
procedure ReadSetup(const fn,SectionName:string);
procedure SaveSetup(const fn,SectionName:string);
procedure EnabledEdit(const bUsed:Boolean);
public
{ Public declarations }
end;
//function ShowConnectionSQLServer( IniSetupFile,SectionName
Char;Phandle: Thandle):Boolean;stdcall;
function ShowConnectionSQLServer(Phandle: Thandle):Boolean;stdcall;
function info(IniSetupFile,SectionName
Char):Boolean;stdcall;
var
frmSQLCnt: TfrmSQLCnt;
implementation
uses global;
{$R *.dfm}
//function ShowConnectionSQLServer(IniSetupFile,SectionName
Char;Phandle: Thandle):Boolean;stdcall;
//begin
// sIniSetupFile:=IniSetupFile;
// sSectionName :=SectionName;
// Application.Handle :=Phandle;
// Result :=False;
// with TfrmSQLCnt.Create(Application) do begin
// try
// MsgBox(sIniSetupFile);
// finally
// Free;
// end;
//
// end;
//end;
function info(IniSetupFile,SectionName
Char):Boolean;stdcall;
begin
sIniSetupFile:=IniSetupFile;
sSectionName :=SectionName;
Result :=True;
end;
function ShowConnectionSQLServer(Phandle: Thandle):Boolean;stdcall;
begin
Application.Handle :=Phandle;
Result :=False;
with TfrmSQLCnt.Create(Application) do begin
try
ShowModal;
finally
Free;
end;
end;
end;
procedure TfrmSQLCnt.ReadSetup(const fn,SectionName:string);
var
MyIni:TIniFile;
begin
MyIni:=TIniFile.Create(fn);
try
EdtServerAddress.Text :=MyIni.ReadString(SectionName,'SERVERIP','127.0.0.1');
RBWinNT.Checked :=MyIni.ReadBool(SectionName,'NTLogin',true);
EnabledEdit( not RBWinNT.Checked);
RBSQLServer.Checked :=MyIni.ReadBool(SectionName,'SQLLogin',False);
EnabledEdit( RBSQLServer.Checked);
EdtUserName.Text :=MyIni.ReadString(SectionName,'UserName','sa');
EdtPassword.Text :=Decc(MyIni.ReadString(SectionName,'Password',''));
CmbDBName.Text :=MyIni.ReadString(SectionName,'DataBaseName','');
finally
MyIni.Free;
end;
end;
procedure TfrmSQLCnt.SaveSetup(const fn,SectionName:string);
var
MyIni:TIniFile;
begin
MyIni:=TIniFile.Create(fn);
try
MyIni.WriteString(SectionName,'SERVERIP',EdtServerAddress.Text);
MyIni.WriteBool(SectionName,'NTLogin',RBWinNT.Checked);
MyIni.WriteBool(SectionName,'SQLLogin',RBSQLServer.Checked);
MyIni.WriteString(SectionName,'UserName',EdtUserName.Text);
MyIni.WriteString(SectionName,'Password',Encc(EdtPassword.Text));
MyIni.WriteString(SectionName,'DataBaseName',CmbDBName.Text);
finally
MyIni.Free;
end;
end;
procedure TfrmSQLCnt.BtnSaveSetupClick(Sender: TObject);
begin
SaveSetup(sIniSetupFile,sSectionName);
end;
procedure TfrmSQLCnt.RBWinNTClick(Sender: TObject);
begin
EnabledEdit(False);
end;
procedure TfrmSQLCnt.RBSQLServerClick(Sender: TObject);
begin
EnabledEdit(true);
end;
procedure TfrmSQLCnt.EnabledEdit(const bUsed: Boolean);
begin
if bUsed then begin
Label2.Enabled :=True; EdtUserName.Enabled :=True;
Label3.Enabled :=True; EdtPassword.Enabled :=True;
end else begin
Label2.Enabled :=False; EdtUserName.Enabled :=False;
Label3.Enabled :=False; EdtPassword.Enabled :=False;
end;
end;
procedure TfrmSQLCnt.btnTestConnectClick(Sender: TObject);
begin
with ADOConnection1 do begin
Close;
if RBWinNT.Checked then
ConnectionString :=format(sConnectSQLServerString2,[Trim(EdtServerAddress.Text),
Trim(CmbDBName.Text)])
else
ConnectionString :=format(sConnectSQLServerString1,[Trim(EdtPassword.Text),
Trim(EdtUserName.Text),Trim(EdtServerAddress.Text),Trim(CmbDBName.Text)]);
// MsgBox(ConnectionString);
try
Open;
Application.MessageBox('连接成功!', '成功', MB_OK + MB_ICONINFORMATION
+ MB_TOPMOST);
except
Application.MessageBox('数据服务器连接成功!', '错误', MB_OK +
MB_ICONSTOP + MB_TOPMOST);
end;
end;
end;
procedure TfrmSQLCnt.CmbDBNameDropDown(Sender: TObject);
begin
with ADOConnection1 do begin
Close;
if RBWinNT.Checked then
ConnectionString :=format(sConnectSQLServerString2,[Trim(EdtServerAddress.Text),'master'])
else
ConnectionString :=format(sConnectSQLServerString1,[Trim(EdtPassword.Text),
Trim(EdtUserName.Text),Trim(EdtServerAddress.Text),'master']);
// MsgBox(ConnectionString);
try
Open;
ADOQuery1.Close;
ADOQuery1.SQL.Text :='select [name],dbid from sysdatabases '+
'where dbid >6 '+
'order by dbid DESC';
ADOQuery1.Open;
ADOQuery1.First;
while not ADOQuery1.Eof do begin
CmbDBName.Items.Add(ADOQuery1.FieldValues['name']);
ADOQuery1.Next;
end;
ADOQuery1.Close;
except
MsgBox('数据库连接失败!',1);
end;
end;
end;
procedure TfrmSQLCnt.FormShow(Sender: TObject);
begin
ReadSetup(sIniSetupFile,sSectionName);
end;
procedure TfrmSQLCnt.BtnCancelClick(Sender: TObject);
begin
close;
end;
procedure TfrmSQLCnt.btnCreateDBClick(Sender: TObject);
var
MyStream:TFileStream;
begin
MyStream :=TFileStream.Create(ExtractFilePath(ParamStr(0))+'test.txt',0);
Memo1.Clear;
{ with ADOConnection1 do begin
Close;
if RBWinNT.Checked then
ConnectionString :=format(sConnectSQLServerString2,[Trim(EdtServerAddress.Text),'master'])
else
ConnectionString :=format(sConnectSQLServerString1,[Trim(EdtPassword.Text),
Trim(EdtUserName.Text),Trim(EdtServerAddress.Text),'master']);
// MsgBox(ConnectionString);
try
Open;
with ADOQuery1 do begin
Close;
SQL.Text :='select filename from sysdatabases where dbid =1 ';
Open;
memo1.Lines.Add('数据库路径:'+ExtractFilePath(fieldbyName('filename').AsString ));
Close;
SQL.Text :='IF EXISTS (SELECT * FROM sysdatabases WHERE name = ''数据库'') '+
'BEGIN '+
' DROP database 数据库 '+
'END; '+
'CREATE database 数据库 ';
ExecSQL;
end;
except
Application.MessageBox('失败1!', '错误', MB_OK + MB_ICONSTOP +
MB_TOPMOST);
end; }
Memo1.Lines.LoadFromStream(MyStream);
// Memo1.Lines.SaveToFile(ExtractFilePath(ParamStr(0))+'aaa.txt')
// Memo1.Lines.Add(ExtractFilePath(ParamStr(0))+'test.txt');
// Memo1.Lines.LoadFromFile(ExtractFilePath(ParamStr(0))+'test.txt');
{ try
ADOCommand1.CommandText :='use [数据库] ' +
'CREATE TABLE [tttttttttttttt] ( '+
' [fID] [int] IDENTITY (1, 1) NOT NULL ) ';
ADOCommand1.Execute;
except
Application.MessageBox('失败2!', '错误', MB_OK + MB_ICONSTOP +
MB_TOPMOST);
end;
end; }
// if not ADOConnection1.Connected then ADOConnection1.Open;
end;
end.
==========================