C语言ODBC连接ACCESS数据库

该代码是c通过ODBC直接读取数据库,从微软下载的,经过本人在VS2013下面严格测试过。不需要单独配置ODBC驱动。
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\test.mdb;";
test.mdb修改为你的access数据库所在的路径。

如果你的数据库是access2007以下版本去掉*.accdb

程序源码:

#include <windows.h>
#include <stdio.h>
#include <sqlext.h> 

/* Data Access Method used in this sample */
const char* DAM = "Direct ODBC";
/* Connection string for Direct ODBC */
char szDSN[256] ="Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=D:\\c_access.accdb;";

void main()
{
	HENV hEnv;
	HDBC hDbc;

	/* ODBC API return status */
	RETCODE rc;
	int iConnStrLength2Ptr;
	char szConnStrOut[256];
	unsigned char* query = "select * from userInfo";
	SQLCHAR chval1[128], chval2[128], colName[128];
	int ret1;
	int ret2;

	/* Number of rows and columns in result set */
	SQLINTEGER rowCount = 0;
	SQLSMALLINT fieldCount = 0, currentField = 0;
	HSTMT hStmt;

	/* Allocate an environment handle */
	rc = SQLAllocEnv(&hEnv);

	/* Allocate a connection handle */
	rc = SQLAllocConnect(hEnv, &hDbc);

	/* Connect to the 'c_access.accdb;' database */
	rc = SQLDriverConnect(hDbc, NULL, (unsigned char*)szDSN,
		SQL_NTS, (unsigned char*)szConnStrOut,
		255, (SQLSMALLINT*)&iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);

	if (SQL_SUCCEEDED(rc))
	{
		printf("%s: Successfully connected to database. Data source name: \n %s\n",
			DAM, szConnStrOut);
		/* Prepare SQL query */
		printf("%s: SQL query:\n %s\n", DAM, query);
		rc = SQLAllocStmt(hDbc, &hStmt);
		rc = SQLPrepare(hStmt, query, SQL_NTS);
		/* Bind result set columns to the local buffers */
		rc = SQLBindCol(hStmt, 1, SQL_C_CHAR, chval1, 128, (SQLINTEGER*)&ret1);
		rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval2, 128, (SQLINTEGER*)&ret2);
		/* Excecute the query and create a record set */
		rc = SQLExecute(hStmt);
		if (SQL_SUCCEEDED(rc))
		{
			printf("%s: Retrieve schema info for the given result set:\n", DAM);
			SQLNumResultCols(hStmt, &fieldCount);
			if (fieldCount > 0)
			{
				for (currentField = 1; currentField <= fieldCount; currentField++)
				{
					SQLDescribeCol(hStmt, currentField,
						colName, sizeof(colName), 0, 0, 0, 0, 0);
					printf(" | %s", colName);
				}
				printf("\n");
			}
			else
			{
				printf("%s: Error: Number of fields in the result set is 0.\n", DAM);
			}
			printf("%s: Fetch the actual data:\n", DAM);
			/* Loop through the rows in the result set */
			rc = SQLFetch(hStmt);
			while (SQL_SUCCEEDED(rc))
			{
				printf(" | %s | %s\n", chval1, chval2);
				rc = SQLFetch(hStmt);
				rowCount++;
			};
			printf("%s: Total Row Count: %d\n", DAM, rowCount);
			rc = SQLFreeStmt(hStmt, SQL_DROP);
		}
	}else
	{
		printf("%s: Couldn't connect to %s.\n", DAM, szDSN);
	}

	/* Disconnect and free up allocated handles */
	SQLDisconnect(hDbc);
	SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
	SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
	system("pause");
}

运行结果:


  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DaveBobo

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值