当前位置:首页 » 数据仓库 » c连接access数据库代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c连接access数据库代码

发布时间: 2023-06-01 16:18:38

c语言如何连接Access数据库

看视频上是连接Mysql数据库,MySQL安装后配置比较麻烦,所以我试着用老师给的模板连接Access数据库,Access是比较方便使用的 只要装了office几乎都有. 首先在网络搜索odbc 连接字符串 找到的是asp连接Access的方法,但是ODBC是通用的,所以直接拿过来也能用. 将 SQLCHAR ConnStrIn[MAXBUFLEN] = “DRIVER={MySQL ODBC 5.1 Driver};SERVER=127.0.0.1;UID=root;PWD=root;DATABASE=test;CharSet=gbk;”; 部分改成 SQLCHAR ConnStrIn[MAXBUFLEN] = “Driver={Microsoft Access Driver (*.mdb)};Dbq=//mydatabase.mdb;Uid=Admin;Pwd=;CharSet=gbk;”; Dbq指的是你的数据库所在路劲 当然之前你应该在c:/下新建一个数据库名为mydatabase.mdb,而且如果后面的SQL操作语句是老师写的 result = SQLPrepare(hstmt,(SQLCHAR*)“insert into T_Person(FAge,FName) values(20,'kider')”,SQL_NTS); 那么你也要在之前建立好一个表T_Person,和表中的两个字段FAge,FName.否则会出错. 怎么样,已经成功链接到Access了吧,接下来你就可以发扬广大了,做个管理系统之类的东东.!

㈡ c语言连接access

如果是纯昌野C语链辩言的话需要引用头文件。
引用iostream.h以及msado15.dll方可进行棚迅缺连接ACCESS数据库的操作。

代码量比较大,这里贴不出来。需要的话可以联系我。

㈢ c++怎么连接access数据库

