求问C++怎么连接SQL server啊

求问C++怎么连接SQL server啊,第1张

// 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(iteriter!=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

}

#include<mysql/mysql.h>

#include<stdio.h>

intmain()

{

MYSQL*conn

MYSQL_RES*res

MYSQL_ROWrow

char*server="localhost"//本地连接

char*user="root"//

char*password="525215980"//mysql密码

char*database="student"//数据库名

char*query="select*fromclass"//需要查询的语句

intt,r

conn=mysql_init(NULL)

if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))

{

printf("Errorconnectingtodatabase:%s\n",mysql_error(conn))

}else{

printf("Connected...\n")

}

t=mysql_query(conn,query)

if(t)

{

printf("Errormakingquery:%s\n",mysql_error(conn))

}else{

printf("Querymade...\n")

res=mysql_use_result(conn)

if(res)

{

while((row=mysql_fetch_row(res))!=NULL)

{

//printf("num=%d\n",mysql_num_fields(res))//列数

for(t=0t<mysql_num_fields(res)t++)

printf("%8s",row[t])

printf("\n")

}

}

mysql_free_result(res)

}

mysql_close(conn)

return0

}

扩展资料

C语言使用注意事项:

1、指针是c语言的灵魂,一定要灵活的使用它:

(1)、指针的声明,创建,赋值,销毁等

(2)、指针的类型转换,传参,回调等

2、递归调用也会经常用到:

(1)、递归遍历树结构

(2)、递归搜索


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6759081.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存