windows 如何从 C/C++ 应用程序进行 SQL 查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6614684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make an SQL query from a C/C++ application?
提问by kampi
Possible Duplicate:
How to execute sql statements from a C program?
可能的重复:
如何从 C 程序执行 sql 语句?
I'm creating a C application, and i need some data from my SQL server. Does anyone know how can I make an SQL query from my C application under Windows?
我正在创建一个 C 应用程序,我需要一些来自我的 SQL 服务器的数据。有谁知道如何在 Windows 下从我的 C 应用程序进行 SQL 查询?
回答by Sharath
Here is a code sample to start you off... Sorry I can't provide you a C example.
这是一个代码示例,可以让您开始……抱歉,我无法为您提供 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
}
}
回答by NTDLS
With C, you'll probably want to use the Microsoft ODBC API: http://msdn.microsoft.com/en-us/library/ms714562(v=VS.85).aspx
使用 C,您可能想要使用 Microsoft ODBC API:http: //msdn.microsoft.com/en-us/library/ms714562(v=VS.85).aspx
I use this heavily in my CSQLBinding class, which contains lots of usage samples. Besides the encapsulating class, (which is obviously C++ only) all of the functionality is completely compatible with plain C.
我在包含大量使用示例的CSQLBinding类中大量使用它。除了封装类(显然只有 C++)所有功能都与普通 C 完全兼容。