环境配置:
1. 先安装你的Access数据库(微软Office 2016中含有或者单独下载,下载地址:http://msdn.itellyou.cn/ 左侧选择应用程序一栏,注意选择下载64位,即标有X64的)
特殊情况查看意外解决方案 (A)
2. 安装Microsoft Access database engine 2007,他的作用暂时可理解为我们所写的程序与Access之间的沟通桥梁--“传话人”。
Microsoft Access database engine 2010(可以安装,但后面的代码是2007的,所以暂时建议使用2007)
下载地址:https://www.microsoft.com/zh-cn/download/details.aspx?id=13255
Microsoft Access database engine 2007
下载地址:https://www.microsoft.com/zh-cn/download/details.aspx?id=23734
特殊情况查看意外解决方案 (C)

连接与测试代码:
本例代码使用VS2010以上版本测试
建议去文末下载写好的含注释的测试代码看。

1. 新建你的数据库表
打开Access
选择空白数据库,为你的数据库中表项起一个名字 如 Test
设计你的存储表格
本例附有测试表格database1.accdb
2.
在你程序的stdafx.h头文件中加入
#import"C:/ProgramFiles/CommonFiles/System/ADO/msado15.dll"
rename("EOF","adoEOF"), rename("BOF","adoBOF")
(如果加载失败的话,去c:\program files\common files\system\ado查看是否有msado15.dll)
3.测试代码介绍
// LinkDBForHX.cpp : 定义控制台应用程序的入口点。

[cpp] view plain

#include "stdafx.h"
#include<iostream>//atlstr
#include<atlstr.h>
using namespace ADODB;
using namespace std;

class AdoAccess {
public:
_ConnectionPtr HX_pConnection; // 数据库指针
_RecordsetPtr HX_pRecordset; // 命令指针
_CommandPtr HX_pCommand; // 记录指针
void OnInitADOConn() {
::CoInitialize(NULL);
try
{
HX_pConnection.CreateInstance(__uuidof(Connection));//等价于 //HX_pConnection.CreateInstance("ADODB.Connection");
//此句包含定位你数据库的所需的访问信息
_bstr_t strConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.accdb;Persist Security Info=False ";
//测试//_bstr_t strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database1.accdb;Persist Security Info=False";//此句包含定位你数据库的所需的访问信息
//测试//_bstr_t strConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Driver = { Microsoft Access Driver(*.mdb, *.accdb) }; DBQ =Database1.accdb;Persist Security Info=False ";
//此句包含定位你数据库的所需的访问信息
//连接master数据库,无密码。
HX_pConnection->Open(strConnect,"","",adModeUnknown); //连接数据库

cout << "连接成功,并获得智能指针" << endl;
}
catch (_com_error e)
{
cout <<e.Description() << endl;
}

}
void ExitConnect() {
if (HX_pRecordset != NULL)
HX_pRecordset->Close();
HX_pConnection->Close();
::CoUninitialize();
}
};

int main()
{
AdoAccess dataBase;

//初始化
dataBase.OnInitADOConn();
dataBase.HX_pRecordset.CreateInstance("ADODB.Recordset");

//选择名为Test的表格
dataBase.HX_pRecordset->Open("select * from Test",
dataBase.HX_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);

int i = 0;
//读取其中的name字段所有数据,并显示
while (!dataBase.HX_pRecordset->adoEOF)
{

_variant_t var;
string strValue;
var = dataBase.HX_pRecordset->GetCollect("name");
//其他类似,都是通过HX_pRecordset传递SQL语句执行查找、添加、删除等命令

if (var.vt != VT_NULL)
strValue = _com_util::ConvertBSTRToString((_bstr_t)var);

++i;
cout << "name " << i << " " << strValue << endl;;

dataBase.HX_pRecordset->MoveNext();
}

//卸载com组件
dataBase.ExitConnect();
int xxxx = 0;
cout << "输入数字结束\n";
cin >> xxxx;
return 0;
}
意外情况解决方案
A 描述:未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序。
请正确安装环境配置中的第一步
B 若A无法解决问题。
选择 该应用程序的 应用程序池 ------>选择高级设置--------->启用32位应用程序 ------->true
操作如下:
按下win + R
在弹出的对话框中输入inetmgr,然后按回车键
选择最左侧
点击应用程序池
再点击下图的DefaultAPPPool
选择高级设置
选择启用32位应用程序 ---置为True.
最后选择确认保存
C 重新安装 Microsoft Access databaseengine 2007
D 程序若提示缺少符号集,请下载相关符号集或者在联网状态下动态加载

㈣ 怎样用C++连接并使用access数据库

使用C++编程语言,连接对Access数据库进行操作,常用的方法有DAO和ADO两种方式,本文将介绍采用ADO的方式方位Access数据库。
先介绍一下ADO,ADO (ActiveX Data Objects) 是一个用于存取数据源的COM组建。它提供了编程语言和统一数据访问方式OLE DB的一个中间层。允许开发人员编写访问数据的代码而不用关心数据库是如何实现的,而只用关心到数据库的连接。
在程序的开始,首先导入所需要的库:#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF") ,这里重命名EOF是必要的,因为典型的VC应用都已经定义了EOF作为常数-1。
完整的程序如下,以注释的形式来对程序进行解释:
_ConnectionPtr m_pConnection; //连接access数据库的链接对象
_RecordsetPtr m_pRecordset; //结果集对象
CoInitialize(NULL); //初始化
m_pConnection.CreateInstance(__uuidof(Connection)); //实例化对象

//连到具体某个mdb ,此处的的Provider语句因Access版本的不同而有所不同。
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyAccess.mdb","","", adModeUnknown);
}
catch(_com_error e)
{
AfxMessagebox(_T("数据库连接失败!"));
return;
}

