2012-03-04 32 views
5

AI đang cố gắng để thực hiện một mục mới trong User DSN, trong ODBC Data Source Administrator mới có đoạn mã sau:tạo ra một DSN sử dụng ODBC mới với Delphi

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; 

nhưng phần SqlConfigDataSource không thực hiện công việc, và cả lỗi được trả về cũng không thể hiểu được. Nó không phải là một số, cũng không phải là mô tả cho lỗi. Bất cứ ai có thể giúp tôi, nơi tôi làm cho những sai lầm? Cảm ơn

Trả lời

7

Có thể lỗi của bạn hoặc thậm chí đặt lỗi là dịch sai các tiêu đề ODBC, sau đó có thể được sử dụng cho phiên bản không phải Unicode hoặc Unicode Delphi. Ví dụ:

  • cho Unicode Delphi bạn chứ không cần phải sử dụng XxxW (UTF16) chức năng từ ODBCCP32.DLL, hơn Xxx chức năng (ANSI);
  • đối với các chức năng không phải Unicode Delphi thay vì Xxx. Và sau đó wideChars phải được định nghĩa là array[..] of Char;
  • SqlConfigDataSource có thể được định nghĩa là XxxW với PAnsiChar;
  • v.v.

Tôi muốn cho bạn thấy ý tưởng, bởi vì không có nguồn đầy đủ, tôi chỉ có thể suy đoán. Sau đó, bạn có cuộc gọi đáng ngờ StringToWideChar(strAttributes, wideChars, 12);. Giá trị strAttributes dài hơn 12 ký tự.

Các mã sau hoạt động tốt trong 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

cảm ơn, điều đó đã làm – dzibul

-1

Câu trả lời là đúng nhưng tôi phải thực hiện một lưu ý.

Nếu bạn không đặt trình kiểm tra với cổng, các cửa sổ đánh dấu "ODBC SQL SERVER DRIVER DBNETLIB 'Kết nối không hợp lệ'" Nó tạo trình điều khiển và kết nối nhưng mỗi khi gửi lỗi này nếu bạn không đặt máy chủ thử nghiệm như:

'Tuyên chiến, cổng'

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

Điều đó sẽ làm cho một câu trả lời tốt hơn bởi vì nó sẽ tránh sendidng lỗi này.

Các vấn đề liên quan