使用Delphi创建一个新的ODBC用户DSN

7

我正在尝试使用以下代码在ODBC数据源管理器的用户DSN中创建一个新条目:

procedure TForm1.FormCreate(Sender: TObject);
var strAttributes: string;
    wideChars   : array[0..1000] of WideChar;
     pfErrorCode: DWORD;
     errMsg: PChar;

begin
 strAttributes := 'DSN=' + 'example_DSN' + Chr(0);
    strAttributes := strAttributes + 'DESCRIPTION=' + 'description' + Chr(0);
    strAttributes := strAttributes + 'SERVER=' + 'testserver' + Chr(0);
    strAttributes := strAttributes + 'DATABASE=' + 'somedatabase' + Chr(0);

  StringToWideChar(strAttributes, wideChars, 12);
  if not SqlConfigDataSource(0, ODBC_ADD_DSN, 'SQL Server', wideChars) then
  begin
    errMsg := AllocMem(SQL_MAX_MESSAGE_LENGTH);
    SQLInstallerError(1, @pfErrorCode, errMsg, SQL_MAX_MESSAGE_LENGTH, nil);
    MessageBox(0, errMsg, PChar('Add System DSN Error #' + IntToStr(pfErrorCode)), 0);
    FreeMem(errMsg);
  end;
end;

但是SqlConfigDataSource部分并没有完成任务,而且返回的错误信息根本无法理解。它既不是一个数字,也不是错误的描述。有谁能帮助我找出我的错误在哪里?谢谢。

3个回答

7
也许您的错误或错误集合在ODBC标头的错误翻译中,然后可以用于非Unicode或Unicode Delphi版本。例如:
对于Unicode Delphi,您更需要使用ODBCCP32.DLL中的UTF16函数XxxW,而不是ANSI函数Xxx;
对于非Unicode Delphi,您需要使用Xxx函数。然后应将wideChars定义为array[..]的Char;
SqlConfigDataSource可以被定义为带有PAnsiChar的XxxW;
我想向您展示这个想法,因为没有完整的源代码,我只能进行推测。然后出现了可疑的调用StringToWideChar(strAttributes,wideChars,12)。strAttributes值比12个字符长得多。
以下代码在Delphi XE2中运行良好:
type
  SQLHWnd = LongWord;
  SQLChar = Char;
  PSQLChar = ^SQLChar;
  SQLBOOL = WordBool;
  UDword = LongInt;
  PUDword = ^UDword;
  SQLSmallint = Smallint;
  SQLReturn = SQLSmallint;

const
  SQL_MAX_MESSAGE_LENGTH = 4096;

  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
  ODBC_REMOVE_DEFAULT_DSN   = 7;    // remove the default DSN

function SQLConfigDataSource (
    hwndParent:     SQLHWnd;
    fRequest:       WORD;
    lpszDriver:     PChar;
    lpszAttributes: PChar
  ): SQLBOOL; {$IFDEF MSWINDOWS} stdcall {$ELSE} cdecl {$ENDIF};
  external 'odbccp32.dll' name 'SQLConfigDataSourceW';

function SQLInstallerError (
    iError:           WORD;
    pfErrorCode:      PUDword;
    lpszErrorMsg:     PChar;
    cbErrorMsgMax:    WORD;
    pcbErrorMsg:      PWORD
  ): SQLReturn; {$IFDEF MSWINDOWS} stdcall {$ELSE} cdecl {$ENDIF};
  external 'odbccp32.dll' name 'SQLInstallerErrorW';

procedure TForm616.Button1Click(Sender: TObject);
var
  strAttributes: string;
  pfErrorCode: UDword;
  errMsg: PChar;
begin
  strAttributes := 'DSN=' + 'example_DSN' + Chr(0);
  strAttributes := strAttributes + 'DESCRIPTION=' + 'description' + Chr(0);
  strAttributes := strAttributes + 'SERVER=' + 'testserver' + Chr(0);
  strAttributes := strAttributes + 'DATABASE=' + 'somedatabase' + Chr(0);
  if not SqlConfigDataSource(0, ODBC_ADD_DSN, 'SQL Server', PChar(strAttributes)) then begin
    errMsg := AllocMem(SQL_MAX_MESSAGE_LENGTH);
    SQLInstallerError(1, @pfErrorCode, errMsg, SQL_MAX_MESSAGE_LENGTH, nil);
    MessageBox(0, errMsg, PChar('Add System DSN Error #' + IntToStr(pfErrorCode)), 0);
    FreeMem(errMsg);
  end;
end;

1
答案是正确的,但我需要做个注释。
如果你没有设置带端口的测试服务器,Windows会标记“ODBC SQL SERVER DRIVER DBNETLIB 'Invalid Connection'”。它会创建驱动程序并连接,但每次发送此错误时,如果你没有像这样设置测试服务器:
'testserver,port'
strAttributes := strAttributes + 'SERVER=' + 'testserver,port' + Chr(0);

那会是一个更好的答案,因为它将避免发送此错误。

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接