2012-03-04 173 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此錯誤。