C语言怎么通过ADO连接SQL server数据库

wj_1314angel 2009-07-03 10:00:26
大家好。我把GoAhead web服务器移植到了WinCE5.0系统里面,但是我希望GoAhead能操作我的SQL SERVER CE2.0数据库。Goahead服务器是C语言开发的,所以我想知道在C语言中怎么用ADO来连接和操作SQL server CE2.0数据库,急,谢谢!
...全文
722 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jixingzhong 的回复:]
#define DBNTWIN32
#include <stdio.h>
#include <windows.h>
#include <sqlfront.h>
#include <sqldb.h>

// Forward declarations of the error handler and message handler.
int err_handler(PDBPROCESS, INT, INT, INT, LPCSTR, LPCSTR);
int msg_handler(PDBPROCESS, DBINT, INT, INT, LPCSTR, LPCSTR,
LPCSTR…
[/Quote]
可以用!
wj_1314angel 2009-07-17
  • 打赏
  • 举报
回复
非常谢谢你,我最后没有采用ADO的方式。
而是写了一个VC的程序 C语言利用Windows的消息机制与VC程序通信 托管VC来访问数据库,因为要查询的数据量比较小 所以用起来比较方便。

谢谢大家!:)
  • 打赏
  • 举报
回复
jixingzhong 2009-07-04
  • 打赏
  • 举报
回复
#define DBNTWIN32
#include <stdio.h>
#include <windows.h>
#include <sqlfront.h>
#include <sqldb.h>

// Forward declarations of the error handler and message handler.
int err_handler(PDBPROCESS, INT, INT, INT, LPCSTR, LPCSTR);
int msg_handler(PDBPROCESS, DBINT, INT, INT, LPCSTR, LPCSTR,
LPCSTR, DBUSMALLINT);

main()
{
PDBPROCESS dbproc; // The connection with SQL Server.
PLOGINREC login; // The login information.
DBCHAR name[100];
DBCHAR city[100];

// Install user-supplied error- and message-handling functions.
dberrhandle (err_handler);
dbmsghandle (msg_handler);

// Initialize DB-Library.
dbinit ();

// Get a LOGINREC.
login = dblogin ();
DBSETLUSER (login, "my_login");
DBSETLPWD (login, "my_password");
DBSETLAPP (login, "example");

// Get a DBPROCESS structure for communication with SQL Server.
dbproc = dbopen (login, "my_server");

// Retrieve some columns from the authors table in the
// pubs database.

// First, put the command into the command buffer.
dbcmd (dbproc, "SELECT au_lname, city FROM pubs..authors");
dbcmd (dbproc, " WHERE state = 'CA' ");

// Send the command to SQL Server and start execution.
dbsqlexec (dbproc);

// Process the results.
if (dbresults (dbproc) == SUCCEED)
{
// Bind column to program variables.
dbbind (dbproc, 1, NTBSTRINGBIND, 0, name);
dbbind (dbproc, 2, NTBSTRINGBIND, 0, city);

// Retrieve and print the result rows.
while (dbnextrow (dbproc) != NO_MORE_ROWS)
{
printf ("%s from %s\n", name, city);
}
}

// Close the connection to SQL Server.
dbexit ();

return (0);
}

int err_handler (PDBPROCESS dbproc, INT severity,
INT dberr, INT oserr, LPCSTR dberrstr, LPCSTR oserrstr)
{
printf ("DB-Library Error %i: %s\n", dberr, dberrstr);
if (oserr != DBNOERR)
{
printf ("Operating System Error %i: %s\n", oserr, oserrstr);
}
return (INT_CANCEL);
}

int msg_handler (PDBPROCESS dbproc, DBINT msgno, INT msgstate,
INT severity, LPCSTR msgtext, LPCSTR server,
LPCSTR procedure, DBUSMALLINT line)
{
printf ("SQL Server Message %ld: %s\n", msgno, msgtext);
return (0);
}
jixingzhong 2009-07-04
  • 打赏
  • 举报
回复
用嵌入式SQL(Embedded SQL for C)

可以看看你的SQL 的联机帮助,根据联机帮助的提示一步一步来好了
jixingzhong 2009-07-04
  • 打赏
  • 举报
