Visual C++如何连接SQL Server 2008 数据库?

我想用C++ 连接SQL Server 2008 ,用ADO方式连接,
m_pConnection.CreateInstance("ADODB.Connection");
_bstr_t strConnect="";
m_pConnection->Open(strConnect,"","",adModeUnknown);
这样写对吗?strConnect应该等于什么?

用ADO方式连接的话,可以分四个步骤:

1、添加对ADO的支持;

2、创建一个数据源连接;

3、对数据源中的数据库进行操作;

4、关闭数据源。

代码如下所示:

// TestADOSql.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"  
#include "string"  
#include "vector"  
//步骤1:添加对ADO的支持
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")  
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(NULL); //初始化COM环境           
    _ConnectionPtr pMyConnect(__uuidof(Connection));//定义连接对象并实例化对象 
    _RecordsetPtr pRst(__uuidof(Recordset));//定义记录集对象并实例化对象               
    try           
    {              
        //步骤2:创建数据源连接
        /*打开数据库“SQLServer”,这里需要根据自己PC的数据库的情况 */             
        pMyConnect->Open("Provider=SQLOLEDB; Server=.;Database=AIS2; uid=sa; pwd=;","","",adModeUnknown);           
    } 
    catch (_com_error &e)           
    {               
         cout<<"Initiate failed!"<<endl;               
         cout<<e.Description()<<endl;               
         cout<<e.HelpFile()<<endl;               
         return 0;           
    }           
    cout<<"Connect succeed!"<<endl;                 
     
    //步骤3:对数据源中的数据库/表进行操作
    try           
    {
        pRst = pMyConnect->Execute("select * from gendat",NULL,adCmdText);//执行SQL: select * from gendat          
        if(!pRst->BOF) 
        {
            pRst->MoveFirst(); 
        }               
        else
        {                    
            cout<<"Data is empty!"<<endl;                     
            return 0;                
        }               
        vector<_bstr_t> column_name;      
         
        /*存储表的所有列名,显示表的列名*/               
        for(int i=0; i< pRst->Fields->GetCount();i++)               
        {                    
            cout<<pRst->Fields->GetItem(_variant_t((long)i))->Name<<" ";                    
            column_name.push_back(pRst->Fields->GetItem(_variant_t((long)i))->Name);               
        }   
        cout<<endl;
         
        /*对表进行遍历访问,显示表中每一行的内容*/               
        while(!pRst->adoEOF)               
        {                    
            vector<_bstr_t>::iterator iter=column_name.begin();                    
            for(iter;iter!=column_name.end();iter++)                    
            {                         
                if(pRst->GetCollect(*iter).vt !=VT_NULL)                         
                {  
                    cout<<(_bstr_t)pRst->GetCollect(*iter)<<" ";                         
                }                         
                else
                {
                    cout<<"NULL"<<endl;  
                }                  
            }
            pRst->MoveNext();                   
            cout<<endl;              
        }           
    }
    catch(_com_error &e)           
    {               
        cout<<e.Description()<<endl;               
        cout<<e.HelpFile()<<endl;               
        return 0;          
    }  

    //步骤4:关闭数据源
    /*关闭数据库并释放指针*/        
    try           
    {               
        pRst->Close();     //关闭记录集               
        pMyConnect->Close();//关闭数据库               
        pRst.Release();//释放记录集对象指针               
        pMyConnect.Release();//释放连接对象指针
    }
    catch(_com_error &e)           
    {               
        cout<<e.Description()<<endl;               
        cout<<e.HelpFile()<<endl;               
        return 0;           
    }                  
    CoUninitialize(); //释放COM环境
    return 0;
}

温馨提示:内容为网友见解,仅供参考
第1个回答  2011-09-27
有两种方法.
一种是:
_ConnectionPtr pMyConnect=NULL;
pMyConnect.CreateInstance(__uuidof(Connection)));
_bstr_t strConnect="Provider=SQLOLEDB; Server=server_name;Database=database_name; uid=user_name; pwd=password;";
pMyConnect->Open(strConnect,"","",NULL);

也可以用:
string ConnectionString = "Provider=SQLOLEDB;server=(local);database=databaseName";
m_pConnection->ConnectionString = ConnectionString.c_str();
m_pConnection->Open("", user, password, adConnectUnspecified);本回答被提问者采纳
第2个回答  2015-11-13

    选择连接方式,如ODBC数据源

    选择打开方式,如CData数据集等

    存储数据、读取数据

第3个回答  2011-09-23
关注,很少连数据库
第4个回答  2011-09-23
直接用数据源
相似回答