dzi*_*bul 6 sql delphi odbc dsn sql-server-2008
我正在尝试使用以下代码在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;
Run Code Online (Sandbox Code Playgroud)
但是SqlConfigDataSource部分不能完成这项工作,并且返回的错误也不是无法解决的.它不是数字,也不是错误的描述.任何人都可以在我犯错的地方帮助我吗?谢谢
可能是您的错误甚至错误集都是ODBC标头的错误翻译,然后可能会用于非Unicode或Unicode Delphi版本.例如:
XxxW(UTF16)函数ODBCCP32.DLL,而不是Xxx(ANSI)函数;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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4798 次 |
| 最近记录: |