可能重複:
How to execute sql statements from a C program?如何從C/C++應用程序中創建SQL查詢?
我創建一個C程序,我需要從我的SQL服務器的一些數據。有誰知道我如何從Windows下的C應用程序進行SQL查詢?
可能重複:
How to execute sql statements from a C program?如何從C/C++應用程序中創建SQL查詢?
我創建一個C程序,我需要從我的SQL服務器的一些數據。有誰知道我如何從Windows下的C應用程序進行SQL查詢?
隨着C,你可能會想使用Microsoft ODBC API:http://msdn.microsoft.com/en-us/library/ms714562(v=VS.85).aspx
我在CSQLBinding類,其中包含大量的使用示例的大量使用這一點。除了封裝類(顯然只有C++),所有的功能都與普通的C完全兼容。
這裏是一個代碼示例,以啓動你...對不起,我不能給你一個C例子。
#include <ATLComTime.h>
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" rename("EOF", "EndOfFile")
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
int main()
{
std::string connectStr = "Provider=sqloledb;Data Source=MyDSN;Database=MyDB;User Id=DBUser;Password=Dbpwd;";
try
{
ADODB::_ConnectionPtr pConnection;
TESTHR(pConnection.CreateInstance(__uuidof(ADODB::Connection)));
ADODB::_ConnectionPtr pConnection->Open (connectStr.c_str(),"","", NULL);
VARIANT * RecordsAffected NULL;
long Options = ADODB::adExecuteNoRecords;
ADODB::_RecordsetPtr pRec = pConnection->Execute("select * from my_table",RecordsAffected,Options);
// Rest you should be able to figure out.
}
catch(_com_error &e)
{
// print error message
}
}
非常感謝,這也幫助了很多 – kampi
什麼是數據庫服務器? –
我不知道,但我認爲它是SQL Server 2008 – kampi