SQLConfigDataSource这个函数的具体用法(最好有例子)?(100分)

  • 主题发起人 主题发起人 windsys
  • 开始时间 开始时间
W

windsys

Unregistered / Unconfirmed
GUEST, unregistred user!
我在大富翁论坛中找到了动态设置odbc和bde的用法,我还是不太会用。谁能帮帮我啊?
我的邮箱:windsys@163.com
 
VC


SQLConfigDataSource Example
The following example uses the ::SQLConfigDataSource ODBC API function to create a new Excel data source called “New Excel Data Source”:

SQLConfigDataSource(NULL,ODBC_ADD_DSN, "Excel Files (*.xls)",
"DSN=New Excel Data Source/0"
"Description=New Excel Data Source/0"
"FileType=Excel/0"
"DataDirectory=C://EXCELDIR/0"
"MaxScanRows=20/0");

Note that the data source is actually a directory (C:/EXCELDIR); this directory must exist. The Excel driver uses directories as its data sources, and files as the individual tables (one table per .XLS file).

delphi大致如下:

SQLConfigDataSource(NiL,ODBC_ADD_DSN, 'Excel Files (*.xls)',
'DSN=New Excel Data Source'+#0 +
'Description=New Excel Data Source'+#0+
'FileType=Excel'+#0+
'DataDirectory=C:/EXCELDIR' +#0+
'MaxScanRows=20'+#0);
 
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 DSN

type
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;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部