首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

,数据库连接失败SQLConnect函数调用异常

2012-07-27 
求助,数据库连接失败SQLConnect函数调用错误我配置了数据源,并且也成功了,我的服务器地址填的是127.0.0.1,

求助,数据库连接失败SQLConnect函数调用错误
我配置了数据源,并且也成功了,我的服务器地址填的是127.0.0.1,登录ID是s20092694,密码是616458数据源测试数据连接是成功的,默认数据库是db28,测试成功了,但是在控制台程序里面,老是在调用SQLConnect连接的时候,老是出错,返回-1,
调用

C/C++ code
    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发现错误是

rc20short
+SqlState0x0041fd1c "IM002"wchar_t [6]
+Msg0x0041f844 "[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序"wchar_t [512]
i1short
NativeError0long
MsgLen45short
请问我该怎么配置环境,是配置用户DSN,还是系统DSN,我两个都配置成功了,为什么连接时候还是出错

附上我的代码
C/C++ code
#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 SQLBINDCOLSQLHENV      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);} 



[解决办法]
代码中没看到你数据库连接的设置。
未发现数据源名称

--》》lz可能用的是odbc吧,没有设置该odbc数据源的名字?猜一个
[解决办法]
根据本人经验,同过ODBC编程时,不要用UNICODE字符集。在vs2005编程时,需要在项目属性中设置字符集为未设置。然后把你代码中所有出现SQLWCHAR的地方改成SWLCHAR就行了。希望你可以试一下。

热点排行