VS2010 用C++向access数据库一表中插入数据,该如何解决
VS2010 用C++向access数据库一表中插入数据
access数据库中student表已经存在,有sid,sname,sex,age,year,gpa字段,sid为主键,任务是向该表中插入5000条随机数据,随机数已经生成,但是不懂怎么插入到数据库中。。。
------解决方案--------------------
显然仅仅这样是不行的。。。。。
需要使用微软提供的数据库访问接口。
推荐使用ODBC。
用到两个类 CDatabase, CRecordset类
access数据库中student表已经存在,有sid,sname,sex,age,year,gpa字段,sid为主键,任务是向该表中插入5000条随机数据,随机数已经生成,但是不懂怎么插入到数据库中。。。
- C/C++ code
#include<iostream> #include<ctime> #include<cstdlib> #include<string> using namespace std; string sex; string sname; int sid; int year; int age; double gpa; void main() { srand(time(0)); int k= 5000; while(k-- != 0) { if(rand() % 2 == 0) { sex = "m"; } else { sex = "f"; } age = rand() % 5 + 20; year = rand() % 10 + 2000; gpa = rand() % 5; int i = rand() % 5 + 5; while(i-- != 0) { int k = rand() % 26 + 97; sname += static_cast<char>(k); } getchar(); }
------解决方案--------------------
显然仅仅这样是不行的。。。。。
需要使用微软提供的数据库访问接口。
推荐使用ODBC。
用到两个类 CDatabase, CRecordset类