使用ADO _ConnectionPtr

 

// GetUser.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <atlstr.h>
#include <iostream>
using namespace std;

#import "C:\Program Files\Common Files\SYSTEM\ADO\msado15.dll" no_namespace rename("EOF","adoEOF") 

DWORD WINAPI GetUserInfo();

int main()
{
	::CoInitialize(NULL);
	_ConnectionPtr pConn = NULL; 

	//sode是数据库服务器192.168.3.9上的一个数据库 
	CString strConn = "Provider=SQLOLEDB.1;Data Source=192.168.3.9,1433;Initial Catalog=sode";   
	if (S_OK != pConn.CreateInstance("ADODB.Connection"))
	{
		cout << "create instance failed..." << endl;
		return -1;
	}

	try 
	{ 
		HRESULT hr = pConn->Open((_bstr_t)strConn, "sa", "sa",adConnectUnspecified);         
		if (FAILED(hr)) 
		{ 
			cout << "connect failed!" << endl;
			return   FALSE; 
		} 
	} 
	catch(_com_error e) 
	{ 
		cout << "connect exception..." << endl;
		return FALSE; 
	}

	cout << "connect succ!" << endl;

	return 0;
}

DWORD WINAPI GetUserInfo()
{
	return 0;
}

你可能感兴趣的:(C++,c,windows,C#)