回 帖 发 新 帖 刷新版面

主题:API编写SQLserver程序,总是错误!!!

下面的代码不知道出了什么问题,执行SQL总是失败!

//下面这几个都是全局变量

SQLHENV            EnvHandle=NULL;

SQLHDBC            LinkHandle=NULL;

SQLHSTMT        SmtHandle=NULL;

unsigned char   LinkString[]="Driver={SQL Server};Server=(local);Database=account;Uid=sa;Pwd=123456;";

unsigned char   SqlString[]="INSERT INTO user(id_x,pw_x) VALUES('Indiana','Jones')";


//函数的定义部分
int InitDataBase(SQLHENV EnvHandle,SQLHDBC LinkHandle,SQLHSTMT SmtHandle,SQLHWND hWnd,unsigned char *LinkString)
{
 SQLRETURN        ReturnValue;
 unsigned char    ReturnString[300];
 short             ReturnLenth;

    ReturnValue=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&EnvHandle);
    if(ReturnValue!=SQL_SUCCESS && ReturnValue!=SQL_SUCCESS_WITH_INFO)
    {
        MessageBox(0,"数据库环境初始化失败!","警告:",0);
    return 0;
    }

    SQLSetEnvAttr(EnvHandle,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);


    ReturnValue=SQLAllocHandle(SQL_HANDLE_DBC,EnvHandle,&LinkHandle);
    if(ReturnValue!=SQL_SUCCESS && ReturnValue!=SQL_SUCCESS_WITH_INFO)
    {
        MessageBox(0,"数据库连接初始化失败!","警告:",0);
    return 0;
    }

    ReturnValue=SQLDriverConnect(LinkHandle,
                                 hWnd,
                                 LinkString,
                                 strlen((PCSTR)LinkString),
                                 (unsigned char *)&ReturnString,
                                 sizeof((PCSTR)ReturnString),
                                 &ReturnLenth,
                                 SQL_DRIVER_PROMPT);

    if(ReturnValue!=SQL_SUCCESS && ReturnValue!=SQL_SUCCESS_WITH_INFO)
    {
        MessageBox(0,"连接数据库出错!","警告:",0);
    return 0;
    }

    ReturnValue=SQLAllocHandle(SQL_HANDLE_STMT,LinkHandle,&SmtHandle);
    if(ReturnValue!=SQL_SUCCESS && ReturnValue!=SQL_SUCCESS_WITH_INFO)
    {
        MessageBox(0,"数据库语句初始化失败!","警告:",0);
    return 0;
    }

    SQLSetStmtAttr(SmtHandle,SQL_ATTR_CURSOR_TYPE,(void *)SQL_CURSOR_STATIC,0);

return 1;
}


//调用的时候这样调用
case IDC_BUTTON1:                 
int a;
                             a=InitDataBase(EnvHandle,LinkHandle,SmtHandle,hWnd,LinkString);        

if(a==1)                             {MessageBox(0,"数据库初始化成功!","",0);}            
return 0;
                                          
case IDC_BUTTON3:
DWORD Lenth=80;
                             SQLRETURN ret=SQLExecDirect(LinkHandle,SqlString,Lenth);
                             if(ret!=SQL_SUCCESS && ret!=SQL_SUCCESS_WITH_INFO)
{
                                MessageBox(0,"SQL语句执行失败!","警告:",0);
}                             return 0;


因为我对数据库不了解,所以不知道哪边出错了。SQL语句在SQLSERVER的查询分析器里面测试过,没问题!请大家帮我看看有什么问题!

回复列表 (共1个回复)

沙发

好象发错地方了

我来回复

您尚未登录,请登录后再回复。点此登录或注册