参见下面的代码,其中有个bitbtn的代码我删了。你要的东西还在。
unit Unitlogin;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, DB, ADODB, DBCtrls;
var ConnStrFile:string;
const
ODBC_ADD_DSN = 1;
// 添加数据源
ODBC_CONFIG_DSN = 2;
// 配置数据源
ODBC_REMOVE_DSN = 3;
// 删除数据源
ODBC_ADD_SYS_DSN = 4;
// 添加系统DSN
ODBC_CONFIG_SYS_DSN = 5;
// 配置系统DSN
ODBC_REMOVE_SYS_DSN = 6;
// 删除系统DSN
type
TSQLConfigDataSource = function( hwndParent: HWND;
fRequest: WORD;
lpszDriver: LPCSTR;
lpszAttributes: LPCSTR ) : BOOL;
stdcall;
TFormlogin = class(TForm)
ADOTable1: TADOTable;
DataSource1: TDataSource;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
ComboBox1: TComboBox;
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject;
var Key: Char);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure ComboBox1Change(Sender: TObject);
private
public
end;
var
Formlogin: TFormlogin;
implementation
{$R *.dfm}
function LoadConnStr: string;
var
sltConn:TStringlist;
begin
sltConn := TStringList.Create;
if FileExists(ConnStrFile) then
sltConn.LoadFromFile(ConnStrFile)
else
sltConn.Text := '';
Result := sltConn.Text;
sltConn.free;
end;
procedure SaveConnStr ;
var
stronn:string;
sltConn:TStringlist;
begin
stronn := PromptDataSource(Application.Handle, formlogin.ADOTable1.ConnectionString);
sltConn := TStringList.Create;
sltConn.Text := stronn;
sltConn.SaveToFile(ConnStrFile);
sltConn.free;
end;
function getCurPath: string;
begin
Result := ExtractFilePath(Application.ExeName);
end;
procedure TFormlogin.FormCreate(Sender: TObject);
var
pFn: TSQLConfigDataSource;
hLib: LongWord;
strDriver: string;
strHome: string;
strAttr: string;
strFile: string;
ConnStr: string;
fResult: BOOL;
ModName: array[0..MAX_PATH] of Char;
srInfo : TSearchRec;
create_odbc:boolean;
begin
//建立ODBC数据
create_odbc:=true;
Windows.GetModuleFileName( HInstance, ModName, SizeOf(ModName) );
strHome := ModName;
while ( strHome[length(strHome)] <> '/' )do
Delete( strHome, length(strHome), 1 );
strFile := strHome + 'wxd.MDB';
// 检查Access权限(Axes = Access)
hLib := LoadLibrary('ODBCCP32');
if( hLib <> NULL ) then
begin
@pFn := GetProcAddress( hLib, 'SQLConfigDataSource' );
if( @pFn <> nil ) then
begin
// 强迫重建DSN
strDriver := 'Microsoft Access Driver (*.mdb)';
strAttr := Format( 'DSN=wxd'+#0+
'DBQ=%s'+#0+
'Exclusive=1'+#0+
'Description=wxd Data'+#0+#0,
[strFile] );
fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] );
if( fResult = false ) then
begin
ShowMessage( '创建DSN (Datasource)失败!' );create_odbc:=false;
end;
// 检测/创建同DSN关联的 MDB 文件
if( FindFirst( strFile, 0, srInfo ) <> 0 ) then
begin
strDriver := 'Microsoft Access Driver (*.mdb)';
strAttr := Format( 'DSN=wxd'+#0+
'DBQ=%s'+#0+
'Exclusive=1'+#0+
'Description=wxd Data'+#0+
'CREATE_DB="%s"'#0+#0,
[strFile,strFile] );
fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] );
if( fResult = false ) then
begin
ShowMessage( '创建MDB文件失败!' );create_odbc:=false;
end;
FindClose( srInfo );
end;
FreeLibrary( hLib );
end
else
begin
ShowMessage( '无法加载ODBCCP32.DLL' );
create_odbc:=false;
end;
if create_odbc then
begin
USER:='';
ConnStr:=getCurPath + 'wxd.mdb';
Connstr:='Provider=MSDASQL.1;Password=wanguojun_user;'
+'Persist Security Info=True;Data Source=wxd;'
+'Initial Catalog='+Connstr;
try
with self.ADOTable1do
begin
ConnectionString :=ConnStr;
open;
first;
while not eofdo
begin
self.ComboBox1.AddItem(fieldbyname('bzmc').AsString,self);
next;
end;
end;
except
messagebox(0,'数据源定义错误,请与管理员联系!','提示', mb_ok+mb_iconinformation);
application.Terminate
end
end;
end;
end;
procedure TFormlogin.BitBtn2Click(Sender: TObject);
begin
application.Terminate;
end;
end.