转:(* This example shows one way to load the ODBC Administrator's DLL (ODBCCP32.DLL) to create an Access MDB file and ODBC DSN pointing at it. Note that it assumes current directory for both the DLL and the MDB, but the DLL will be found if in the WinSys directory which is where it normally is anyway. Similar operation applies to most driver types, with some modifications. eg: Access requires the MDB file to exist so you can hook the DSN to it. Note also that the "CREATE_DB" call is an Access special (MS Jet Engine) and has other variants like COMPACT_DB and REPAIR_DB. For a full list see either the Jet Engine Programmers Guide or the MSDN and search for "CREATE_DB". This was originally written in MSVC6 and was ported to Delphi 5. Full documentation can be found in the MSDN at http://msdn.microsoft.com/. *)const ODBC_ADD_DSN = 1; // Add data source ODBC_CONFIG_DSN = 2; // Configure (edit) data source ODBC_REMOVE_DSN = 3; // Remove data source ODBC_ADD_SYS_DSN = 4; // add a system DSN ODBC_CONFIG_SYS_DSN = 5; // Configure a system DSN ODBC_REMOVE_SYS_DSN = 6; // remove a system DSNtype TSQLConfigDataSource = function( hwndParent: HWND; fRequest: WORD; lpszDriver: LPCSTR; lpszAttributes: LPCSTR ) : BOOL; stdcall;procedure Form1.FormCreate(Sender: TObject); var pFn: TSQLConfigDataSource; hLib: LongWord; strDriver: string; strHome: string; strAttr: string; strFile: string; fResult: BOOL; ModName: array[0..MAX_PATH] of Char; srInfo : TSearchRec; hRegLib: HMODULE; begin Windows.GetModuleFileName( HInstance, ModName, SizeOf(ModName) ); strHome := ModName; while ( strHome[length(strHome)] <> '\' ) do Delete( strHome, length(strHome), 1 ); strFile := strHome + 'TestData.MDB'; // Test Access Rights (Axes = Access) hLib := LoadLibrary( 'ODBCCP32' ); // load from default path if( hLib <> NULL ) then begin @pFn := GetProcAddress( hLib, 'SQLConfigDataSource' ); if( @pFn <> nil ) then begin // force (re-)create DSN strDriver := 'Microsoft Access Driver (*.mdb)'; strAttr := Format( 'DSN=TestDSN'+#0+ 'DBQ=%s'+#0+ 'Exclusive=1'+#0+ 'Description=Test Data'+#0+#0, [strFile] ); fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] ); if( fResult = false ) then ShowMessage( 'Create DSN (Datasource) failed!' ); // test/create MDB file associated with DSN if( FindFirst( strFile, 0, srInfo ) <> 0 ) then begin strDriver := 'Microsoft Access Driver (*.mdb)'; strAttr := Format( 'DSN=TestDSN'+#0+ 'DBQ=%s'+#0+ 'Exclusive=1'+#0+ 'Description=Test Data'+#0+ 'CREATE_DB="%s"'#0+#0, [strFile,strFile] ); fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] ); if( fResult = false ) then ShowMessage( 'Create MDB (Database file) failed!' ); end; FindClose( srInfo ); end; (* MASSIVE SNIP *) FreeLibrary( hLib ); end else begin ShowMessage( 'Unable to load ODBCCP32.DLL' ); end; StatusClockTimer.Enabled := true; end;
兄弟,好象话说错了吧,用ADO怎么作数据库?----
var CreateAccess:OleVariant; begin CreateAccess:=CreateOleObject('ADOX.Catalog'); CreateAccess.Create('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Aceco.mdb'); CreateAccess := Unassigned; end;
呵呵,是个意思而已,表述不清,还请包含 就是用ADO连接Access数据库:)
to wisenowa(127.0.0.1) 多步OLE DB 操作错误
以前好像是看过一篇,原理大概是这样的:把空的 mdb 文件编译进 res (资源)文件中,再COPY出来的吧。但我没试过。不过我还是建议带有一个空的 MDB 文件,此文件经过一定的加密运算,存放于EXE文件同一文件夹,建立时再COPY一份,解密,完事。
动态连接Microsoft OLE db Provider FOR odbc Drivers的方法 相信你哪也可以仿照如下方法: procedure Tfrm_main.ListBox1Click(Sender: TObject); var ls_db,ls_odbc,username,password,connectedparam:string; begin ls_db := ComboBox1.Text; if ls_db = '' then begin showmessage('请选择数据库类型'); exit; end; username:=trim(ED_UserName.text); password:=trim(ed_pwd.text); ls_odbc := listbox1.Items.Strings[listbox1.ItemIndex]; //开始连接指定数据库 if CheckBox1.Checked then begin pus_connectparam:='Provider=MSDASQL.1;Persist Security Info=False;'+ 'User ID='+username+ ';Password='+password+ ';Data Source='+ls_odbc; end else begin pus_connectparam:='Provider=MSDASQL.1;Persist Security Info=False;'+ 'Data Source='+ls_odbc; username:='没有定义'; end; try with frm_db.ADOC_ODBC do begin Connected:=false; ConnectionString:=pus_connectparam; LoginPrompt:=false; Connected:=true; end; except statusbar_top.Panels[0].Text:=ComboBox1.TEXT+'数据库'+username+'用户'; statusbar_top.Panels[1].Text:='其数据源名:'+ls_odbc+'连接失败!'+Pus_CurconnectUser+'可用'; raise ERangeError.CreateFmt('数据库连接失败,请查看用户名和密码是否输错误?',[1002]); //Application.MessageBox('请查看用户名和密码是否输错?'+ // '注意:本软件ODBC功能模块暂时只支持Access 和SQLServer数据库', // '数据库连接错误',MB_OK) end; frm_db.ADOQ_ODBC.Connection:=frm_db.ADOC_ODBC; prs_tempconnectparam:=pus_connectparam;//保存全局连接参数 Pus_CurconnectUser:=username+'用户其数据源名:'+ls_odbc;//置新的当前连接用户 prs_tempconnectUser:=Pus_CurconnectUser;//保存全局连接用户 prs_DBtype:=ComboBox1.Text; statusbar_top.Panels[0].Text:=ComboBox1.TEXT+'数据库'; statusbar_top.Panels[1].Text:=Pus_CurconnectUser+'处于连接'; Memo_WrieteSQL.Visible:=true; //***************** listbox2.items.clear; frm_db.ADOC_ODBC.gettablenames(listbox2.items,false); PageControl1.SelectNextPage(true); end;
This example shows one way to load the ODBC
Administrator's DLL (ODBCCP32.DLL) to create
an Access MDB file and ODBC DSN pointing at
it. Note that it assumes current directory
for both the DLL and the MDB, but the DLL
will be found if in the WinSys directory which
is where it normally is anyway. Similar operation applies to most driver types,
with some modifications. eg: Access requires
the MDB file to exist so you can hook the DSN
to it. Note also that the "CREATE_DB" call is an Access
special (MS Jet Engine) and has other variants
like COMPACT_DB and REPAIR_DB. For a full list
see either the Jet Engine Programmers Guide or
the MSDN and search for "CREATE_DB". This was originally written in MSVC6 and was
ported to Delphi 5. Full documentation can be found in the MSDN
at http://msdn.microsoft.com/.
*)const
ODBC_ADD_DSN = 1; // Add data source
ODBC_CONFIG_DSN = 2; // Configure (edit) data source
ODBC_REMOVE_DSN = 3; // Remove data source
ODBC_ADD_SYS_DSN = 4; // add a system DSN
ODBC_CONFIG_SYS_DSN = 5; // Configure a system DSN
ODBC_REMOVE_SYS_DSN = 6; // remove a system DSNtype
TSQLConfigDataSource = function( hwndParent: HWND;
fRequest: WORD;
lpszDriver: LPCSTR;
lpszAttributes: LPCSTR ) : BOOL; stdcall;procedure Form1.FormCreate(Sender: TObject);
var
pFn: TSQLConfigDataSource;
hLib: LongWord;
strDriver: string;
strHome: string;
strAttr: string;
strFile: string;
fResult: BOOL;
ModName: array[0..MAX_PATH] of Char;
srInfo : TSearchRec;
hRegLib: HMODULE;
begin
Windows.GetModuleFileName( HInstance, ModName, SizeOf(ModName) );
strHome := ModName;
while ( strHome[length(strHome)] <> '\' ) do
Delete( strHome, length(strHome), 1 );
strFile := strHome + 'TestData.MDB'; // Test Access Rights (Axes =
Access)
hLib := LoadLibrary( 'ODBCCP32' ); // load from default path
if( hLib <> NULL ) then
begin
@pFn := GetProcAddress( hLib, 'SQLConfigDataSource' );
if( @pFn <> nil ) then
begin
// force (re-)create DSN
strDriver := 'Microsoft Access Driver (*.mdb)';
strAttr := Format( 'DSN=TestDSN'+#0+
'DBQ=%s'+#0+
'Exclusive=1'+#0+
'Description=Test Data'+#0+#0,
[strFile] );
fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] );
if( fResult = false ) then ShowMessage( 'Create DSN (Datasource)
failed!' ); // test/create MDB file associated with DSN
if( FindFirst( strFile, 0, srInfo ) <> 0 ) then
begin
strDriver := 'Microsoft Access Driver (*.mdb)';
strAttr := Format( 'DSN=TestDSN'+#0+
'DBQ=%s'+#0+
'Exclusive=1'+#0+
'Description=Test Data'+#0+
'CREATE_DB="%s"'#0+#0,
[strFile,strFile] );
fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] );
if( fResult = false ) then ShowMessage( 'Create MDB (Database file)
failed!' );
end;
FindClose( srInfo ); end; (* MASSIVE SNIP *) FreeLibrary( hLib );
end
else
begin
ShowMessage( 'Unable to load ODBCCP32.DLL' );
end;
StatusClockTimer.Enabled := true;
end;
CreateAccess:OleVariant;
begin
CreateAccess:=CreateOleObject('ADOX.Catalog');
CreateAccess.Create('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Aceco.mdb');
CreateAccess := Unassigned;
end;
就是用ADO连接Access数据库:)
多步OLE DB 操作错误
相信你哪也可以仿照如下方法:
procedure Tfrm_main.ListBox1Click(Sender: TObject);
var
ls_db,ls_odbc,username,password,connectedparam:string;
begin
ls_db := ComboBox1.Text;
if ls_db = '' then
begin
showmessage('请选择数据库类型');
exit;
end;
username:=trim(ED_UserName.text);
password:=trim(ed_pwd.text);
ls_odbc := listbox1.Items.Strings[listbox1.ItemIndex];
//开始连接指定数据库
if CheckBox1.Checked then
begin
pus_connectparam:='Provider=MSDASQL.1;Persist Security Info=False;'+
'User ID='+username+
';Password='+password+
';Data Source='+ls_odbc; end
else begin
pus_connectparam:='Provider=MSDASQL.1;Persist Security Info=False;'+
'Data Source='+ls_odbc;
username:='没有定义';
end;
try
with frm_db.ADOC_ODBC do
begin
Connected:=false;
ConnectionString:=pus_connectparam;
LoginPrompt:=false;
Connected:=true;
end;
except
statusbar_top.Panels[0].Text:=ComboBox1.TEXT+'数据库'+username+'用户';
statusbar_top.Panels[1].Text:='其数据源名:'+ls_odbc+'连接失败!'+Pus_CurconnectUser+'可用';
raise ERangeError.CreateFmt('数据库连接失败,请查看用户名和密码是否输错误?',[1002]);
//Application.MessageBox('请查看用户名和密码是否输错?'+
// '注意:本软件ODBC功能模块暂时只支持Access 和SQLServer数据库',
// '数据库连接错误',MB_OK)
end;
frm_db.ADOQ_ODBC.Connection:=frm_db.ADOC_ODBC;
prs_tempconnectparam:=pus_connectparam;//保存全局连接参数
Pus_CurconnectUser:=username+'用户其数据源名:'+ls_odbc;//置新的当前连接用户
prs_tempconnectUser:=Pus_CurconnectUser;//保存全局连接用户
prs_DBtype:=ComboBox1.Text;
statusbar_top.Panels[0].Text:=ComboBox1.TEXT+'数据库';
statusbar_top.Panels[1].Text:=Pus_CurconnectUser+'处于连接';
Memo_WrieteSQL.Visible:=true;
//*****************
listbox2.items.clear;
frm_db.ADOC_ODBC.gettablenames(listbox2.items,false);
PageControl1.SelectNextPage(true);
end;