• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            公子周

                                     --不亂于心,不困于情
            隨筆 - 3, 文章 - 0, 評論 - 5, 引用 - 0
            數據加載中……

            ODBC連接sql數據庫,支持創建數據庫,創建表,查詢,更新,刪除

            此代碼,在網上有多份,但是很多不知道,創建數據庫,創建表,查詢,更新,刪除
            class ODBC
            {
            private:  
            SQLHANDLE hEnv;  
            SQLHANDLE hDbc;  
            SQLHANDLE hStmt;  
            SQLRETURN retCode;  
            SQLINTEGER retErro;  
            SQLINTEGER rowCount;  
            SQLSMALLINT colCount;  
            vector<string> DBinfo;
            bool bState;  
            string ws2s(const wstring& ws);
            wstring s2ws(const string& s);
            bool Close();  
            bool IsOpen();  
            public:
            ODBC(void);
            ~ODBC(void);
            public:
            bool Connect();  
               
                int GetRowCount(){return rowCount;}  
                int GetColCount(){return colCount;}  
            void replace(string &str,const char *string_to_replace,const char *new_string);
            void replace(string &str,const char *string_to_replace,int num);
                vector<string*> ExecuteQueryVector(const char* pszSql);  
                int ExecuteQuery(const char* pszSql);  //執行查詢  
                int ExecuteNonQuery(const char* pszSql);//執行非查詢(更新或刪除)  
            int createdatabase(string sql);//創建數據庫,0為成功,-1為失敗
            int createtable(string sql);//創建數據表。0為成功,-1為失敗,特別注意,語句不要帶go字眼
            };

            ODBC::ODBC(void)
            {
            bState=false;  
            rowCount=colCount=0;  
            retCode=SQLAllocHandle(SQL_HANDLE_ENV,NULL,&hEnv);  
            if((retCode!=SQL_SUCCESS)&& (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            cout<<"Erro AllocHandle"<<retCode<<endl;  
            return;  
            }  
            retCode=SQLSetEnvAttr(hEnv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3,SQL_IS_INTEGER);  
            if((retCode!=SQL_SUCCESS)&& (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            cout<<"Erro AllocHandle"<<retCode<<endl;  
            SQLFreeHandle( SQL_HANDLE_DBC, hEnv );  
            return;  
            }  
            retCode=SQLAllocHandle(SQL_HANDLE_DBC,hEnv,&hDbc);  
            if((retCode!=SQL_SUCCESS)&& (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            cout<<"Erro AllocHandle"<<retCode<<endl;  
            SQLFreeHandle( SQL_HANDLE_DBC, hEnv );  
            return;  
            }  
            }
            ODBC::~ODBC(void)
            {
            Close(); 
            }
            bool ODBC::Close()  
            {  
            if(bState)  
            {  
            SQLDisconnect(hDbc);  
            SQLFreeHandle(SQL_HANDLE_DBC,hDbc);  
            SQLFreeHandle(SQL_HANDLE_ENV,hEnv);  
            bState=false;  
            }  
            return true;  
            }  
            bool ODBC::Connect()  
            {  
            string ip,user,password;
            ip="127.0.0.1";
            user="sa";
            password="Nicholas@123";
            if(bState==false)  
            {  
            // retCode=SQLConnect(hDbc,(SQLCHAR*) pszDSN,SQL_NTS,(SQLCHAR*) pszUName,SQL_NTS,(SQLCHAR*) pszUPassword,SQL_NTS);
            string strConnect="Driver={SQL Server};Database=test;Server=";
            strConnect+=ip;
            strConnect+=";uid=";
            strConnect+=user;
            strConnect+=";pwd=";
            strConnect+=password;
            short sDriverOutputLength;
            wchar_t szDriverOutput[256];
            retCode=SQLDriverConnect(hDbc,NULL,(SQLWCHAR *)s2ws(strConnect).c_str(),SQL_NTS, (SQLWCHAR  *)szDriverOutput,256,&sDriverOutputLength,SQL_DRIVER_NOPROMPT);
            if((retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            cout<<"Erro Connect "<<retCode<<endl;  
            SQLFreeHandle( SQL_HANDLE_DBC, hDbc );  
            return false;  
            }  
            retCode=SQLAllocHandle(SQL_HANDLE_STMT,hDbc,&hStmt);  
            if((retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            cout<<"Erro Connect "<<retCode<<endl;  
            SQLDisconnect( hDbc );  
            SQLFreeHandle( SQL_HANDLE_DBC, hDbc);  
            return false;  
            }  
            }       
            bState=true;  
            cout<<"success!"<<endl;  
            return true;  
            }  
            string ODBC::ws2s(const wstring& ws)
            {
            _bstr_t t = ws.c_str();
            char* pchar = (char*)t;
            string result = pchar;
            return result;
            }
            wstring ODBC::s2ws(const string& s)
            {
            _bstr_t t = s.c_str();
            wchar_t* pwchar = (wchar_t*)t;
            wstring result = pwchar;
            return result;
            }
            int ODBC::ExecuteQuery(const char* pszSql)  
            {  
            string strr=pszSql;
            if(pszSql==NULL)  
            return 0;  
            cout<<"hStmt="<<hStmt<<endl;  
            retCode=SQLExecDirect(hStmt,(SQLWCHAR *)s2ws(strr).c_str(),SQL_NTS);  
            if((retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            cout<<"Erro ExecDirect "<<retCode<<endl;  
            return -1;  
            }  
            /*  retCode=SQLRowCount(hStmt,&rowCount);  //不受select 影響。。 
            if((retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO)) 
            cout<<"Erro RowCount "<<retCode<<endl; 
            return -1; 
            }*/  
            retCode=SQLNumResultCols(hStmt,&colCount);  
            if((retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            cout<<"Erro ColCount "<<retCode<<endl;  
            return -1;  
            }  
            rowCount=0;  
            while(SQL_NO_DATA!=SQLFetch(hStmt))  
            {  
            //cout<<pszBuf<<endl;  
            rowCount++;  
            }  
            SQLCancel(hStmt);  
            return rowCount;  
            }  
            int ODBC::ExecuteNonQuery(const char* pszSql)  
            {  
            rowCount=0;  
            if(pszSql==NULL)  
            return 0;  
            //cout<<"hStmt="<<hStmt<<endl;  
            retCode=SQLExecDirect(hStmt,(SQLWCHAR *)s2ws(pszSql).c_str(),SQL_NTS);  
            if((retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            // cout<<"Erro ExecDirect "<<retCode<<endl;  
            return -1;  
            }  
            retCode=SQLRowCount(hStmt,&rowCount);   
            if((retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            // cout<<"Erro RowCount "<<retCode<<endl;  
            return -1;  
            }  
            retCode=SQLNumResultCols(hStmt,&colCount);  
            if((retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            // cout<<"Erro ColCount "<<retCode<<endl;  
            return -1;  
            }  
            SQLCancel(hStmt);  
            return rowCount;  
            }  
            vector<string* >  ODBC::ExecuteQueryVector(const char* pszSql)  
            {  
            string strr=pszSql;
            vector<string* > v;  
            if(pszSql==NULL)  
            return v;  
            retCode=SQLExecDirect(hStmt,(SQLWCHAR *)s2ws(strr).c_str(),SQL_NTS);  
            if((retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            cout<<"Erro ExecDirect "<<retCode<<endl;  
            return v;  
            }  
            retCode=SQLNumResultCols(hStmt,&colCount);  
            if((retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            cout<<"Erro ColCount "<<retCode<<endl;  
            return v;  
            }  
            rowCount=0;  
            SQLINTEGER colLen = 0;  
            SQLSMALLINT buf_len = 0;  
            SQLINTEGER colType = 0;  
            while(true)  
            {    
            char sz_buf[256];  
            char* pszBuf;  
            SQLINTEGER  buflen;  
            string* rowData=new string[colCount+1];    
            if(SQLFetch(hStmt)==SQL_NO_DATA)  
            {  
            break;  
            }  
            for(int i=1;i<=colCount;i++)  
            {  
            SQLColAttribute(hStmt, i, SQL_DESC_NAME, sz_buf, 256, &buf_len, 0);  
            SQLColAttribute(hStmt, i, SQL_DESC_TYPE, 0, 0, 0, &colType);  
            SQLColAttribute(hStmt, i, SQL_DESC_LENGTH, NULL, 0, 0, &colLen);  
            pszBuf=new char[colLen+1];  
            pszBuf[0]='/0';  
            SQLGetData(hStmt,i,SQL_C_CHAR,pszBuf,colLen,&buflen);  
            rowData[i-1]=pszBuf;  
            }  
            v.push_back(rowData);     
            rowCount++;  
            }  
            SQLCancel(hStmt);  
            return v;  
            }  
            bool ODBC::IsOpen()  
            {  
            return bState;  
            }  
            void ODBC::replace(string &str,const char *string_to_replace,const char *new_string)
            {
                //   Find   the   first   string   to   replace
                int   index   =   str.find(string_to_replace);
                //   while   there   is   one
                while(index   !=   std::string::npos)
                {
                    //   Replace   it
                    str.replace(index,   strlen(string_to_replace),   new_string);
                    //   Find   the   next   one
                    index   =   str.find(string_to_replace,   index   +   strlen(new_string));
                }
            void ODBC::replace(string &str,const char *string_to_replace,int num)
            {
            stringstream strStream;
            strStream<<num;
            string new_string=strStream.str();
            int   index   =   str.find(string_to_replace);
            while(index   !=   std::string::npos)
            {
            str.replace(index,   strlen(string_to_replace),   new_string);
            index   =   str.find(string_to_replace,   index   +  new_string.length());
            }
            }
            //創建數據庫
            int ODBC::createdatabase(string sql)
            {
            string strr=sql;
            if(sql=="")  
            return -1;  
            retCode=SQLExecDirect(hStmt,(SQLWCHAR *)s2ws(strr).c_str(),SQL_NTS);  
            if((retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            return -1;
            }  
            return 0;
            }
            //創建數據表。0為成功,-1為失敗
            int ODBC::createtable(string sql)
            {
            string strr=sql;
            if(sql=="")  
            return -1;  
            retCode=SQLExecDirect(hStmt,(SQLWCHAR *)s2ws(strr).c_str(),SQL_NTS);  
            if((retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO))  
            {  
            return -1;
            }  
            return 0;
            }


            ODBC odbc;
            odbc.connect();
            //在實際使用的時候,

            string sql="insert into [test].[dbo].[Exception] values ('0%','1%',2%,3%,'4%','5%','6%',7%);";
            odbc.replace(sql,"0%",iter->first.c_str());//站點名稱
            odbc.replace(sql,"1%",iter_url_ip->first.c_str());//IP地址
            odbc.replace(sql,"2%",iter_url_ip->second.ipcount);//IP訪問次數
            odbc.replace(sql,"3%",1);//攻擊類型
            odbc.replace(sql,"4%",_Map.getTime().c_str());//時間
            odbc.replace(sql,"5%",sessiontop10.c_str());//session top 10
            odbc.replace(sql,"6%",urltop10.c_str());//url top 10
            odbc.replace(sql,"7%",0);//DDOS判定原因
            odbc.ExecuteNonQuery(sql.c_str());

            posted on 2012-07-10 22:13 公子周 閱讀(1587) 評論(0)  編輯 收藏 引用 所屬分類: 數據庫類

            久久99国产精品久久久| 久久人妻少妇嫩草AV无码专区| 国产91久久精品一区二区| 一级做a爰片久久毛片人呢| 久久精品国产亚洲AV香蕉| 久久久中文字幕| 久久综合88熟人妻| 伊人久久一区二区三区无码| 丰满少妇高潮惨叫久久久| 欧美久久久久久| 久久久精品国产Sm最大网站| 精品久久8x国产免费观看| 国产成人精品久久亚洲高清不卡 | 亚洲午夜久久久久久久久久| 久久久久久国产a免费观看不卡| 久久综合久久综合亚洲| 久久久久免费精品国产| 日韩AV无码久久一区二区 | 色综合久久最新中文字幕| 青青青青久久精品国产h久久精品五福影院1421 | 国内精品久久久久影院网站| 久久66热人妻偷产精品9| 无码人妻少妇久久中文字幕| 久久精品国产影库免费看| 久久国产亚洲精品无码| 久久久久久免费视频| 色99久久久久高潮综合影院| 99久久无码一区人妻| 欧美精品一区二区精品久久| 日日躁夜夜躁狠狠久久AV| 狠狠色丁香久久婷婷综合| 99久久免费国产精品特黄| 蜜臀av性久久久久蜜臀aⅴ麻豆 | 伊人久久精品线影院| 久久国产精品成人免费| 99久久免费国产精品热| 国产成人久久精品激情| 精品久久久久久亚洲| 日韩亚洲欧美久久久www综合网 | 久久国产精品免费| 久久se精品一区二区影院 |