VC里面SQLConfigDataSource应该怎么用?

yumingfei419308 2010-10-26 07:47:45
VC里SQLConfigDataSource好像被#define为了SQLConfigDataSourceW,但是在下面的程序里:
if(SQLConfigDataSourceW(NULL,
ODBC_ADD_SYS_DSN,
"Microsoft Access Driver (*.mdb)",
"DSN=Test\0Description=New Access Data Source\0DBQ=F:\\学习文件\\网络编程\\网络词典\\09\\9.9 \\Server\\Dict.mdb\0FIL=MS Access\0MaxScanRows=20\0"))
{
AfxMessageBox( "加载数据源成功 ");
}
else
{
AfxMessageBox( "没有成功加载数据源 ");
}

第三个参数显示类型不对。应该怎么改?
提示错误为:
F:\Program Files\数据库\condb\condb.cpp(65) : error C2664: 'SQLConfigDataSourceW' : cannot convert parameter 3 from 'char [32]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
...全文
196 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyuan136 2011-12-20
  • 打赏
  • 举报
回复
Thank zyq5945(zhuyeqing) for answer.
zgl7903 2010-10-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yumingfei419308 的回复:]
谢谢了,应该就是这个问题了,还想问一下
c:\program files\microsoft visual studio\vc98\include\sqltypes.h(114) : error C2146: syntax error : missing ';' before identifier 'SQLHWND'
发生这个错误是怎么回事?
[/Quote]
1 确保工程预处理宏中有WIN32
2 #include “sqltypes.h" 前加 #include "windows.h"
yumingfei419308 2010-10-28
  • 打赏
  • 举报
回复
谢谢了,应该就是这个问题了,还想问一下
c:\program files\microsoft visual studio\vc98\include\sqltypes.h(114) : error C2146: syntax error : missing ';' before identifier 'SQLHWND'
发生这个错误是怎么回事?
zgl7903 2010-10-28
  • 打赏
  • 举报
回复

#include <ODBCINST.H>
#pragma comment(lib, "ODBC32.LIB")
#pragma comment(lib, "ODBCCP32.LIB")
void Config_Sample(void)
{
// Create an Access datasource, set Exclusive = True.
LPCTSTR szDriver = _T("Microsoft Access Driver (*.mdb)");
LPCTSTR szAttributes =
_T("DSN=ExcSample\0")
_T("FIL=MS Access\0")
_T("JETINIPATH=odbcddp.ini\0")
_T("DBQ=c:\\temp\\UserInfo.mdb\0")
_T("DEFAULTDIR=c:\\temp\0")
_T("EXCLUSIVE=1\0");

//添加
if(!SQLConfigDataSource(NULL, ODBC_ADD_DSN, szDriver, szAttributes))
{
ASSERT(0);
}

//删除
if(!SQLConfigDataSource(NULL, ODBC_REMOVE_DSN, szDriver, szAttributes))
{
ASSERT(0);
}
}
yumingfei419308 2010-10-28
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<string.h>
#include<cstring>
#include<odbcinst.h>


void SetODBCSource() //增加ODBC数据源
{
string strExePath= "F:\\学习文件\\网络编程\\网络词典\\09\\9.9\\Server\\";
string strAccessPath = strExePath+ "student.mdb";
int iLen = strAccessPath.strlen();
char cpConfig[MAX_PATH];

strcpy(cpConfig, "DSN=Dicto\0");
strcpy(cpConfig + 10, "DBQ="); //10是"DSN=studt\0"的长度
strcpy(cpConfig + 14, strAccessPath);
strcpy(cpConfig + 14 + iLen, "\0");
strcpy(cpConfig + 15 + iLen, "DEFAULTDIR=");
strcpy(cpConfig + 15 + iLen + 11, strExePath);
strcpy(cpConfig + 25 + iLen + strExePath.strlen(), "\0\0");

if(!SQLConfigDataSourceW(NULL,ODBC_ADD_SYS_DSN, //注册数据源
"Microsoft Access Driver (*.mdb)\0",cpConfig))
AfxMessageBox("不能添加数据源", MB_OK | MB_ICONEXCLAMATION);
}

int main()
{
SetODBCSource();
return 0;
}

显示错误:
--------------------Configuration: odbcadd - Win32 Debug--------------------
Compiling...
add.cpp
c:\program files\microsoft visual studio\vc98\include\sqltypes.h(114) : error C2146: syntax error : missing ';' before identifier 'SQLHWND'
c:\program files\microsoft visual studio\vc98\include\sqltypes.h(114) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

odbcadd.exe - 2 error(s), 0 warning(s)
傻X 2010-10-28
  • 打赏
  • 举报
回复
你这个是Unicode的问题。。用_T("")和L""都能解决。。

另外错误报SQLHWND之前没加分号。。。但是一般这种错误很难讲。。。楼主上代码
yumingfei419308 2010-10-28
  • 打赏
  • 举报
回复
感觉说的挺有道理,但是还是error
想问一下,显示这个错误是什么问题:
error C2146: syntax error : missing ';' before identifier 'SQLHWND'
在工程和cmd下都是这个问题
zyq5945 2010-10-26
  • 打赏
  • 举报
回复
工程是UNICODE的,字符串需要这样用_T("字符串")包含,或者改成多字节工程
if(SQLConfigDataSourceW(NULL, 
ODBC_ADD_SYS_DSN,
_T("Microsoft Access Driver (*.mdb)",
"DSN=Test\0Description=New Access Data Source\0DBQ=F:\\学习文件\\网络编程\\网络词典\\09\\9.9 \\Server\\Dict.mdb\0FIL=MS Access\0MaxScanRows=20\0")))
{
AfxMessageBox( _T("加载数据源成功 "));
}
else
{
AfxMessageBox( _T("没有成功加载数据源 "));
}

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