2、使用SQL函数进行连接。
对于1、配置数据源,配置完以后就可以编程 *** 作数据库了。
对于2、使用SQL函数进行连接,参考代码如下:
#include<windows.h>
#include<sql.h>
#include<sqlext.h>
void main()
{
HENV henv //环境句柄
HDBC hdbc //数据源句柄
HSTMT hstmt //执行语句句柄
unsigned char datasource[]="数据源名称" //即<a href="https://www.baidu.com/s?wd=ODBC&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLrHIWuWRsPWTvujD4P1Nb0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1mYP1R1n1cknW6sPWbzrj03Pf" target="_blank" class="baidu-highlight">ODBC</a>源中设置的源名称
unsigned char user[]= "用户名" //数据库的帐户名
unsigned char pwd[]= "密码" //数据库的密码
unsigned char search[]="select xm from stu where xh=0"
SQLRETURN retcode//记录各SQL函数的返回情况
// 分配环境句柄
retcode= SQLAllocEnv(&henv) // 等介于 SQLAllocHandle(SQL_HANDLE_ENV, SQL_<a href="https://www.baidu.com/s?wd=NULL&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLrHIWuWRsPWTvujD4P1Nb0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1mYP1R1n1cknW6sPWbzrj03Pf" target="_blank" class="baidu-highlight">NULL</a>_HANDLE, &henv)
// 设置ODBC环境版本号为3.0
retcode= SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0)
// 分配连接句柄
retcode= SQLAllocConnect(henv,&hdbc)// 等介于 SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc)
//设置连接属性,登录超时为*rgbValue秒(可以没有)
// SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)(rgbValue), 0)
//直接连接数据源
// 如果是windows身份验证,第二、三参数可以是<a href="https://www.baidu.com/s?wd=NULL&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLrHIWuWRsPWTvujD4P1Nb0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1mYP1R1n1cknW6sPWbzrj03Pf" target="_blank" class="baidu-highlight">NULL</a>,也可以是任何字串
//SQL_NTS 即 "<a href="https://www.baidu.com/s?wd=Null&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLrHIWuWRsPWTvujD4P1Nb0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1mYP1R1n1cknW6sPWbzrj03Pf" target="_blank" class="baidu-highlight">Null</a>-Terminated String"
retcode= SQLConnect(hdbc,datasource, SQL_NTS, user, SQL_NTS , pwd, SQL_NTS )
//分配语句句柄
retcode= SQLAllocStmt(hdbc,&hstmt) // 等介于 SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt)
//直接执行查询语句
retcode=SQLExecDirect(hstmt,search,SQL_NTS)
//将数据缓冲区绑定数据库中的相应字段(i是查询结果集列号,queryData是绑定缓冲区,BUFF_LENGTH是缓冲区长度)
SQLBindCol(hstmt, i, SQL_C_CHAR, queryData[i-1], BUFF_LENGTH, 0)
//遍历结果集到相应缓冲区 queryData
SQLFetch(hstmt)
/*
*对遍历结果的相关 *** 作,如显示等
*/
//注意释放顺序,否则会造成未知错误!
SQLFreeHandle(SQL_HANDLE_STMT, hstmt)
SQLDisconnect(hdbc)
SQLFreeHandle(SQL_HANDLE_DBC, hdbc)
SQLFreeHandle(SQL_HANDLE_ENV, henv)
}
代码看上去很复杂,其实都可以直接复制。其中需要改的是数据源名称、数据库用户名、数据库密码和SQL语句。
然后就是SQLFetch后得到结果集后,如何使用的问题了。
在下面的注释部分 填写你的处理代码即可。
/*
*对遍历结果的相关 *** 作,如显示等
*/
你那个update语句是有where条件的,也可能根本就没有符合where条件的记录存在,所以也就不会有被更新的情况。但这个语句是被成功执行了的。所以返回值是成功的。大概是这么回事。
#include "resource.h"#include <windows.h>
#include <sqlext.h>
SQLHENV hEnv = NULL
SQLHDBC hConn = NULL
SQLHSTMT hStmt=NULL
SQLHSTMT hStmt1=NULL
HWND hWinMain
unsigned char ConnString[1024]
unsigned char FullString[1024]
unsigned char buffer1[1024]
unsigned char number[50]
unsigned char branch[50]
unsigned char money[50]
unsigned char szSQL[100]={"insert into account values(?,?,?)"}
long int size1,size2,size3
void Connect()
{
int ax,length
short int *Temp=NULL
ax=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&hEnv)
if(ax!=SQL_SUCCESS &&ax!=SQL_SUCCESS_WITH_INFO)
return
ax=SQLSetEnvAttr(hEnv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0)
if(ax!=SQL_SUCCESS &&ax!=SQL_SUCCESS_WITH_INFO)
return
ax=SQLAllocHandle(SQL_HANDLE_DBC,hEnv,&hConn)
if(ax!=SQL_SUCCESS &&ax!=SQL_SUCCESS_WITH_INFO)
return
SQLSetConnectAttr(hConn,SQL_ATTR_AUTOCOMMIT,SQL_AUTOCOMMIT_OFF,0)
length=sizeof(ConnString)
ax=SQLDriverConnect(hConn,hWinMain,ConnString,length,FullString,sizeof(FullString),Temp,SQL_DRIVER_COMPLETE)
if(ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO)
MessageBox(hWinMain,(char *)FullString,TEXT("提示:"),0)
}
BOOL CALLBACK MainDlg (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND a
int ax
switch(message)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
Connect()return truebreak
case IDC_EDIT1:
GetDlgItemText(hwnd,IDC_EDIT1,(char *)ConnString,sizeof(ConnString))
a=GetDlgItem(hwnd,IDC_BUTTON1)
if(*ConnString)
EnableWindow(a,TRUE)
else
EnableWindow(a,FALSE)
return truebreak
case IDC_BUTTON2:
GetDlgItemText(hwnd,IDC_EDIT5,(char *)buffer1,sizeof(buffer1))
ax=SQLAllocHandle(SQL_HANDLE_STMT,hConn,&hStmt)
if(ax!=SQL_SUCCESS &&ax!=SQL_SUCCESS_WITH_INFO)
return 0
SQLSetStmtAttr(hStmt,SQL_ATTR_CURSOR_TYPE,(void *)SQL_CURSOR_STATIC,0)
MessageBox(hWinMain,(char *)buffer1,TEXT("提示:"),0)
ax=lstrlen((char *)buffer1)
SQLExecDirect(hStmt,buffer1,ax)
SQLFreeHandle(SQL_HANDLE_STMT,hStmt)break
case IDCANCEL:
if(hConn)
{
SQLEndTran(SQL_HANDLE_DBC,hConn,SQL_COMMIT)
SQLDisconnect(hConn)
SQLFreeHandle(SQL_HANDLE_DBC,hConn)
}
if(hEnv)
SQLFreeHandle(SQL_HANDLE_ENV,hEnv)
MessageBox(hWinMain,TEXT("已成功与数据库断开连接!"),TEXT("提示...."),0)
break
case IDOK:
GetDlgItemText(hwnd,IDC_EDIT2,(char *)number,sizeof(number))
GetDlgItemText(hwnd,IDC_EDIT3,(char *)branch,sizeof(branch))
GetDlgItemText(hwnd,IDC_EDIT4,(char *)money,sizeof(money))
size1=sizeof(number)
size2=sizeof(branch)
size3=sizeof(money)
ax=SQLAllocHandle(SQL_HANDLE_STMT,hConn,&hStmt1)
if(ax!=SQL_SUCCESS &&ax!=SQL_SUCCESS_WITH_INFO)
return 0
SQLSetStmtAttr(hStmt1,SQL_ATTR_CURSOR_TYPE,(void *)SQL_CURSOR_STATIC,0)
SQLPrepare(hStmt1,szSQL,sizeof(szSQL))
SQLBindParameter(hStmt1,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_VARCHAR,50,0,number,10,&size1)
SQLBindParameter(hStmt1,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_VARCHAR,50,0,branch,10,&size2)
SQLBindParameter(hStmt1,3,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_VARCHAR,50,0,money,10,&size3)
SQLExecute(hStmt1)
MessageBox(hWinMain,TEXT("已成功添加一条记录!"),TEXT("提示...."),0)
SQLFreeHandle(SQL_HANDLE_STMT,hStmt1)
break
}
case WM_INITDIALOG:
hWinMain=hwndreturn truebreak
case WM_CLOSE:
EndDialog(hwnd,0)
return truebreak
}
return false
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
DialogBoxParam (hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, MainDlg, NULL)
return 0
}
//Driver={SQL Server}Server=服务器名Database=eeqt(文件名)
//insert into account values(520,'中国银行',30000)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)