m_pRecordset.CreateInstance(__uuidof(Recordset)); //实例化结果集对象
//执行sql语句
try
{
CString sql= _T("select * from Patient");
m_pRecordset->Open(sql, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
if(m_pConnection->State)
{
m_pConnection->Close();
m_pConnection= NULL;
}
return ;
}
//处理结果集
try
{
//若结果为空,结束
if(m_pRecordset->BOF)
{
AfxMessageBox_T(("表内数据为空!"));
if(m_pConnection->State)
{
m_pRecordset->Close();
m_pRecordset = NULL;
m_pConnection->Close();
m_pConnection= NULL;
}
return ;
}
//游标定位到第一条记录
m_pRecordset->MoveFirst();
_variant_t var; //从结果集中取出的数据放到var中
char *name;
while(!m_pRecordset->adoEOF)
{
var= m_pRecordset->GetCollect("Name"); //要取字段的名称。
if(var.vt != VT_NULL)
{
name= _com_util::ConvertBSTRToString((_bstr_t)var); //转换成char*类型
}
string MyName = name;
m_pRecordset->MoveNext();
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMssage());
}
//退出程序时的处理 ,关闭数据库的相关操作
if(m_pConnection->State)
{
m_pRecordset->Close();
m_pRecordset = NULL;
m_pConnection->Close();
m_pConnection= NULL;
}

㈤ C或C++语言连接ACCESS数据库代码是什么

#include<stdio.h>
#include<string.h>
typedef struct{
char name[20];
int number;
int grade;
int class;
float mark[10];
float average;
}T;
void show(T *student,int *tp,int n) /* 把成绩显示在屏幕上 */
{
int i,j;
char a[3]=" ";
printf("***********************************************************\n");
printf(" name number grade class average order\n");
for(i=0;i<n;i++)
{
printf("-----------------------------------------------------------\n");
printf("%d:\n",i+1);
printf(" %s %d %d %d %f %d\n",student[tp[i]].name,student[tp[i]].number,student[tp[i]].grade,student[tp[i]].class,student[tp[i]].average,tp[i]+1);
printf("mark:");
for(j=0;j<2;j++)
printf("%s%d:%f",a,j+1,student[tp[i]].mark[j]);
printf("\n");
}
printf("***********************************************************\n");
printf("\n\n\n");
}
void writefile(T *student,int n) /* 把成绩存在磁盘上 */
{
FILE *fp;
int i,j;
if((fp=fopen("d:\\kanwei.txt","w+"))==NULL)
{
printf("can't open file");
exit(0);
}
for(i=0;i<=n;i++)
{
fprintf(fp,"%s %d %d %d ",student[i].name,student[i].number,student[i].grade,student[i].class);
for(j=0;j<2;j++)
fprintf(fp,"%f ",student[i].mark[j]);
fprintf(fp,"%f\n",student[i].average);
}
fclose(fp);
}
void students(T *student,T *temp,int m,int n,int k) /* 实现两个结构体的拷贝 */
{
int i;
if(k==0)
{
strcpy(student[n].name,temp[m].name);
student[n].number=temp[m].number;
student[n].grade=temp[m].grade;
student[n].class=temp[m].class;
for(i=0;i<2;i++)
student[n].mark[i]=temp[m].mark[i];
student[n].average=temp[m].average;
}
else if(k==1)
{
strcpy(temp[m].name,student[n].name);
temp[m].number=student[n].number;
temp[m].grade=student[n].grade;
temp[m].class=student[n].class;
for(i=0;i<2;i++)
temp[m].mark[i]=student[n].mark[i];
temp[m].average=student[n].average;
}
}
void addfile(T *student,int n) /* 加入新学生到文件 */
{
T temp[2];
int i,j=1;
float ave=0.0;
printf("Please input the student:\n");
printf(" name number grade class mark1 mark2\n");
printf("****************************************************\n");
scanf("%s",temp[0].name);
scanf("%d",&temp[0].number);
scanf("%d",&temp[0].grade);
scanf("%d",&temp[0].class);
for(i=0;i<2;i++)
{
scanf("%f",&temp[0].mark[i]);
ave=ave+temp[0].mark[i];
}
temp[0].average=ave=ave/2;
i=0;
while(ave<=student[i].average&&i<n)
i++;
students(student,temp,j%2,i,1); /* temp[j/2]=student[i]; */
students(student,temp,(j+1)%2,i,0); /* student[i]=stu; */
for(;i<n;i++)
{
j++;
students(student,temp,j%2,i+1,1); /* temp[(i+2)/2]=student[i+1]; */
students(student,temp,(j+1)%2,i+1,0); /* student[i+1]=temp[(i+1)/2]; */
}
writefile(student,i);
}
void showall(T *student,int n) /* 显示文件中所有的学生 */
{
int i;
int a[30];
for(i=0;i<n;i++)
a[i]=i;
show(student,a,n);
}
int find(T *student,int n,int *tp) /* 在文件中查询学生可以多行查询 */
{
int k,im=0,i,m,num,gra,clas;
char na[20];
float ord;
printf("*******************************\n");
printf(" name n&g&c ave order\n");
printf(" 1 2 3 4 \n");
printf("*******************************\n");
scanf("%d",&k);
switch(k)
{
case 1:
scanf("%s",&na);
for(i=0;i<n;i++)
{
if(strcmp(student[i].name,na)==0)
{
tp[im++]=i;
}
}
break;
case 2:
scanf("%d%d%d",&num,&gra,&clas);
for(i=0;i<n;i++)
{
if(student[i].number==num&&student[i].grade==gra&&student[i].class==clas)
{
tp[im++]=i;
}
}
break;
case 3:
scanf("%f",&ord);
for(i=0;i<n;i++)
{
if(ord==student[i].average)
{
tp[im++]=i;
}
}
break;
case 4:
scanf("%d",&m);
if(m<=n)
{
tp[im++]=m-1;
}
break;
case 5:
break;
default:
printf("error operate!\n");
exit(0);
}
if(im>=1)
show(student,tp,im);
if(im==0&&k<5&&k>=1)
printf("cant find!\n");
return(im);
}
dele(T *student,int n,int *tp) /* 对某个学生进行删除 */
{
int j;
printf("choose the student:\n");
j=find(student,n,tp);
if(j>=1)
{
if(j>1)
{
printf("Which one do you want to choose?\n");
scanf("%d",&j);
j=tp[j-1];
}
else
j=tp[0];
for(;j<n-1;j++)
students(student,student,j+1,j,0);
writefile(student,j-1);
}
}
void modify(T *student,int n,int *tp) /* 对某个学生进行修改 */
{
dele(student,n,tp);
addfile(student,n-1);
}
void readfile(int m) /* 读取文件中的数据,程序的基础 */
{
FILE *fp;
T student[30];
float mark[10],ave;
int i=0,j,tp[20];
if((fp=fopen("d:\\kanwei.txt","a+t"))==NULL)
{
printf("can't open file");
exit(0);
}
while(fscanf(fp,"%s%d%d%d",student[i].name,&student[i].number,&student[i].grade,&student[i].class)!=EOF)
{

for(j=0;j<2;j++)
{
fscanf(fp,"%f",&mark[j]);
student[i].mark[j]=mark[j];
}
fscanf(fp,"%f",&ave);
student[i].average=ave;
i++;
}
fclose(fp);
switch(m)
{
case 1:
find(student,i,tp);
break;
case 2:
addfile(student,i);
break;
case 3:
dele(student,i,tp);
break;
case 4:
modify(student,i,tp);
break;
case 5:
showall(student,i);
break;
default:
exit(0);
}
}
main() /* 主程序 */
{
int i=1;
while(i)
{
printf(" Choose the operate:\n");
printf("******************************************************\n");
printf(" find add delete modify showall exit\n");
printf(" 1 2 3 4 5 0\n");
printf("******************************************************\n");
scanf("%d",&i);
readfile(i);
}
}
(这是一个关于成绩系统的,下面的可以参照,我也不知道是做什么的。)

用ODBC吧,不过还是要用到MFC..知道创建数据源吗? 首先创建一个名为rsgl(举例而已,自己取个)的数据源连接数据库,然后写如下代码通过数据源访问数据库:

C/C++ code

#include "afxdb.h"

//---------------------------------------------------------------
// Create and open a database object;
// do not load the cursor library
CDatabase db;
//db.OpenEx( NULL, CDatabase::forceOdbcDialog );
db.OpenEx( "DSN=[color=#FF0000]rsgl[/color];UID=;PWD=", CDatabase::noOdbcDialog );

// Create and open a recordset object
// directly from CRecordset. Note that a
// table must exist in a connected database.
// Use forwardOnly type recordset for best
// performance, since only MoveNext is required
CRecordset rs( &db );
rs.Open( CRecordset::forwardOnly,
_T( "SELECT * FROM system_table" ) );

// Create a CDBVariant object to
// store field data
CDBVariant varValue;

// Loop through the recordset,
// using GetFieldValue and
// GetODBCFieldCount to retrieve
// data in all columns
short nFields = rs.GetODBCFieldCount( );
while( !rs.IsEOF( ) )
{
for( short index = 0; index < nFields; index++ )
{
rs.GetFieldValue( index, varValue );
// do something with varValue
AfxMessageBox(*varValue.m_pstring);
}
rs.MoveNext( );
}

rs.Close( );
db.Close( );

(不知道有没有用,我其他地方找的。)

㈥ C#winform程序连接ACCESS数据库字符串.

1.采用独占方式进行连接:
"Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:\App1\你的数据库名.mdb; Exclusive=1; Uid=你的用户名; Pwd=你的密码;"
MS ACCESS OLEDB & OleDbConnection (.NET下的OleDb接口)进行链接
2.普通方式(最常用)连接ACCESS数据库:
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\App1\你的数据库名.mdb; User Id=admin; Password="
3.使用工作组方式(系统数据库)连接ACCESS数据库:
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\App1\你的数据库名.mdb; Jet OLEDB:System Database=c:\App1\你的系统数据库名.mdw"
4.连接到带有密码的ACCESS数据库:
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\App1\你的数据库名.mdb; Jet OLEDB:Database Password=你的密码"
5.连接到处于局域网主机上的ACCESS数据库:
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\Server_Name\Share_Name\Share_Path\你的数据库名.mdb"
6.连接到处于远程服务器上的ACCESS数据库:
"Provider=MS Remote; Remote Server=http://远程服务器IP; Remote Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\App1\你的数据库名.mdb"
MS ACCESS ODBC开放式接口连接字符串
7.标准链接:
"Driver= {MicrosoftAccessDriver(*.mdb)};DBQ=C:\App1\你的数据库名.mdb;Uid=你的用户名;Pwd=你的密码;"
如果ACCESS数据库未设置用户名和密码,请留空。下同。
8.WorkGroup方式(工作组方式)连接:
"Driver={Microsoft Access Driver (*.mdb)}; Dbq=C:\App1\你的数据库名.mdb; SystemDB=C:\App1\你的数据库名.mdw;"
9.采用独占方式进行连接:
"Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:\App1\你的数据库名.mdb; Exclusive=1; Uid=你的用户名; Pwd=你的密码;"
MS ACCESS OLEDB & OleDbConnection (.NET下的OleDb接口)进行链接
10.普通方式(最常用)连接ACCESS数据库:
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\App1\你的数据库名.mdb; User Id=admin; Password="
11.使用工作组方式(系统数据库)连接ACCESS数据库:
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\App1\你的数据库名.mdb; Jet OLEDB:System Database=c:\App1\你的系统数据库名.mdw"
12.连接到带有密码的ACCESS数据库:
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\App1\你的数据库名.mdb; Jet OLEDB:Database Password=你的密码"
13.连接到处于局域网主机上的ACCESS数据库:
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\Server_Name\Share_Name\Share_Path\你的数据库名.mdb"
14.连接到处于远程服务器上的ACCESS数据库:
"Provider=MS Remote; Remote Server=http://远程服务器IP; Remote Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\App1\你的数据库名.mdb"

㈦ c语言如何使用access数据库

1、C/C++与数据库交互,像 mssql/ mysql / oracle 等,一般都有成熟的第三方库,这些库里面无非就是封装了与数据库通讯的方式和通讯协议搜一下要用的数据库相关的 API 文档,会说得很清楚任何文件都是二进制数据,关键是数据存储的组织方式通用扩展名的文件,像gif/doc/jpg/wav,格式都是固定的。
2、举个例子,连接SQL:

// 打开数据库
strDBClass.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Jet OLEDB:Database Password=%s"), m_strUnEntryptMdbFilePath,m_strMDBPassword);
// 创建连接
HRESULT hr = m_pConnection.CreateInstance(_uuidof(Connection));
_ConnectionPtr m_pConnection->Open(m_strDBClass,_T(""),_T(""),adConnectUnspecified);
// 声明表单指针
_RecordsetPtr pBandRecordset;
pBandRecordset.CreateInstance(__uuidof(Recordset));
// 执行语句
CString strSQL(L"SELECT * FROM [Band]");
m_pConnection->Execute((LPCTSTR)strSQL,NULL,0);
// 提取某一项 例如BandInfo
int iBandInfo = wcscmp(colum, L"BandInfo");
while(!recordsetPtr->adoEOF)
{
var = recordsetPtr->GetCollect(colum);
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
recordsetPtr->MoveNext();
}