vc++ 下 通过ado 操作oracle 源码程序,打开 查询插入

1. 数据库安装好,配置好客户端

2 实现了打开数据库,查找,执行接口

有些也太不厚道,想找个资源到处是要分的,还不知道能不能用,直接贴出来补更好么。

下面是源码,包括三部分 类的头文件,源文件和操作例子:

头文件


#ifndef		SYSTEMMODEL_DAL_SQLDB_H_
#define	SYSTEMMODEL_DAL_SQLDB_H_

#include <string>
#include <iostream>
using namespace std;


#include <Windows.h>
#import "C:\\Program Files\\Common Files\\System\\ADO\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
typedef _ConnectionPtr CONNECTION;
typedef _RecordsetPtr RECORDSET;

typedef enum
{
	FAILURE = -1,
	SUCCESS = 0
}STATUS;

class SQLDB
{
public:
	
	SQLDB(string dbName, string userName, string pwd);

	//Destruct the SQLDB
	~SQLDB();

	//Open the SQL DataBase
	STATUS Open();
	//Search data
	RECORDSET Search(char* sqlStr);
	//Execute the SQL
	void Execute(char* sqlStr);
	//Close the SQL DataBase
	void Close();
private:
	string conString;
	string nameUser;
	string pwdUser;
	string dbName;

	//Connection handle
	CONNECTION dbCon;
	bool closed;

};

#endif //SYSTEMMODEL_DAL_SQLDB_H_


 

 

源文件

#include "SQLDB.h"


//Construct the SQLDB
//conString:"Provider=SQLEXPRESS;Persist Security Info=False;User ID=sa;Password=tang1028;Initial Catalog=EmpeorSysDB"
SQLDB::SQLDB(string dbName, string userName, string pwd)
{
	::CoInitialize(NULL);
	conString = "Provider=MSDAORA.1;server=localhost;database=" + dbName;
	nameUser = userName;
	pwdUser = pwd;
	
	this->closed = false;
}

//Open the SQL DataBase 
STATUS SQLDB::Open()
{
	STATUS status = STATUS::FAILURE;
	try
	{
		dbCon.CreateInstance(__uuidof(Connection));
		dbCon->Open(conString.c_str(), nameUser.c_str(), pwdUser.c_str(), adModeUnknown);
	}
	catch (_com_error)
	{
		this->Close();
		return status;
	}

	status = STATUS::SUCCESS;

	return status;
}

//Destruct the SQLDB
SQLDB::~SQLDB()
{
	if (!this->closed)
	{
		this->closed = true;
		//Close the DB
		this->Close();
		//UnInit the Com
		::CoUninitialize();
	}
}

//Execute the SQL
RECORDSET SQLDB::Search(char* sqlStr)
{
	if (this->dbCon)
	{
		try
		{
			RECORDSET dbRecordSet = this->dbCon->Execute(sqlStr, NULL, adCmdText);
			return dbRecordSet;
		}
		catch (_com_error)
		{
			this->Close();
			return NULL;
		}
	}
	return NULL;
}

//Execute the SQL
void SQLDB::Execute(char* sqlStr)
{
	if (this->dbCon)
	{
		try
		{
			this->dbCon->Execute(sqlStr, NULL, adCmdText);
		}
		catch (_com_error)
		{
			this->Close();
			return;
		}
	}
}

//Close the SQL DataBase
void SQLDB::Close()
{
	if (this->dbCon)
	{
		this->dbCon->Close();
		this->dbCon = NULL;
	}
}


 

例子:

#include <iostream>
#include "SQLDB.h"

using namespace std;


string getStringValue(RECORDSET set,char * colName)
{
	string ret = "";
	_variant_t var;
	var = set->GetCollect(colName);  //可能为空
	if (var.vt != VT_NULL)   //为NULL
	{
		ret = (_bstr_t)var;
	}
	return ret;
}


int main()
{
	string dbName = "orcl";
	string userName = "SCOTT";
	string pwd = "orcl";

	char tablename[50] = "EMP";
	//Create a SQLDB handle 
	SQLDB* db = new SQLDB(dbName, userName, pwd);

	//Open the DB
	db->Open();
	
	char tmp[400];
	sprintf_s(tmp, "select * from %s", tablename);
	//Excute seraching
	RECORDSET set = db->Search(tmp);
	while (!set->EndOfFile)
	{
		int no = set->GetCollect("EMPNO");
		//std::string name = (_bstr_t)(set->GetCollect("ename"));
		std::string name = getStringValue(set,"ename");
		//std::string job = (_bstr_t)(set->GetCollect("JOB"));
		std::string job = getStringValue(set, "JOB");
		std::cout << no << " " << name << " " << job << std::endl;
		set->MoveNext();
	}

	//Close the DB
	db->Close();


	cout <<"\r\n"<<"Updating DB"<< endl;
	//Open the DB
	db->Open();
	//Excute seraching
	db->Execute("UPDATE EMP SET SAL=1000 where EMPNO=7369");
	//Close the DB
	db->Close();

	db->Open();
	sprintf_s(tmp,"insert into %s(EMPNO,ENAME,SAL) values('%d','%s','%d')",tablename,5002,"问问",1111);
	db->Execute(tmp);
	db->Close();

	cout << "\r\n"<<"Finish update db\r\n" << endl;

	//Delete the SQLDB handle
	delete db;

	cout << "\r\n" << endl;


	return 0;
}








 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值