急~!高分相送~~~!

arlalei 2003-05-16 07:32:18
我想做 一个 客户端程序, 访问远程服务器上的数据库~
不知道 vc 里是否提供了 访问远程数据库的 类或是函数~~~~???

或者 还有别的方法 实现,
赐教~~~!
...全文
28 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-05-17
  • 打赏
  • 举报
回复
below is from MSDN,you can find a lot of technical article in MSDN if you search"stored procedure and VC"
good luck

Steps To Reproduce Behavior
In the SQL Server 7.0 Query Analyzer select the test database Pubs.


Create the following stored procedure. This stored procedure returns a recordset and an out parameter count.



if exists (select * from sysobjects where id = object_id(N'[dbo].[GetJobs]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop proc GetJobs
go
create proc GetJobs @id as int, @count as int [out] as
begin
Select @count = Count(*) from jobs where job_id >@id
Select * from jobs where job_id >@id
end
go




Use VC App Wizard to create a new console application and modify the code as follows:



#include "stdafx.h"
#include "stdio.h"
#import "C:\PROGRA~1\COMMON~1\System\ado\msado15.dll" no_namespace rename ("EOF", "EOF2")

struct InitOle {
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;

int main(int argc, char* argv[])
{
_variant_t varErr((long)0, VT_ERROR);
_CommandPtr comm(__uuidof(Command));
_ConnectionPtr conn(__uuidof(Connection));

_bstr_t connstr="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=pubs;Data Source=(local)";
conn->Open(connstr, "", "", adConnectUnspecified);
comm->ActiveConnection=conn;
comm->CommandText="GetJobs";
comm->CommandType = adCmdStoredProc ;
comm->Parameters->Refresh();
_variant_t recs;

comm->Parameters->Item[_variant_t((short)1)]->Value= _variant_t((long)5);
_RecordsetPtr rs = comm->Execute(&recs, &vtMissing,adCmdStoredProc);

_variant_t recordcount= comm->Parameters->Item[_variant_t((short)2)]->Value;

printf("recordcount = %li\n", (long)recordcount);
return 0;
}



Change the Datasource, User ID and password in the connection string above.




The recordcount variant that the above code returns is of type VT_NULL rather than the number of records that the stored procedure returns.
doudouniwan 2003-05-17
  • 打赏
  • 举报
回复
使用ADO吧
erigido 2003-05-17
  • 打赏
  • 举报
回复
现在还可以用ADO.NET
happydreamer 2003-05-16
  • 打赏
  • 举报
回复
如果,你是用VC++的话,可以使用ADO。
例子如下:
// ADOSample.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <objbase.h>
#include <oledb.h>
#include <atlbase.h>
#include <windows.h>
#include <comdef.h>

#import "c:\\Program Files\\Common Files\\System\\ADO\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
#import "C:\Program Files\Microsoft SQL Server\80\Tools\Binn\sqldmo.dll"

void dump_com_error(_com_error &e)
{
printf("Error\n");
printf("\a\tCode = %08lx\n", e.Error());
printf("\a\tCode meaning = %s", e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
}


int main(int argc, char* argv[])
{
printf("Hello World!\n");
::CoInitialize(NULL);
try
{

_ConnectionPtr pCon=NULL;
_RecordsetPtr pRs=NULL;

pCon.CreateInstance(__uuidof(Connection));
pCon->Open("Export","dongsong","dongsong",adConnectUnspecified);

pRs.CreateInstance(__uuidof(Recordset));
pRs->CursorLocation=adUseClient;
pRs->Open("select name from sysobjects where type='u'",
_variant_t((IDispatch *) pCon, true),adOpenStatic, adLockReadOnly, adCmdUnknown);
while (!(pRs->EndOfFile))
{

printf("%s\n",(char*)_bstr_t(pRs->Fields->Item["name"]->Value));

pRs->MoveNext();
}
}catch (_com_error &e)
{
dump_com_error(e);
}


::CoUninitialize();


return 0;
}


HawaiiLeo 2003-05-16
  • 打赏
  • 举报
回复
可以使用ADO啊,ADO提供了很多功能,也有很多方法。

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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