Visual C++访问数据库能不能利用预编译SQL?

middle 2002-07-14 06:46:02
在JDBC中有PreparedSentence,用来把SQL语句的语法树保存在服务器端,以便提高类似查询的速度。请问VC访问数据库有没有这样的功能?ODBC,ADO, OLE DB都可以。
...全文
79 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
蒋晟 2002-07-18
  • 打赏
  • 举报
回复
ADOX 2.7

See Also
Command Property

© 1998-2001 Microsoft Corporation. All rights reserved.
Command and CommandText Properties Example (VC++)
The following code demonstrates how to use the Command property to update the text of a procedure.

// BeginCommandTextCpp
#import "c:\Program Files\Common Files\system\ado\msadox.dll" \
no_namespace
#import "c:\Program Files\Common Files\system\ado\msado15.dll"

#include "iostream.h"
#include "stdio.h"
#include "conio.h"

//Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void ProcedureTextX(void);

//////////////////////////////////////////////////////////
// //
// Main Function //
// //
//////////////////////////////////////////////////////////
void main()
{
if(FAILED(::CoInitialize(NULL)))
return;

ProcedureTextX();

::CoUninitialize();
}

//////////////////////////////////////////////////////////
// //
// ProcedureTextX Function //
// //
//////////////////////////////////////////////////////////
void ProcedureTextX()
{
HRESULT hr = S_OK;

// Define ADOX object pointers.
// Initialize pointers on define.
// These are in the ADOX:: namespace.
_CatalogPtr m_pCatalog = NULL;

// Define ADODB object pointers.
ADODB::_ConnectionPtr m_pCnn = NULL;
ADODB::_CommandPtr m_pCommand = NULL;

try
{
//Open the Connection
TESTHR(hr = m_pCnn.CreateInstance(__uuidof(ADODB::Connection)));
TESTHR(hr = m_pCatalog.CreateInstance(__uuidof(Catalog)));
TESTHR(hr = m_pCommand.CreateInstance(__uuidof(ADODB::Command)));
m_pCnn->Open("Provider=Microsoft.Jet.OLEDB.4.0;"
"data source=c:\\Program Files\\Microsoft Office"
"\\Office\\Samples\\Northwind.mdb;","","",NULL);

//Open the catalog
m_pCatalog->PutActiveConnection(_variant_t((IDispatch *)m_pCnn));

//Get the Command
m_pCommand = m_pCatalog->Procedures->GetItem("CustomerById")->GetCommand();

//Update the CommandText
m_pCommand->PutCommandText("PARAMETERS [CustId] Text;select "
"CustomerId, CompanyName, ContactName "
"from Customers where CustomerId = [CustId]");

//Update the Procedure
m_pCatalog->Procedures->GetItem("CustomerById")->PutCommand(
_variant_t((IDispatch *)m_pCommand));
}

catch(_com_error &e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());

printf("\n\tSource : %s \n\tdescription : %s \n ",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
}

catch(...)
{
cout << "Error occured in include files...."<< endl;
}
}
// EndCommandTextCpp

See Also
Command Property

© 1998-2001 Microsoft Corporation. All rights reserved.
middle 2002-07-15
  • 打赏
  • 举报
回复
对不起,还要问一下。我关键是想知道odbc或者ado关于此类操作的api,最好有个例子。如果没有,把api的用法告诉我也可以。

ado的command对象好像使用作此类功能的,不过微软的文档对于带参数(也就是问号)的sql语句解释的不是很清楚,期待解决。
蒋晟 2002-07-15
  • 打赏
  • 举报
回复
也可以啊
用程序生成查询
middle 2002-07-15
  • 打赏
  • 举报
回复
我不是这个意思。我需要像在JDBC里那样,在客户端写好SQL语句,比如

SELECT * FROM TABLE WHERE NAME= ?

然后让数据库在服务器端存储其语法树,以便随后用实际的参数运行查询。
蒋晟 2002-07-14
  • 打赏
  • 举报
回复
都可以……在数据库里面写好查询先

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