回复
win API
Fleeboy 2009-07-04
  • 打赏
  • 举报
回复
c++ 调用ADO访问SQL 2000代码:

// adotest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#import "c:\Windows\System32\msado15.dll" no_namespace rename("EOF","adoEOF")

int _tmain(int argc, _TCHAR* argv[])
{

::CoInitialize(NULL);
_ConnectionPtr m_pConnection;
HRESULT hr=m_pConnection.CreateInstance(__uuidof(Connection));
if(!SUCCEEDED(hr))
{
std::cout<<"m_pConnection.CreateInstance(__uuidof(Connection)) error"<<std::endl;
Sleep(-1);
return 0;
}
std::string str = "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Sell;Data Source=WUBIYU-PC";
m_pConnection->ConnectionString ="Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Sell;Data Source=WUBIYU-PC" ;

try
{
m_pConnection->Open((_bstr_t)str.c_str(),"","",NULL);
}
catch(_com_error &e)
{
std::cout<<"打开失败:"<<e.Description()<<std::endl;
Sleep(-1);
return 0;
}

_RecordsetPtr recordsetP;


if(FAILED(recordsetP.CreateInstance(__uuidof( Recordset ))))
{
std::cout<<"新建Recordset对象失败:"<<std::endl;
Sleep(-1);
return 0;
}
try
{
//recordsetP->Open(_variant_t("Sell_Agent"),
// _variant_t((IDispatch *)m_pConnection,true), CursorTypeEnum::adOpenKeyset,LockTypeEnum::adLockOptimistic, CommandTypeEnum::adCmdTable);
//recordsetP->Open(_variant_t("Sell_Agent"),
// _variant_t((IDispatch *)m_pConnection,true), ::adOpenKeyset,::adLockOptimistic, ::adCmdTable);

}
catch (_com_error &e)
{
std::cout<<"打开失败"<<e.Description()<<std::endl;
}

//std::string cmdStr = "UPDATE Sell_Agent SET Integral = 21 WHERE id = 2";
std::string cmdStr = "select * from Sell_Agent WHERE id = 2";
_CommandPtr pCmdChange;
hr = pCmdChange.CreateInstance(__uuidof(Command));
if(!SUCCEEDED(hr))
{
std::cout<<"create Command object error"<<std::endl;
Sleep(-1);
return 0;
}
pCmdChange->ActiveConnection = m_pConnection;
pCmdChange->CommandText = (_bstr_t)cmdStr.c_str();
m_pConnection->Errors->Clear();
_variant_t recordsCount=0;
recordsetP = pCmdChange->Execute(&recordsCount, NULL, adCmdText);
std::cout<< (long)recordsCount<<std::endl;

try
{
std::cout<<recordsetP->GetFields()->Count<<std::endl;
recordsetP->MoveFirst();
while(!recordsetP->adoEOF)
{
int id_value =(int)recordsetP->Fields->Item[_variant_t("Id")]->Value;
std::string id = (_bstr_t)recordsetP->GetFields()->GetItem((long)0)->Name;
std::string name_value =(_bstr_t)recordsetP->GetFields()->GetItem((long)1)->Value;
std::string name = (_bstr_t)recordsetP->GetFields()->GetItem((long)1)->Name;
std::cout<<id.c_str()<<" :"<<id_value<<" "<<name.c_str()<<" :"<<name_value.c_str()<<endl;
recordsetP->MoveNext();
}
}
catch(_com_error &el)
{
std::cout<<el.Description()<<std::endl;
}
std::cout<<"完成"<<std::endl;
::CoUninitialize();
Sleep(-1);
return 0;
}
herman~~ 2009-07-04
  • 打赏
  • 举报
回复
嵌入式的 SQL? 没了解过 学习
wj_1314angel 2009-07-03
  • 打赏
  • 举报
回复
谢谢你的帮助 但是您提供的那个Topic也不完整 我希望能获得一些具体的代码级别的帮助 谢谢您!
liliangbao 2009-07-03
  • 打赏
  • 举报
回复
帮顶~
lingyin55 2009-07-03
  • 打赏
  • 举报
回复

69,397

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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