您的位置:首页 > 数据库

如何测试一个SQL Server ODBC驱动程序连接使用的是5

2009-11-07 10:07 429 查看
;
; IS 5.0.8
;
[Setup]
AppName=ODBCtest
AppVerName=ODBCtest
Uninstallable=false
UpdateUninstallLogAppName=false
DisableDirPage=false
DisableProgramGroupPage=true
DefaultDirName={pf}ODBCtest
DisableStartupPrompt=true
CreateAppDir=false
OutputBaseFilename=TestODBC
Compression=bzip
AllowUNCPath=false
ShowLanguageDialog=yes
[Code]
// include const and function declarations
#include "odbclib.iss"
var
// variables
myserver, myuser, mypwd: String;

// Parameters for SQL Server
// DRIVER={SQL Server}
// SERVER=servername
// UID=username
// PWD=password
// DATABASE=dbname (optional)
// WSID=workstation id (optional usually computer name)
// APP=application (optional)
// NETWORK=Dbmssocn (optional)
// ADDRESS=server,port (optional)
{ Protocols
TCP/IP Windows Sockets Dbmssocn.dll
Named pipes Dbnmpntw.dll
Multiprotocol Dbmsrpcn.dll
Novell SPX/IPX Dbmsspxn.dll
Banyan Vines Dbmsvinn.dll
DECNet Dbmsdecn.dll
AppleTalk Dbmsadsn.dll
}
//
// connect to server using usr and pwd
//
function ConnectServer( server, usr, pwd : String ) : Boolean;
var envH :SQLHANDLE ; // env handle
conH :SQLHANDLE; // connection handle
sqlret : SQLRETURN;
hwnd : LongInt; // window wizard handle
constrIN, constrOUT: String;
sizeout : SQLSMALLINT;
msg: String;
begin
hwnd := StrToInt(ExpandConstant('{wizardhwnd}'));
envH := SQL_NULL_HENV;
conH := SQL_NULL_HDBC;
Result := true;
sqlret := SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, envH );
if sqlret = SQL_SUCCESS then
begin
sqlret := SQLSetEnvAttr( envH, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3, 0 );
if sqlret = SQL_SUCCESS then
begin
sqlret := SQLAllocHandle( SQL_HANDLE_DBC, envH, conH );
if sqlret = SQL_SUCCESS then
begin
constrIN := 'DRIVER={SQL Server};SERVER='+server+';UID='+usr+';PWD='+pwd+';APP=Inno Setup Test;';
// if it's necessary connect via tcp/ip
// constrIN := constrIN + 'NETWORK=Dbmssocn;ADDRESS='+server+',1433';
constrOUT := StringOfChar( ' ', BUFSIZE );
// connessione
sqlret := SQLDriverConnect( conH,
hwnd,
constrIN,
Length(constrIN),
constrOUT,
BUFSIZE - 1,
sizeout,
SQL_DRIVER_NOPROMPT );
constrOUT := CastIntegerToString( CastStringToInteger(constrOUT) );
msg := #13#10 + #13#10 + GetODBCMsg( SQL_HANDLE_DBC, conH );
if IsSQLRetOk( sqlret ) then
begin
MsgBox( 'Connection string is: ' + #13#10 + #13#10 + constrOUT + msg, mbInformation ,MB_OK );
sqlret := SQLDisconnect( conH );
end
else begin
MsgBox( 'Connection failed!' + #13#10 + #13#10 + constrIN + msg, mbError , MB_OK );
sqlret := SQLFreeHandle( SQL_HANDLE_DBC, conH );
Result := false;
end;
end
else begin
msg := GetODBCMsg( SQL_HANDLE_DBC, conH );
MsgBox( 'Error creating CON handle:' + #13#10 + #13#10 + msg, mbError , MB_OK );
Result := false;
end;
end
else begin
msg := GetODBCMsg( SQL_HANDLE_ENV, envH );
MsgBox( 'Error setting ODBC version:' + #13#10 + #13#10 + msg, mbError , MB_OK );
Result := false;
end
// anyway release ENV handle
sqlret := SQLFreeHandle( SQL_HANDLE_ENV, envH );
end
else begin
msg := GetODBCMsg( SQL_HANDLE_ENV, envH );
MsgBox( 'Error creating ENV handle:' + #13#10 + #13#10 + msg, mbError , MB_OK );
Result := false;
end;
end;
// function by Dicho
// http://www.vincenzo.net/isxkb/modules.php?name=News&file=article&sid=96
//
procedure SetControlCursor(control: TWinControl; cursor: TCursor);
var i:Integer;
wc: TWinControl;
begin
if (not (control = nil)) then begin
control.Cursor := cursor;
try
for i:=0 to control.ControlCount-1 do begin
wc := TWinControl(control.Controls[i]);
if (NOT(wc = nil)) then
SetControlCursor(wc, cursor)
else
control.Controls[i].Cursor := cursor;
end; {for}
finally
end;{try}
end;{if}
end;{procedure SetControlCursor}

#include "SrvUsrPwd.isf"
procedure InitializeWizard();
begin
LoginODBC_CreatePage(wpWelcome);
end;

==========================================
form SrvUsrPwd.isf

[CustomMessages]
LoginODBCCaption=Login ODBC Caption
LoginODBCDescription=Login ODBC description
[Code]
var
lbUsername: TLabel;
lbServer: TLabel;
lbPassword: TLabel;
edUsername: TEdit;
edServer: TEdit;
edPassword: TEdit;
butTest: TButton;

procedure TestOnClick(Sender: TObject);
begin
WizardForm.Enabled := false;
myserver := edServer.Text;
myuser := edUsername.Text;
mypwd := edPassword.Text;

SetControlCursor(WizardForm, crHourGlass);
ConnectServer( myserver, myuser , mypwd );
SetControlCursor(WizardForm, crDefault);

WizardForm.Enabled := true;
end;
procedure LoginODBC_Activate(Page: TWizardPage);
begin
end;
function LoginODBC_ShouldSkipPage(Page: TWizardPage): Boolean;
begin
Result := False;
end;
function LoginODBC_BackButtonClick(Page: TWizardPage): Boolean;
begin
Result := True;
end;
function LoginODBC_NextButtonClick(Page: TWizardPage): Boolean;
begin
Result := True;
end;
procedure LoginODBC_CancelButtonClick(Page: TWizardPage; var Cancel, Confirm: Boolean);
begin
end;
function LoginODBC_CreatePage(PreviousPageId: Integer): Integer;
var
Page: TWizardPage;
begin
Page := CreateCustomPage(
PreviousPageId,
ExpandConstant('{cm:LoginODBCCaption}'),
ExpandConstant('{cm:LoginODBCDescription}')
);
{ lbUsername }
lbUsername := TLabel.Create(Page);
with lbUsername do
begin
Parent := Page.Surface;
Left := ScaleX(32);
Top := ScaleY(80);
Width := ScaleX(48);
Height := ScaleY(13);
Caption := 'Username';
FocusControl := edUsername;
end;
{ lbServer }
lbServer := TLabel.Create(Page);
with lbServer do
begin
Parent := Page.Surface;
Left := ScaleX(32);
Top := ScaleY(16);
Width := ScaleX(32);
Height := ScaleY(13);
Caption := 'Server';
FocusControl := edServer;
end;
{ lbPassword }
lbPassword := TLabel.Create(Page);
with lbPassword do
begin
Parent := Page.Surface;
Left := ScaleX(32);
Top := ScaleY(144);
Width := ScaleX(46);
Height := ScaleY(13);
Caption := 'Password';
FocusControl := edPassword;
end;
{ edUsername }
edUsername := TEdit.Create(Page);
with edUsername do
begin
Parent := Page.Surface;
Left := ScaleX(32);
Top := ScaleY(96);
Width := ScaleX(325);
Height := ScaleY(21);
AutoSize := False;
TabOrder := 1;
Text := 'sa';
end;
{ edServer }
edServer := TEdit.Create(Page);
with edServer do
begin
Parent := Page.Surface;
Left := ScaleX(32);
Top := ScaleY(32);
Width := ScaleX(329);
Height := ScaleY(21);
AutoSize := False;
TabOrder := 0;
Text := 'localhost';
end;
{ edPassword }
edPassword := TEdit.Create(Page);
with edPassword do
begin
Parent := Page.Surface;
Left := ScaleX(32);
Top := ScaleY(160);
Width := ScaleX(325);
Height := ScaleY(21);
AutoSize := False;
PasswordChar := '*';
TabOrder := 2;
end;
// butTest
butTest := TButton.Create(Page);
with butTest do
begin
Parent := Page.Surface;
Left := ScaleX(288);
Top := ScaleY(192);
Width := ScaleX(73);
Height := ScaleY(25);
Caption := 'Test';
TabOrder := 3;
OnClick := @TestOnClick;
end;

with Page do
begin
OnActivate := @LoginODBC_Activate;
OnShouldSkipPage := @LoginODBC_ShouldSkipPage;
OnBackButtonClick := @LoginODBC_BackButtonClick;
OnNextButtonClick := @LoginODBC_NextButtonClick;
OnCancelButtonClick := @LoginODBC_CancelButtonClick;
end;
Result := Page.ID;
end;本文出自 “学无止境” 博客,请务必保留此出处http://dqk1985.blog.51cto.com/1005868/223467
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