C语言ODBC连接MySQL数据库制作简易用户登录系统

windows开发学习笔记 专栏收录该内容
0 篇文章 0 订阅

1、代码如下:

#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#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_BUTTON_SAVE:
			SaveToDataBase(hwnd);
        break;
		case IDC_BUTTON_LOGIN:
			Login(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 SaveToDataBase(HWND hwnd)
{
    SQLHENV henv = NULL;
    SQLHDBC hdbc = NULL;
    SQLHSTMT hstmt = NULL;
    SQLRETURN result;
    SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVER={MySQL ODBC 5.3 ANSI Driver};SERVER=127.0.0.1;UID=root;PWD=226;DATABASE=userlogin;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);
    //SQL_NTS telling the function the previous parameter is Null-Terminated String, 
//please alculate the string length for me
	
	TCHAR userName[20];
	GetDlgItemText(hwnd,IDC_EDIT_USERNAME,userName,sizeof(userName)/sizeof(TCHAR));
	TCHAR password[20];
	GetDlgItemText(hwnd,IDC_EDIT_PASSWORD,password,sizeof(password)/sizeof(TCHAR));
	
	TCHAR sql[255];
	wsprintf(sql,"insert into T_user(FuserName,Fpassword) values('%s','%s')",userName,password);
	result = SQLPrepare(hstmt,(SQLCHAR*)sql,SQL_NTS);
	CHECKDBSTMTERROR(hwnd,result,hstmt);
    result =SQLExecute(hstmt);
    CHECKDBSTMTERROR(hwnd,result,hstmt);
	
	SQLFreeStmt(hstmt,SQL_CLOSE);
    SQLDisconnect(hdbc);
    SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
    SQLFreeHandle(SQL_HANDLE_ENV,henv);
	MessageBox(hwnd,TEXT("保存成功"),TEXT("标题"),MB_OK);
}
void Login(HWND hwnd)
{
    SQLHENV henv = NULL;
    SQLHDBC hdbc = NULL;
    SQLHSTMT hstmt = NULL;
    SQLRETURN result;
    SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVER={MySQL ODBC 5.3 ANSI Driver};SERVER=127.0.0.1;UID=root;PWD=226;DATABASE=userlogin;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);

	TCHAR inputUserName[20];
	GetDlgItemText(hwnd,IDC_EDIT_USERNAME,inputUserName,sizeof(inputUserName)/sizeof(TCHAR));
	TCHAR inputPassword[20];
	GetDlgItemText(hwnd,IDC_EDIT_PASSWORD,inputPassword,sizeof(inputPassword)/sizeof(TCHAR));
	TCHAR sql[255];
	ZeroMemory(sql,sizeof(sql)/sizeof(TCHAR));
	wsprintf(sql,"select Fpassword from T_user where FuserName=%s",inputUserName);
	
	result = SQLPrepare(hstmt,(SQLCHAR*)"select FuserName,Fpassword from T_user ",SQL_NTS);
	CHECKDBSTMTERROR(hwnd,result,hstmt);
    result =SQLExecute(hstmt);
    CHECKDBSTMTERROR(hwnd,result,hstmt);
	SQLINTEGER cbsatid=SQL_NTS;
	
	BOOL found=FALSE;
	while (SQLFetch(hstmt)!=SQL_NO_DATA_FOUND)
	{

	
		TCHAR password[20];
		SQLGetData(hstmt,2,SQL_C_CHAR,password,sizeof(password)/sizeof(TCHAR),&cbsatid);
	
			if(0==lstrcmp(inputPassword,password))
			{
				MessageBox(hwnd,TEXT("输入正确,登录成功"),TEXT("提示"),MB_OK);
				found=TRUE;
				break;
			}
	
	}
	if(FALSE==found)
	{
		MessageBox(hwnd,TEXT("输入错误"),TEXT("报错"),MB_OK| MB_ICONERROR); 
	}


	SQLFreeStmt(hstmt,SQL_CLOSE);
    SQLDisconnect(hdbc);
    SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
    SQLFreeHandle(SQL_HANDLE_ENV,henv);

}


2、数据库及数据表如下:




3、注意在编译的时候,在工程设置中添加连接库“odbc32.lib odbccp32.lib”。


4、运行效果如下:



  • 1
    点赞
  • 0
    评论
  • 3
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2022 CSDN 皮肤主题:书香水墨 设计师:CSDN官方博客 返回首页

打赏作者

仰望星空90226

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值