这两天要写一个与oracle存储过程相关的测试程序,可恨自己对着一无所知,探索两天终于出来了,记录之,备以后留用。啦啦啦啦~~~~
first:连接oracle数据库。
工具:oracle 11g
环境:我的数据库存在于虚拟机的远程主机上,所以要进行远程连接。需要在自己的电脑上装备上 Oracle 的客户端。设置数据源。
设置数据源步骤:1,安装好oracle client 11g 后,打开 net manger,选择本地-》服务命名,点击左边的绿色“+”号。
填入网络服务名字(设置odbc数据源时要用到);
然后一步一步往下走,结束记得测试连接和保存哦。
2.设置odbc数据源,可以用管理页面的打开,也可以用oracle数据管理员。
点击添加,选择oracle 11g 选项, 设置
其中tns 为 net manger 设置的网络服务名,dsn 为 数据库名称。测试连接,并点击ok。 自此数据源设置ok。
3,下面打开vs,开始用ado连接数据库
#import "C://program files//common files//system//ado//msado15.dll" no_namespace rename ("EOF", "adoEOF") 加入库,并重命名结束符号
(64位系统编写是请使用,#import "C:\Program Files (x86)\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF") 否则会出现 存储过程错误)
_ConnectionPtr m_cConnectPtr;
_CommandPtr m_pCommand;
声明两个参数。然后对ado 组件进行初始化:CoInitialize(NULL); //初始化Com组件
当然,离开的时候要进行反初始化,CoUninitialize();
接下去是连接数据库:
HRESULT hr = m_cConnectPtr.CreateInstance(__uuidof(Connection));
if (FAILED(hr))
{
AfxMessageBox("Create ADO Connection Failed!");
return;
}
try
{
hr = m_cConnectPtr->Open("Provider=OraOLEDB.Oracle.1;Data Source=oracle_epolice", "epoliceadmin", "epoliceadmin", adModeUnknown);
//其中,ds为odbc数据源设置的tsn和netmanger设置的网络服务名,然后就是用户和密码了。
if (FAILED(hr))
{
AfxMessageBox("Cannot connect to database!");
}
}
catch(_com_error e) //捕捉异常
{
AfxMessageBox(e.Description());
return;
}
AfxMessageBox("connect success");
ok,数据库连接上,则可以开始对数据库的操作了。当昨晚数据库操作后,要记得断开连接哦:m_cConnectPtr->Close();
3,我做的是数据库调用有参数的存储过程,程序如下:
void COracle_firstDlg::OnBtnBegin()
{
_ParameterPtr param = NULL;
_bstr_t bstrSP("{CALL PROC_INSERT_EP_PASSVEHICLE(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}" );
try
{
m_pCommand.CreateInstance(__uuidof(Command));
m_pCommand->ActiveConnection = m_cConnectPtr; // 将库连接赋于它
m_pCommand->CommandText = bstrSP;
m_pCommand->CommandType = adCmdText;
m_cConnectPtr->Properties->Refresh(); //重要步骤
create_param4proc_vchar("in_pass_index",m_pass_index.GetLength(),m_pass_index.GetBuffer(m_pass_index.GetLength()));
create_param4proc_vchar("in_chan_index",m_chan_index.GetLength(),m_chan_index.GetBuffer(m_chan_index.GetLength()));
create_param4proc_vchar("in_pass_direction",m_pass_direction.GetLength(),m_pass_direction.GetBuffer(m_pass_direction.GetLength()));
create_param4proc_vchar("in_pass_licence_code",m_pass_lience_code.GetLength(),m_pass_lience_code.GetBuffer(m_pass_lience_code.GetLength()));
create_param4proc_vchar("in_pass_licence_type",m_pass_lience_type.GetLength(),m_pass_lience_type.GetBuffer(m_pass_lience_type.GetLength()));
create_param4proc_data("in_pass_time",m_pass_time);
create_param4proc_num("in_pass_real_speed",m_pass_real_speed.GetLength(),m_pass_real_speed.GetBuffer(m_pass_real_speed.GetLength()));
create_param4proc_num("in_pass_length",m_pass_length.GetLength(),m_pass_length.GetBuffer(m_pass_length.GetLength()));
create_param4proc_vchar("in_pass_licence_color",m_pass_lience_color.GetLength(),m_pass_lience_color.GetBuffer(m_pass_lience_color.GetLength()));
create_param4proc_vchar("in_pass_type",m_pass_type.GetLength(),m_pass_type.GetBuffer(m_pass_type.GetLength()));
create_param4proc_blob("in_pass_part_pic",m_pass_real_pic.GetBuffer(m_pass_real_pic.GetLength()));
create_param4proc_blob("in_pass_full_pic",m_pass_full_pic.GetBuffer(m_pass_full_pic.GetLength()));
create_param4proc_blob("in_pass_licence_pic",m_pass_lience_pic.GetBuffer(m_pass_lience_pic.GetLength()));
create_param4proc_vchar("in_pass_record_type",m_pass_record_type.GetLength(),m_pass_record_type.GetBuffer(m_pass_record_type.GetLength()));
create_param4proc_vchar("in_pass_deal_mark",m_pass_deal_mark.GetLength(),m_pass_deal_mark.GetBuffer(m_pass_deal_mark.GetLength()));
create_param4proc_vchar("in_roadway_index",m_roadway_index.GetLength(),m_roadway_index.GetBuffer(m_roadway_index.GetLength()));
create_param4proc_blob("in_pass_option_pic",m_pass_option_pic.GetBuffer(m_pass_option_pic.GetLength()));
create_param4proc_blob("in_pass_splice_pic",m_pass_splice_pic.GetBuffer(m_pass_splice_pic.GetLength()));
create_param4proc_vchar("in_pic_asso_num",m_pic_asso_num.GetLength(),m_pic_asso_num.GetBuffer(m_pic_asso_num.GetLength()));
create_param4proc_vchar("in_full_pic_md5",m_full_pic_md5.GetLength(),m_full_pic_md5.GetBuffer(m_full_pic_md5.GetLength()));
create_param4proc_vchar("in_licence_pic_md5",m_lience_pic_md5.GetLength(),m_lience_pic_md5.GetBuffer(m_lience_pic_md5.GetLength()));
create_param4proc_vchar("in_splice_pic_md5",m_splice_pic_md5.GetLength(),m_splice_pic_md5.GetBuffer(m_splice_pic_md5.GetLength()));
m_pCommand->Properties->Refresh(); //重要步骤
//运行存储过程
PropertyPtr prop = m_pCommand->Properties->GetItem("SPPrmsLOB");
prop->PutValue(_variant_t(VARIANT_TRUE,VT_BOOL)) ;
m_pCommand->Execute(NULL,NULL, adExecuteNoRecords);
prop = m_pCommand->Properties->GetItem("SPPrmsLOB");
prop->PutValue(_variant_t(VARIANT_FALSE,VT_BOOL)) ;
}
catch (_com_error e)
{
AfxMessageBox(e.Description());
return ;
}
AfxMessageBox("instert sucess");
}
以下是对4中参数的用法:
int COracle_firstDlg::create_param4proc_vchar(char* proc_param,int length,char* value)
{
_ParameterPtr param = NULL;
param = m_pCommand->CreateParameter((_bstr_t)proc_param,adVarChar,adParamInput,length,(_variant_t)value);
m_pCommand->Parameters->Append(param);
return TRUE;
}
int COracle_firstDlg::create_param4proc_data(char* proc_param,COleDateTime m_data)
{
_ParameterPtr param = NULL;
// COleDateTime co_data;
// co_data.ParseDateTime(m_data);
_variant_t vardata = m_data;
param = m_pCommand->CreateParameter((_bstr_t)proc_param,adDate,adParamInput,20,vardata);
m_pCommand->Parameters->Append(param);
return TRUE;
}
int COracle_firstDlg::create_param4proc_blob(char* proc_param,char* pic_addr)
{
_ParameterPtr param = NULL;
//读取一张图片
_variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
_StreamPtr adostream;
adostream.CreateInstance(_T("ADODB.Stream"));
adostream->Type = adTypeBinary;
adostream->Open(varOptional,adModeUnknown, adOpenStreamUnspecified, _T(""), _T(""));
adostream->LoadFromFile(pic_addr);
_variant_t vReadTo = adostream->Read(adReadAll);
long blob_size = adostream->GetSize();
adostream->Close();
param = m_pCommand->CreateParameter((_bstr_t)proc_param,adLongVarBinary,adParamInput,blob_size,vReadTo);
m_pCommand->Parameters->Append(param);
return TRUE;
}
int COracle_firstDlg::create_param4proc_num(char* proc_param,int length,char* value)
{
_ParameterPtr param = NULL;
param = m_pCommand->CreateParameter((_bstr_t)proc_param,adInteger,adParamInput,4,(_variant_t)value);
m_pCommand->Parameters->Append(param);
return TRUE;
}