求助,数据库连接失败SQLConnect函数调用错误

coolboylai 2012-04-21 11:02:35
我配置了数据源,并且也成功了,我的服务器地址填的是127.0.0.1,登录ID是s20092694,密码是616458数据源测试数据连接是成功的,默认数据库是db28,测试成功了,但是在控制台程序里面,老是在调用SQLConnect连接的时候,老是出错,返回-1,
调用
	
SQLWCHAR SqlState[6], SQLStmt[100], Msg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER NativeError;
SQLSMALLINT i, MsgLen;
SQLRETURN rc2;

i = 1;
while ((rc2 = SQLGetDiagRec(SQL_HANDLE_DBC,hdbc1, i, SqlState, &NativeError,
Msg, sizeof(Msg), &MsgLen)) != SQL_NO_DATA)
{
//DisplayError(SqlState,NativeError,Msg,MsgLen);
111 cout<<SqlState<<" "<<NativeError<<" "<<Msg;
i++;
}


在111处设置断点,DEBUG发现错误是

rc2 0 short
+ SqlState 0x0041fd1c "IM002" wchar_t [6]
+ Msg 0x0041f844 "[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序" wchar_t [512]
i 1 short
NativeError 0 long
MsgLen 45 short
请问我该怎么配置环境,是配置用户DSN,还是系统DSN,我两个都配置成功了,为什么连接时候还是出错

附上我的代码

#include <stdio.h>
//#include <iostream.h>
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include<sqltypes.h>
#include<odbcinst.h >
#include <odbcss.h>
#include <iostream>
using namespace std;


#pragma comment(lib,"odbc32.lib")
#define MAXBUFLEN 255
#define MaxNameLen 20

#define SQLBINDCOL

SQLHENV henv = SQL_NULL_HENV;//定义环境句柄
SQLHDBC hdbc1 = SQL_NULL_HDBC;//定义数据库连接句柄
SQLHSTMT hstmt1 = SQL_NULL_HSTMT;//定义语句句柄

int main()
{
RETCODE retcode;//错误返回码

// Allocate the ODBC Environment and save handle.
retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
if(retcode < 0 )//错误处理
{
cout<<"allocate ODBC Environment handle errors."<<endl;
return -1;
}

// Notify ODBC that this is an ODBC 3.0 application.
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
if(retcode < 0 ) //错误处理
{
cout<<"the ODBC is not version3.0 "<<endl;
return -1;
}

// Allocate an ODBC connection and connect.
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
if(retcode < 0 ) //错误处理
{
cout<<"allocate ODBC connection handle errors."<<endl;
return -1;
}

//Data Source Name must be of type User DNS or System DNS
char* szDSN = "dbtest";
char* szUID = "s20092694";//log name
char* szAuthStr = "616458";//passward

//connect to the Data Source
retcode=SQLConnect(hdbc1,(SQLWCHAR*)szDSN,SQL_NTS,(SQLWCHAR*)szUID,SQL_NTS,(SQLWCHAR*)szAuthStr,SQL_NTS);
if(retcode < 0 ) //错误处理
{
cout<<"connect to ODBC datasource errors."<<endl;
SQLWCHAR SqlState[6], SQLStmt[100], Msg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER NativeError;
SQLSMALLINT i, MsgLen;
SQLRETURN rc2;

i = 1;
while ((rc2 = SQLGetDiagRec(SQL_HANDLE_DBC,hdbc1, i, SqlState, &NativeError,
Msg, sizeof(Msg), &MsgLen)) != SQL_NO_DATA)
{
//DisplayError(SqlState,NativeError,Msg,MsgLen);
cout<<SqlState<<" "<<NativeError<<" "<<Msg;
i++;
}

return -1;
}

// Allocate a statement handle.
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);
if(retcode < 0 ) //错误处理
{
cout<<"allocate ODBC statement handle errors."<<endl;
return -1;
}

// Execute an SQL statement directly on the statement handle.每一句后面都跟了一个错误处理,当发生错误时可以很方便的判断错在哪里

retcode = SQLExecDirect(hstmt1,(SQLWCHAR*)"create table provider (sno char(5) primary key,sname char(10) not null,status int,city char(10))", SQL_NTS);
if(retcode<0)
{
cout<<"creat errors."<<endl;
return -1;
}

retcode = SQLExecDirect(hstmt1,(SQLWCHAR*)"insert into provider values('S1','精益','20','天津')",SQL_NTS);
if(retcode<0)
{
cout<<"s1 insert errors."<<endl;
return -1;
}

retcode = SQLExecDirect(hstmt1,(SQLWCHAR*)"insert into provider values('S2','胜锡','10','北京')",SQL_NTS);
if(retcode<0)
{
cout<<"s2 insert errors."<<endl;
return -1;
}

retcode = SQLExecDirect(hstmt1,(SQLWCHAR*)"insert into provider values('S3','东方红','30','天津')",SQL_NTS);
if(retcode<0)
{
cout<<"s3 insert errors."<<endl;
return -1;
}

