日期:2014-05-16  浏览次数:20570 次

c++将txt数据批量导入mysql数据库的例子

////包括多线程的使用,数据库的操作和打开文件对话框的使用。


#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include <SQL.H>//连接库:odbc32.lib odbccp32.lib
#include <SQLEXT.H>//数据库头文件
#include <SQLTYPES.H>//数据库头文件
#include <COMMDLG.H>//OPENFILENAME 头文件
#include<STRING.H>
#define LOGIN_TIMEOUT 30
#define MAXBUFLEN 255
///#define CHECKDBSTMTERROR(hwnd,result,hstmt) 
char szFile[MAX_PATH];
BOOL ImportStatus = FALSE; //导入状态标志
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];

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);
}
//查检是否有错误
BOOL CHECKDBSTMTERROR(HWND hwnd,SQLRETURN result,SQLHSTMT hstmt)
{
	if(SQL_ERROR==result)
	{
		ShowDBStmtError(hwnd,hstmt);
		return TRUE;
	} 
	else 
	{
		return FALSE;
	}
}
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 openfile(HWND hwnd)
{
OPENFILENAME ofn;//定义一个OPENFILENAME类型的结构体。
				ZeroMemory(&ofn,sizeof(ofn));//使用结构体变量前把该结构体变量各个位清零。
				ofn.lStructSize = sizeof(ofn);
				ofn.lpstrFile = szFile; 
				ofn.lpstrFile[0] = TEXT('\0');
				ofn.nMaxFile = sizeof(szFile);
				ofn.lpstrFilter = TEXT("ALL\0*.*\0文本文档\0*.TXT\0");
				ofn.nFilterIndex = 2;
				ofn.lpstrFileTitle = NULL;
				ofn.nMaxFileTitle = 0;
				ofn.lpstrInitialDir = NULL;
				ofn.Flags = OFN_EXPLORER |OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
				if (GetOpenFileName(&ofn))
				{
					SetDlgItemText(hwnd,IDC_FILEINFO,szFile);
				}
}
BOOL inport_begin(HWND hwnd)
{
	//分配环境句柄
	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 FALSE;
	}
	ImportStatus = TRUE;
	return TRUE;
}
DWORD WINAPI ThreadFunc(LPVOID lpParam)                //多线程任务
{
	HWND hwnd = (HWND)lpParam;
	char str[50];
	char fname[11];
	char fage[11];
	int  age;
	int i,j,k;
	int IntoNum = 0;
	//TCHAR temp[256];
	
	TCHAR info[256];
	FILE * Input;
	for (i=0;szFile[i]!='\0';++i)                        //处理绝对路径
	{
		if (szFile[i]=='\\')
		{
			szFile[i]='/';
		}
	}
	if(strlen(szFile)==0){SetDlgItemText(hwnd,IDC_STATUS,"请选择文件!"); return 0;}
	Input = fopen(szFile,"r");                                //打开文本文档
	BOOL hasError=FALSE;
	/*创建新的数据库句柄,执行SQL语句*/
	result = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
	result = SQLPrepare(hstmt,(SQLCHAR *)"insert into T_Person(FName,FAge) values(?,?)",SQL_NTS);//两个占位符
	SQLINTEGER iSQL_NTS=