C或C++语言连接ACCESS数据库代码是什么
发布网友
发布时间:2022-05-01 19:21
我来回答
共1个回答
热心网友
时间:2022-06-21 20:28
#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( );
(不知道有没有用,我其他地方找的。)