如何调用API的函数SQLconfigdatasource,实现ODBC由程序自动创建 (100分)

  • 主题发起人 fengjingyan
  • 开始时间
F

fengjingyan

Unregistered / Unconfirmed
GUEST, unregistred user!
各位专家好,我目前遇到了难题,想在发布的应用程序中自动创建ODBC的名称,<br>知道用SQLconfigdatasource()可以实现,可又不知道该如何使用。<br>哪位高手给予指点,将不胜感激!
 
有这样的问题,查一下
 
为什么没人回答?难吗?不会吧?这里可是Delphi高手云集的地方,我急用。<br>我要送高分的!上面这位朋友,查的怎么样了?
 
interface <br>const <br>&nbsp; ODBCCP32 &nbsp;= 'ODBCCP32.dll'; <br><br>const <br>{ SQLConfigDataSource Request flags } <br>&nbsp; ODBC_ADD_DSN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 1; // Add a new user data source <br>&nbsp; ODBC_CONFIG_DSN &nbsp; &nbsp; &nbsp; &nbsp; = 2; // Configure (modify) an existing user data source <br>&nbsp; ODBC_REMOVE_DSN &nbsp; &nbsp; &nbsp; &nbsp; = 3; // Remove an existing user data source <br>&nbsp; ODBC_ADD_SYS_DSN &nbsp; &nbsp; &nbsp; &nbsp;= 4; // Add a new system data source <br>&nbsp; ODBC_CONFIG_SYS_DSN &nbsp; &nbsp; = 5; // Modify an existing system data source <br>&nbsp; ODBC_REMOVE_SYS_DSN &nbsp; &nbsp; = 6; // Remove an existing system data source <br>&nbsp; ODBC_REMOVE_DEFAULT_DSN = 7; // Remove the default data source specification &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// section from the system information, same as <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// the SQLRemoveDefaultDataSource function. <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Other params should be NULLS; if not, they &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// will be ignored. <br>{ adds, modifies, or deletes data sources } <br>function SQLConfigDataSource(hWnd: HWND; Request: LongInt; DriverType: PChar; &nbsp;<br>&nbsp; Attributes: PChar): BOOL; stdcall; <br>implementation <br>{ SQLConfigDataSource } <br>{ 功能: adds, modifies, or deletes data sources } <br>{ 输入: hWnd &nbsp; &nbsp; &nbsp; Parent window handle. The function will not display &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;any dialog boxes if the handle is null. <br>&nbsp; &nbsp; &nbsp; &nbsp; Request &nbsp; &nbsp;Type of request <br>&nbsp; &nbsp; &nbsp; &nbsp; DriverType Driver description <br>&nbsp; &nbsp; &nbsp; &nbsp; Attributes List of attributes in the form of keyword-value pairs <br>&nbsp; 输出: BOOL 类型, 操作成功为True, 否则为False <br><br>&nbsp;例子: <br>&nbsp; procedure TForm1.Button3Click(Sender: TObject); <br>&nbsp; const <br>&nbsp; &nbsp; c = 'DSN=mypubs'#0 + &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; 'DESCRIPTION=SQLConfigDSN Sample'#0 + <br>&nbsp; &nbsp; &nbsp; &nbsp; 'SERVER=(local)'#0 + <br>&nbsp; &nbsp; &nbsp; &nbsp; 'ADDRESS=xyb'#0 + <br>&nbsp; &nbsp; &nbsp; &nbsp; 'NETWORK=.'#0 + <br>&nbsp; &nbsp; &nbsp; &nbsp; 'DATABASE=pubs'#0#0; <br>&nbsp; begin <br>&nbsp; &nbsp; if SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, 'SQL Server', c) then <br>&nbsp; &nbsp; &nbsp; caption := 'odbc ok' <br>&nbsp; &nbsp; else caption := 'odbc false'; <br>&nbsp; end; <br>} <br>function SQLConfigDataSource; external ODBCCP32 name 'SQLConfigDataSource';
 
直接写注册表也行
 
非常感谢,不过,没有试成功。<br>在Deliph6中调试,出现错误“undeclared identifier:'HWND'、undeclared identifier:'BOOL' "<br>还请多多指导!
 
HWND,需要在uses中加入windows这个单元!<br>可以将bool替换成integer或Longint试一试!
 
成了,你的代码没错!只是应该把CONST之后的代码放在uses部分的下面。<br>如果我想自动加入用户名和密码,怎么办?<br>再次感谢!
 

Similar threads

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