加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ADO:实现向Oracle数据库中插入数据

发布时间:2020-12-12 13:59:51 所属栏目:百科 来源:网络整理
导读:1、使用Command对象完成插入。 Command 对象定义了将对数据源执行的指定命令。该对象中常用的属性和方法如下: ⑴ ActiveConnection 属性:该属性指定 Command 对象当前所属的 Connection 对象; ⑵ CommandText 属性:该属性包含发送给数据提供者的命令文本

1、使用Command对象完成插入。

Command 对象定义了将对数据源执行的指定命令。该对象中常用的属性和方法如下:
⑴ ActiveConnection 属性:该属性指定 Command 对象当前所属的 Connection 对象;
⑵ CommandText 属性:该属性包含发送给数据提供者的命令文本,该属性的值可以是 SQL 语句、表名 或 存储过程名 的字符串。
⑶ CommandType 属性:指示 Command 对象的属性,其值可以取 CommandTypeEnum 中的某个值,取值类似 Recordset 对象的 Open 方法的 Options 参数。
⑷ Execute 方法:执行在 CommandText 属性中指定的查询、SQL 语句 或 存储过程。


使用 Command 对象的过程步骤:
⑴ 定义 _CommandPtr 型变量;
⑵ 实例化变量;
⑶ 设置变量的相关属性;

⑷ 调用 Execute 方法执行命令。

实例代码:

/*
    功能:使用 Command 对象向数据库中插入数据
*/
BOOL CRentDVDDlg::RentDVDByCommand(CString& strDVDID,CString& strRentName,CString& strRentDate)
{
	BOOL bRet = FALSE;
	HRESULT hr;

	/*实例化一个Command对象*/
	_CommandPtr pCommand;
	hr = pCommand.CreateInstance(__uuidof(Command));
	if(FAILED(hr))
	{
		MessageBox(_T("RentDVDByCommand:Command对象实例化失败!"));
		return FALSE;
	}

	/*关联当前连接*/
	pCommand->ActiveConnection = m_pConnection;

	/*组串*/
	CString strSQL;
	strSQL.Format(_T("insert into tbRentInfo(sDVDID,sName,sDate) values('%s','%s','%s')"),strDVDID,strRentName,strRentDate);
	pCommand->CommandText =_bstr_t(strSQL);

	/*执行插入操作*/
	try
	{
		_variant_t vRecords;
		hr = pCommand->Execute(&vRecords,NULL,adCmdText);
		if (SUCCEEDED(hr))
		{
			bRet = TRUE;
		}
	}
	catch(_com_error *e)
	{
		MessageBox(e->ErrorMessage());
	}

	return bRet;
}

2、使用Connection对象完成插入数据的操作,与上一种比较类似:

/*
    功能:使用 Connection 对象向数据库中插入数据
*/
BOOL CRentDVDDlg::RentDVDByConnection(CString& strDVDID,CString& strRentDate)
{
	BOOL bRet = FALSE;
	HRESULT hr;

	/*组串*/
	CString strSQL;
	strSQL.Format(_T("insert into tbRentInfo(sDVDID,strRentDate);

	/*执行插入操作*/
	try
	{
		_variant_t vRecords,RecordsAffected;
		hr = m_pConnection->Execute(_bstr_t(strSQL),&RecordsAffected,adCmdText);
		if (SUCCEEDED(hr))
		{
			bRet = TRUE;
		}
	}
	catch(_com_error *e)
	{
		MessageBox(e->ErrorMessage());
	}

	return bRet;
}

3、使用RecordSet对象完成插入数据操作,RecordSet对象的使用前面文章有介绍:

/*
    功能:使用 Connection 对象向数据库中插入数据
*/
BOOL CRentDVDDlg::RentDVDByConnection(CString& strDVDID,adCmdText);
		if (SUCCEEDED(hr))
		{
			bRet = TRUE;
		}
	}
	catch(_com_error *e)
	{
		MessageBox(e->ErrorMessage());
	}

	return bRet;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读