c语言连接access数据库(odbc)方式

/*
该代码是c通过ODBC直接读取数据库,从微软下载的,经过本人在vc6++下面严格测试过。
不需要单独配置ODBC驱动
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\test.mdb;";
test.mdb修改为你的access数据库所在的路径。
如果你的数据库是access2007以下版本去掉*.accdb
unsigned char* query = "SELECT Customers.[Company], Customers.[First Name] FROM Customers;";
这里修改sql语句。注意两句
*/
#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=C:\\test.mdb;";
//char szDSN[256] =
"Driver={Microsoft Access Driver (*.mdb)};DSN='';DBQ=C:\\test.mdb;";
main()
{
HENV    hEnv;
HDBC    hDbc;
/* ODBC API return status */
新河东狮吼下载RETCODE rc;
int    iConnStrLength2Ptr;
char    szConnStrOut[256];
unsigned char* query = "SELECT Customers.[Company], Customers.[First Name] FROM Customers;";
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 'Northwind 2007.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");
semm7
}
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 */
motionjpegrc = 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);
printf("%s: Cleanup. Done.\n", DAM);
}

本文发布于:2024-09-22 13:39:05,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/571440.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据库   版本   驱动   语句
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议