11 个解决方案
#2
帮顶~
#3
谢谢你的帮助 但是您提供的那个Topic也不完整 我希望能获得一些具体的代码级别的帮助 谢谢您!
#4
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;
}
#5
win API
#6
用嵌入式SQL(Embedded SQL for C)
可以看看你的SQL 的联机帮助,根据联机帮助的提示一步一步来好了
可以看看你的SQL 的联机帮助,根据联机帮助的提示一步一步来好了
#7
#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);
}
#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);
}
#9
嵌入式的 SQL? 没了解过 学习
#10
非常谢谢你,我最后没有采用ADO的方式。
而是写了一个VC的程序 C语言利用Windows的消息机制与VC程序通信 托管VC来访问数据库,因为要查询的数据量比较小 所以用起来比较方便。
谢谢大家!:)
而是写了一个VC的程序 C语言利用Windows的消息机制与VC程序通信 托管VC来访问数据库,因为要查询的数据量比较小 所以用起来比较方便。
谢谢大家!:)
#11
可以用!
#1
#2
帮顶~
#3
谢谢你的帮助 但是您提供的那个Topic也不完整 我希望能获得一些具体的代码级别的帮助 谢谢您!
#4
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;
}
#5
win API
#6
用嵌入式SQL(Embedded SQL for C)
可以看看你的SQL 的联机帮助,根据联机帮助的提示一步一步来好了
可以看看你的SQL 的联机帮助,根据联机帮助的提示一步一步来好了
#7
#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);
}
#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);
}
#8
#9
嵌入式的 SQL? 没了解过 学习
#10
非常谢谢你,我最后没有采用ADO的方式。
而是写了一个VC的程序 C语言利用Windows的消息机制与VC程序通信 托管VC来访问数据库,因为要查询的数据量比较小 所以用起来比较方便。
谢谢大家!:)
而是写了一个VC的程序 C语言利用Windows的消息机制与VC程序通信 托管VC来访问数据库,因为要查询的数据量比较小 所以用起来比较方便。
谢谢大家!:)
#11
可以用!