2012-03-04 174 views
5

AI一个新的ODBC用户DSN想使用户DSN用下面的代码的新条目,在ODBC数据源管理器:创建德尔福

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部分没有做的工作,而且返回的错误不是undarstandable可言。这不是一个数字,也不是错误的描述。任何人都可以帮我解决我犯的错误吗?谢谢

回答

7

可能你的错误或者甚至是一组错误是在ODBC头文件的不正确的翻译中,然后它可能被用于非Unicode或Unicode的Delphi版本。例如:

  • 对Unicode的德尔福,你最好使用XxxW(UTF-16)功能从ODBCCP32.DLL,比Xxx(ANSI)的功能;
  • 对于非Unicode的Delphi而不是Xxx函数。然后wideChars应该被定义为array[..] of Char;
  • SqlConfigDataSource可以定义为XxxW与PAnsiChar;

我想告诉你的想法,因为没有完整的源我只能猜测。然后你有可疑的电话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; 
+0

谢谢,就是这样做的 – dzibul 2012-03-04 17:16:27

-1

答案是正确的,但我必须做出说明。

如果不设置与港口的TESTSERVER,窗户痕迹 “ODBC SQL Server驱动程序DBNETLIB‘无效连接’” 它创建驱动程序和所连接,但每次如果你不把它发送这个错误测试服务器,如:

“TESTSERVER,口”

strAttributes := strAttributes + 'SERVER=' + 'testserver,port' + Chr(0); 

这将使一个更好的答案,因为它会避免sendidng此错误。