retcode = SQLExecDirect(hstmt1,(SQLWCHAR*)"insert into provider values('S4','丰泰盛','20','天津')",SQL_NTS);
if(retcode<0)
{
cout<<"s4 insert errors."<<endl;
return -1;
}

retcode = SQLExecDirect(hstmt1,(SQLWCHAR*)"insert into provider values('S5','为民','30','上海')",SQL_NTS);
if(retcode<0)
{
cout<<"s5 insert errors."<<endl;
return -1;
}

retcode = SQLExecDirect(hstmt1,(SQLWCHAR*)"insert into provider values('S6','通天','25',null)",SQL_NTS);
if(retcode<0)
{
cout<<"s6 insert errors."<<endl;
return -1;
}

retcode = SQLExecDirect(hstmt1,(SQLWCHAR*)"SElECT sname,city FROM provider where sno='s1'", SQL_NTS);
if(retcode < 0 )
{
cout<<"Executing statement throught ODBC errors."<<endl;
return -1;
}
/* retcode = SQLExecDirect(hstmt1,(SQLWCHAR*)"drop table provider", SQL_NTS);
if(retcode<0)
{
cout<<"drop errors."<<endl;
return -1;
}*/

// SQLBindCol variables
SQLWCHAR city[MaxNameLen + 1];
SQLWCHAR name[MaxNameLen + 1];
SQLINTEGER columnLen = 0;//数据库定义中该属性列的长度

#ifdef SQLBINDCOL

//游标已被封装在其中,一开始把两个列号分别写为provider中的列号2(name),4(city),结果name的值为city的值,city的值为烫烫烫烫烫烫烫烫,这才发现第二个参数应为游标中的列号,而不是表中的列号,
retcode = SQLBindCol(hstmt1, 1, SQL_C_CHAR,name,MaxNameLen , &columnLen);
retcode = SQLBindCol(hstmt1, 2, SQL_C_CHAR,city,MaxNameLen , &columnLen);
while ( (retcode = SQLFetch(hstmt1) ) != SQL_NO_DATA)
{
if(columnLen>0)
printf("name = %s city = %s\n", name,city);
else
printf("name = %s city = NULL\n", name,city);
}



#else

while(1 )
{
retcode = SQLFetch(hstmt1);
if(retcode == SQL_NO_DATA)
break;

retcode = SQLGetData(hstmt1, 1, SQL_C_CHAR, name, MaxNameLen, &columnLen);
retcode = SQLGetData(hstmt1, 2, SQL_C_CHAR, city, MaxNameLen, &columnLen);
if(columnLen>0)
printf("name = %s city = %s\n", name,city);
else
printf("name = %s city = NULL\n", name,city);

}

#endif


/* Clean up.*/
SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);

return(0);
}

...全文
871 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
LALUOBOYOU 2014-11-13
  • 打赏
  • 举报
回复
引用 5 楼 zxysong 的回复:
根据本人经验,同过ODBC编程时,不要用UNICODE字符集。在vs2005编程时,需要在项目属性中设置字符集为未设置。然后把你代码中所有出现SQLWCHAR的地方改成SWLCHAR就行了。希望你可以试一下。
正解!
LALUOBOYOU 2014-11-13
  • 打赏
  • 举报
回复
引用 7 楼 wwwlxz 的回复:
项目菜单--项目属性(最后一个)--配置属性--常规--项目默认值--字符集,(Project-> Properties-> Configurations Properties-> General-> Project Defaults-> Charater Set)将使用Unicode字符集改为未设置即可。
正解!!
wwwlxz123 2013-01-20
  • 打赏
  • 举报
回复
项目菜单--项目属性(最后一个)--配置属性--常规--项目默认值--字符集,(Project-> Properties-> Configurations Properties-> General-> Project Defaults-> Charater Set)将使用Unicode字符集改为未设置即可。
snowrab0281 2012-09-28
  • 打赏
  • 举报
回复
请问楼主,好了没?我也遇到了同样的问题,不知道为啥啊?百度里看见有人说将服务器名改为数据库名,就OK,可是我的也不行啊。
zxysong 2012-06-24
  • 打赏
  • 举报
回复
根据本人经验,同过ODBC编程时,不要用UNICODE字符集。在vs2005编程时,需要在项目属性中设置字符集为未设置。然后把你代码中所有出现SQLWCHAR的地方改成SWLCHAR就行了。希望你可以试一下。
  • 打赏
  • 举报
回复
代码中没看到你数据库连接的设置。
未发现数据源名称

--》》lz可能用的是odbc吧,没有设置该odbc数据源的名字?猜一个
coolboylai 2012-04-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
代码中没看到你数据库连接的设置。
未发现数据源名称

--》》lz可能用的是odbc吧,没有设置该odbc数据源的名字?猜一个
[/Quote]

我在控制面板的数据源配置了,里面配置了是数据源啊,而且测试成功了
coolboylai 2012-04-22
  • 打赏
  • 举报
回复
好吧,无语了==
simonxt 2012-04-22
  • 打赏
  • 举报
回复
帮顶,爱莫能助
去到c/C++板块问问

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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