ODBC以微软自带Access数据库举例:创建一个名为myds.mdb的文件,两个字段ID与NAME,数据自己随便填一些即可。
方法1:
需要手动添加数据源myds
- #include <stdio.h>
- #include <windows.h>
- #include <sql.h>
- #include <sqlext.h>
- int main()
- {
- HENV env;
- HDBC dbc;
- HSTMT stmt;
- //1.分配环境句柄
- SQLAllocEnv(&env);
- //2.分配连接句柄
- SQLAllocConnect(env,&dbc);
- //3.与数据库建立连接
- SQLConnect(dbc,(SQLCHAR*)"myds",SQL_NTS,NULL,0,NULL,0);
- //4.发送一个语句句柄
- SQLAllocStmt(dbc,&stmt);
- //5.使用语句句柄向数据库发送SQL语句
- SQLExecDirect(stmt,(SQLCHAR*)"SELECT * FROM myds",SQL_NTS);
- //6.得到查询结果并处理
- RETCODE code;
- SQLINTEGER sid;
- SQLCHAR name[100];
- memset(name,0,100);
- SDWORD size;
- for(code=SQLFetch(stmt);code==SQL_SUCCESS;code=SQLFetch(stmt)){
- //取出每一列并显示
- SQLGetData(stmt,1,SQL_C_ULONG,&sid,sizeof(sid),&size);
- SQLGetData(stmt,2,SQL_C_CHAR,name,sizeof(name),&size);
- printf("ID=%d,NAME=%s/n",sid,name);
- }
- //添加数据
- HSTMT stmt1; //不能再使用stmt了
- SQLAllocStmt(dbc,&stmt1);
- SQLExecDirect(stmt1,(SQLCHAR*)"INSERT INTO myds VALUES(10,'hahah')",SQL_NTS);
- SQLEndTran(SQL_HANDLE_DBC,dbc,SQL_COMMIT); //提交事物
- //7.释放资源:先创建的最后释放
- SQLFreeStmt(stmt1,SQL_CLOSE);
- SQLFreeStmt(stmt,SQL_CLOSE);
- SQLDisconnect(dbc);
- SQLFreeConnect(dbc);
- SQLFreeEnv(env);
- return 0;
- }
方法2:
代码内配置数据源myds
- #include <stdio.h>
- #include <windows.h>
- #include <sql.h>
- #include <sqlext.h>
- #include <odbcinst.h>
- int main()
- {
- HENV env;
- HDBC dbc;
- HSTMT stmt;
- //1.分配环境句柄
- SQLAllocEnv(&env);
- //2.分配连接句柄
- SQLAllocConnect(env,&dbc);
- /*
- //3.通过程序来添加数据源:需要添加odbcinst.h头文件
- SQLConfigDataSource(0,
- ODBC_ADD_DSN,
- "Driver do Microsoft Access (*.mdb)",
- "DSN=myds/0"
- "DEFAULTDIR=D://rar//rar//Tuesday//odbc/0"
- "DBQ=D://rar//rar//Tuesday//odbc//myds.mdb/0");
- */
- //设置连接的属性
- SQLSetConnectAttr(dbc,SQL_ATTR_TRACEFILE,"trace.txt",SQL_NTS);//此步必须在前
- SQLSetConnectAttr(dbc,SQL_ATTR_TRACE,(SQLPOINTER)SQL_OPT_TRACE_ON,SQL_IS_INTEGER);
- //4.与数据库建立连接
- SQLConnect(dbc,(SQLCHAR*)"myds",SQL_NTS,NULL,0,NULL,0);
- //5.发送一个语句句柄
- SQLAllocStmt(dbc,&stmt);
- //6.使用语句句柄向数据库发送SQL语句
- SQLExecDirect(stmt,(SQLCHAR*)"SELECT * FROM myds",SQL_NTS);
- //7.得到查询结果并处理
- RETCODE code;
- SQLINTEGER sid;
- SQLCHAR name[100];
- memset(name,0,100);
- SDWORD size;
- for(code=SQLFetch(stmt);code==SQL_SUCCESS;code=SQLFetch(stmt)){
- //取出每一列并显示
- SQLGetData(stmt,1,SQL_C_ULONG,&sid,sizeof(sid),&size);
- SQLGetData(stmt,2,SQL_C_CHAR,name,sizeof(name),&size);
- printf("ID=%3d , NAME=%s/n",sid,name);
- }
- //添加数据
- HSTMT stmt1; //不能再使用stmt了
- SQLAllocStmt(dbc,&stmt1);
- SQLExecDirect(stmt1,(SQLCHAR*)"INSERT INTO myds VALUES(10,'hahah')",SQL_NTS);
- SQLEndTran(SQL_HANDLE_DBC,dbc,SQL_COMMIT); //提交事物
- //8.释放资源:先创建的最后释放
- SQLFreeStmt(stmt1,SQL_CLOSE);
- SQLFreeStmt(stmt,SQL_CLOSE);
- SQLDisconnect(dbc);
- SQLFreeConnect(dbc);
- SQLFreeEnv(env);
- return 0;
- }
方法3:
使用MFC编程
- #include <afxdb.h>
- #include <stdio.h>
- class MyRecordSet : public CRecordset
- {
- public:
- CString m_name;
- CString m_id;
- // int m_id;
- public:
- MyRecordSet(CDatabase *db):CRecordset(db){
- m_nFields = 2; //字段的数目
- }
- void DoFieldExchange(CFieldExchange* pFX){
- pFX->SetFieldType(CFieldExchange::outputColumn);
- RFX_Text(pFX, "ID", m_id); //ID与NAME顺序不能错
- // RFX_Int(pFX,"ID", m_id);
- RFX_Text(pFX, "NAME", m_name);
- }
- };
- void query()
- {
- try{
- //1.与指定数据源建立连接
- CDatabase db;
- db.OpenEx("DSN=myds;UID=;PWD=");
- //2.数据库操作
- //查询
- MyRecordSet rs(&db);
- rs.Open(CRecordset::dynaset,"SELECT * FROM myds",
- CRecordset::none
- //CRecordset::readOnly
- );//发送SQL语句
- CString id;
- CString name;
- while(!rs.IsEOF()){
- rs.GetFieldValue((short)0,id);
- rs.GetFieldValue((short)1,name);
- printf("%s , %s/n",id,name);
- rs.MoveNext();
- }
- //更新--update
- rs.MoveFirst();
- rs.Edit();
- rs.m_name = "zhang"; //因为这里才创建了子类MyRecordSet
- rs.Update();
- //第二次查询
- printf("/n2nd query:/n");
- rs.m_strFilter = "id>5";
- rs.Requery();
- while(!rs.IsEOF()){
- rs.GetFieldValue((short)0,id);
- rs.GetFieldValue((short)1,name);
- printf("%s , %s/n",id,name);
- rs.MoveNext();
- }
- //3.关闭
- rs.Close();
- db.Close();
- }
- catch (CDBException *e) {
- printf("%s",e->m_strError);
- }
- }
- void add()
- {
- CDatabase db;
- try{
- db.OpenEx("DSN=myds;UID=;PWD=");
- //事务处理
- db.BeginTrans();
- db.ExecuteSQL("INSERT INTO myds VALUES(15,'what')");
- db.ExecuteSQL("INSERT INTO myds VALUES(20,'sss')");
- db.CommitTrans();
- }
- catch (CDBException *e) {
- printf("%s",e->m_strError);
- db.Rollback();
- }
- }
- int main()
- {
- // add();
- query();
- return 0;
- }
源文档 <http://blog.csdn.net/leopardaa521/article/details/4420451>