以前,一直使用odbc,olded和ado连接sqlserver,但自从微软发布轻量级sqlserver localdb数据库后,以前的连接字符串不太好用了。
后来查看sqlserver的服务组件,意外发现了sqlserver native client数据库驱动接口,对应SQLNCLI COM组件(可在注册表中查到)。
于是尝试在OLDED数据库连接串中使用该组件,竟然真的有效。直接上代码:
// SQLServer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
//引入atl的命令行接口,该文件已经包含了oledb.h
#include <atldbcli.h>
//引入atl的转换工具
//#include <atlconv.h>
using namespace std;
typedef struct result
{
int fileid;
int groupid;
DBSTATUS dwFileidStatus;
DBSTATUS dwGroupidStatus;
} RESULT;
int connByOLEDB()
{
HRESULT hr;
::CoInitialize(NULL);
CDataSource instDataSource;
//OLEDB连接字符串格式:
//Provider=SQLOLEDB.1;Data Source=<host>\\<instance>;Initial Catalog=dbName;
//Provider=SQLOLEDB.1;Data Source=<host>,<port>;Initial Catalog=dbName;
//Provider=SQLOLEDB.1;Data Source=pipename;Initial Catalog=dbName;
//wchar_t szConnectionString[512] = L"Provider=SQLOLEDB.1;Data Source=np:\\\\.\\pipe\\LOCALDB#840AFFF4\\tsql\\query;Initial Catalog=master;User ID=sa;Password=123456;Persist Security Info=True;";
//【【【【通过COM组件SQLNCLI,连接并操作localdb】】】】
//Provider=SQLNCLI11.1;DataSoucr=(localdb)\\<instance>;Initial Catalog=dbName;
wchar_t szConnectionString[512] = L"Provider=SQLNCLI11.1;Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;User ID=sa;Password=123456;Persist Security Info=True;";
hr = instDataSource.OpenFromInitializationString(szConnectionString);
if (FAILED(hr))
{
printf("连接失败\n");
return 0;
}
//打开会话
CSession instSession;
hr = instSession.Open(instDataSource);
if (FAILED(hr))
return 0;
//创建命令对象
CCommand<CManualAccessor, CRowset, CNoMultipleResults> instCommand;
hr = instCommand.CreateCommand(instSession);
if (FAILED(hr))
return 0;
//查询命令对象的文本接口
//在vs2010中,这里是instCommandText
//在vs2015中,这里是ICommandText
CComPtr<ICommandText> pCommandText;
hr = instCommand.m_spCommand->QueryInterface(&pCommandText);
USES_CONVERSION;
//查询语句
const wchar_t *cmd = L"select fileid,groupid from sysfiles";
hr = pCommandText->SetCommandText(DBGUID_SQL, cmd);
if (FAILED(hr))
return 0;
CDBPropSet instPropSet(DBPROPSET_ROWSET);
instPropSet.AddProperty(DBPROP_ISequentialStream, true);
hr = instCommand.Open(&instPropSet, NULL);
if (FAILED(hr))
return 0;
//获取列信息
DBORDINAL ulColumns = 0;
DBCOLUMNINFO *pColumnInfo;
LPOLESTR pStrings;
hr = instCommand.GetColumnInfo(&ulColumns
, &pColumnInfo
, &pStrings);
if (FAILED(hr))
return 0;
//初始化列绑定
RESULT rst;
BYTE * pColumnInfoBuff;
int bufferSize = sizeof(rst.fileid) + sizeof(rst.groupid);
pColumnInfoBuff = new BYTE[bufferSize];
hr = instCommand.CreateAccessor(ulColumns, pColumnInfoBuff, (DBLENGTH)bufferSize);
if (FAILED(hr))
return 0;
//开始列绑定
DBLENGTH dbLength0 = pColumnInfo[0].ulColumnSize;
DBLENGTH dbLength1 = pColumnInfo[1].ulColumnSize;
instCommand.AddBindEntry(1, DBTYPE_I4, dbLength0, &rst.fileid, NULL, &rst.dwFileidStatus);
instCommand.AddBindEntry(2, DBTYPE_I4, dbLength1, &rst.groupid, NULL, &rst.dwGroupidStatus);
hr = instCommand.Bind();
if(FAILED(hr))
return 0;
//获取查询信息
while(S_OK == instCommand.MoveNext())
{
printf("fileid = [%d] ,groupid = [%d]\n", rst.fileid, rst.groupid);
}
//关闭连接
instCommand.Close();
instSession.Close();
instDataSource.Close();
::CoUninitialize();
system("pause");
return 0;
}