mysql数据库连接插入查询_c++ 连接MySql数据库并执行插入,查询数据测试代码

#include "stdafx.h"

#include

#include

#include //连接库:odbc32.lib odbccp32.lib

#include

#include

#include "resource.h"

#include "MainDlg.h"

#define LOGIN_TIMEOUT 30

#define MAXBUFLEN 255

#define CHECKDBSTMTERROR(hwnd,result,hstmt) if(SQL_ERROR==result){ShowDBStmtError(hwnd,hstmt);return;}

/*

*/

BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

{

switch(uMsg)

{

HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);

HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);

HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);

}

return FALSE;

}

BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)

{

return TRUE;

}

void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)

{

switch(id)

{

case IDC_OK:

{

Test(hwnd);

}

break;

default:

break;

}

}

void Main_OnClose(HWND hwnd)

{

EndDialog(hwnd, 0);

}

void ShowDBError(HWND hwnd,SQLSMALLINT type,SQLHANDLE sqlHandle)

{

char pStatus[10], pMsg[101];

SQLSMALLINT SQLmsglen;

char error[200] = {0};

SQLINTEGER SQLerr;

long erg2 = SQLGetDiagRec(type, sqlHandle,1,

(SQLCHAR *)pStatus,&SQLerr,(SQLCHAR *)pMsg,100,&SQLmsglen);

wsprintf(error,"%s (%d)\n",pMsg,(int)SQLerr);

MessageBox(hwnd,error,TEXT("数据库执行错误"),MB_ICONERROR|MB_OK);

}

void ShowDBConnError(HWND hwnd,SQLHDBC hdbc)

{

ShowDBError(hwnd,SQL_HANDLE_DBC,hdbc);

}

void ShowDBStmtError(HWND hwnd,SQLHSTMT hstmt)

{

ShowDBError(hwnd,SQL_HANDLE_STMT,hstmt);

}

void Test(HWND hwnd)

{

SQLHENV henv = NULL;

SQLHDBC hdbc = NULL;

SQLHSTMT hstmt = NULL;

SQLRETURN result;

SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=127.0.0.1;UID=root;PWD=123456;DATABASE=test;CharSet=gbk;";

SQLCHAR ConnStrOut[MAXBUFLEN];

//分配环境句柄

result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);

//设置管理环境属性

result = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);

//分配连接句柄

result = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);

//设置连接属性

result = SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (void*)LOGIN_TIMEOUT, 0);

//连接数据库

result = SQLDriverConnect(hdbc,NULL,

ConnStrIn,SQL_NTS,

ConnStrOut,MAXBUFLEN,

(SQLSMALLINT *)0,SQL_DRIVER_NOPROMPT);

if(SQL_ERROR==result)

{

ShowDBConnError(hwnd,hdbc);

return;

}

//初始化语句句柄

result = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);

//result = SQLPrepare(hstmt,(SQLCHAR*)"select id,name,age from student",SQL_NTS);//如果是查询

result = SQLPrepare(hstmt,(SQLCHAR*)"insert into student(id,name,age) values(3,'wanger',30)",SQL_NTS);

CHECKDBSTMTERROR(hwnd,result,hstmt);

result =SQLExecute(hstmt);

CHECKDBSTMTERROR(hwnd,result,hstmt);

//显示查询数据开始

/*  SQLINTEGER cbsatid=SQL_NTS;

TCHAR info[50]={0};

while (SQLFetch(hstmt)!=SQL_NO_DATA_FOUND)

{

SQLCHAR name[20];

SQLINTEGER id;

SQLSMALLINT age;

SQLGetData(hstmt,1,SQL_C_LONG,&id,sizeof(SQLINTEGER),&cbsatid);

SQLGetData(hstmt,2,SQL_C_CHAR,name,20,&cbsatid);

SQLGetData(hstmt,3,SQL_C_TINYINT,&age,sizeof(SQLSMALLINT),&cbsatid);

wsprintf(info,"%i,%s,%i",id,name,age);

MessageBox(NULL,info,"提示",MB_OK);

}

*/

//显示查询数据结束

SQLFreeStmt(hstmt,SQL_CLOSE);

SQLDisconnect(hdbc);

SQLFreeHandle(SQL_HANDLE_DBC,hdbc);

SQLFreeHandle(SQL_HANDLE_ENV,henv);

MessageBox(hwnd,TEXT("执行成功"),TEXT("标题"),MB_OK);

}

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值