ADO连接SQL Server2008数据库问题
程序代码:
#include "iostream" using namespace std; #import "C:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF","adoEOF") int main( ) { int nIndex = 0; _RecordsetPtr m_pRecordset; _ConnectionPtr m_pConnection; HRESULT hr = NULL; try { ::CoInitialize( NULL ); m_pConnection.CreateInstance("Adodb.Connection"); m_pRecordset.CreateInstance(_uuidof(Recordset)); hr = m_pConnection->Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=F:\\db\\DataBaseForTemp;Data Source=.","","",adConnectUnspecified);//这里处错误了,提示如下:[DBNETLIB][ConnectionOpen (Connect()).]SQL Server 不存在或拒绝访问。 if ( m_pConnection->State == adStateOpen ) { VARIANT vt; vt.vt = VT_I4; vt.lVal = 0; char Com[] = "select * from StudentInfo"; m_pRecordset = m_pConnection->Execute((LPTSTR)(LPCTSTR)Com,&vt,adCmdText); m_pRecordset->MoveFirst( ); nIndex = m_pRecordset->GetCollect( ( _variant_t )"StuID" ); cout<<nIndex<<endl; } } catch ( _com_error &e ) { cerr<<e.Description( ); } //关闭记录集 if(m_pRecordset!=NULL) m_pRecordset->Close( ); m_pConnection->Close(); ::CoUninitialize( ); return 0; }
如上所述,我还要几个疑问,希望大家帮我解答下:
1.是不是VC连接SQL Server的前提是自己安装了SQL Server
2.我的数据库是VS2012下,选择SQL Server工程建立的,是不是,不是独立SQL Server建立的数据库就不能这样连接
3.我连接的数据库是通过TSQL语句建立的,语句如下:
程序代码:
create database DataBaseForTemp on ( name = 'tmpdb_data', filename = 'c:\db\DataBaseForTemp.mdf', size = 10, maxsize = 20, filegrowth = 2 ) log on( name = 'tmpdb_log', filename = 'c:\db\DataBaseForTemp.ldf', size = 5, maxsize = 10, filegrowth = 1 ) go这样建立的数据库连接时候需要账号和密码吗?
4.我怎么不懂数据库的实例名,服务器名,在哪儿可以获取呢?我安装的VS2012基本都是默认设置,除了安装路径。
谢谢