{"id":62,"date":"2013-12-14T14:41:14","date_gmt":"2013-12-14T14:41:14","guid":{"rendered":""},"modified":"2013-12-14T14:41:14","modified_gmt":"2013-12-14T14:41:14","slug":"","status":"publish","type":"post","link":"http:\/\/weizn.net\/?p=62","title":{"rendered":"MSSQL_Connector"},"content":{"rendered":"<pre class=\"brush:cpp; toolbar: true; auto-links: true;\">#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;windows.h&gt;\r\n#include &lt;conio.h&gt;\r\n#include &lt;sql.h&gt;\r\n#include &lt;sqlext.h&gt;\r\n#include &lt;sqltypes.h&gt;\r\n#include &lt;odbcss.h&gt;\r\n\r\n#define MAXBUFLEN 255\r\n\r\nSQLHENV henv = SQL_NULL_HENV;\r\nSQLHDBC hdbc = SQL_NULL_HDBC;\r\nSQLHSTMT hstmt = SQL_NULL_HSTMT;\r\n\r\nint Connect_SQLServer()\r\n{\r\n    RETCODE retcode;\r\n \r\n    SQLCHAR ConnStrIn[MAXBUFLEN]=\"DRIVER={SQL Server};SERVER=127.0.0.1;UID=wayne;PWD=305859616;DATABASE=master;\";\r\n    \/\/1.\u8fde\u63a5\u6570\u636e\u6e90\r\n    \/\/1.\u73af\u5883\u53e5\u67c4\r\n    retcode=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE ,&amp;henv);\r\n    retcode=SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,SQL_IS_INTEGER);\r\n    \/\/2.\u8fde\u63a5\u53e5\u67c4\r\n    retcode=SQLAllocHandle(SQL_HANDLE_DBC,henv,&amp;hdbc);\r\n    retcode=SQLDriverConnect(hdbc,NULL,ConnStrIn,SQL_NTS,NULL,NULL,NULL,SQL_DRIVER_NOPROMPT);\r\n    \/\/\u5224\u65ad\u8fde\u63a5\u662f\u5426\u6210\u529f\r\n    if((retcode!=SQL_SUCCESS)&amp;&amp;(retcode!=SQL_SUCCESS_WITH_INFO))\r\n        return 0;\r\n return 1;\r\n}\r\n\r\nint ChangeDB(char *DBN)\r\n{\r\n RETCODE retcode;\r\n SQLHSTMT  hstmt;\r\n char SQL_CMD[100];\r\n \r\n memset(SQL_CMD,NULL,sizeof(SQL_CMD));\r\n    sprintf(SQL_CMD,\"use %s;\",DBN);\r\n retcode= SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &amp;hstmt);\r\n if(retcode!=SQL_SUCCESS &amp;&amp; retcode!=SQL_SUCCESS_WITH_INFO)\r\n  return 0;\r\n SQLFreeStmt(hstmt,SQL_CLOSE);\r\n retcode=SQLExecDirect(hstmt,(unsigned char *)SQL_CMD,SQL_NTS);\r\n if(retcode!=SQL_SUCCESS &amp;&amp; retcode!=SQL_SUCCESS_WITH_INFO)\r\n  return 0;\r\n \r\n return 1;\r\n}\r\n\r\nint QueryDB(SQLHSTMT *hstmt,char *SQL_CMD)\r\n{\r\n RETCODE retcode;\r\n \r\n\r\n retcode= SQLAllocHandle(SQL_HANDLE_STMT, hdbc, hstmt);\r\n if(retcode!=SQL_SUCCESS &amp;&amp; retcode!=SQL_SUCCESS_WITH_INFO)\r\n  return 0;\r\n SQLFreeStmt(*hstmt,SQL_CLOSE);\r\n retcode=SQLExecDirect(*hstmt,(unsigned char *)SQL_CMD,SQL_NTS);\r\n if(retcode!=SQL_SUCCESS &amp;&amp; retcode!=SQL_SUCCESS_WITH_INFO)\r\n  return 0;\r\n \r\n return 1;\r\n}\r\n\r\nint show_results(SQLHSTMT hstmt)\r\n{\r\n RETCODE retcode;\r\n SQLINTEGER RowCount;\r\n char data[100];\r\n    SQLCHAR   buff[50];\r\n    SQLINTEGER  len;\r\n int flag=0;\r\n\r\n    retcode=SQLRowCount(hstmt,&amp;RowCount);\r\n if(retcode!=SQL_SUCCESS &amp;&amp; retcode!=SQL_SUCCESS_WITH_INFO)\r\n  return 0;\r\n\r\n while(1)\r\n {\r\n  memset(data, 0, sizeof(data));\r\n  retcode = SQLFetch(hstmt);\r\n  if (retcode==SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)\r\n  {\r\n   memset(buff,NULL,sizeof(buff));\r\n   SQLGetData(hstmt,(UWORD)2,SQL_C_CHAR,buff,sizeof(buff)-1, &amp;len);  \/\/\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u5217\r\n\r\n\/\/    if(!strncmp((char *)buff,\"3\",1) || !strncmp((char *)buff,\"2\",1) \\\r\n\/\/     || !strncmp((char *)buff,\"1\",1))\r\n\/\/     flag=1;\r\n\/\/    else\r\n\/\/     flag=0;\r\n   strcat(data,(char *)buff);\r\n\r\n   strcat(data, \"\\t\");\r\n   memset(buff,NULL,sizeof(buff));\r\n   SQLGetData(hstmt,(UWORD)3,SQL_C_CHAR,buff,sizeof(buff)-1,&amp;len);\r\n   strcat(data,(char *)buff);\r\n\r\n   strcat(data, \"\\t\");\r\n   memset(buff,NULL,sizeof(buff));\r\n   SQLGetData(hstmt,(UWORD)4,SQL_C_CHAR,buff,sizeof(buff)-1,&amp;len);\r\n\/\/    if(!strcmp((char *)buff,\"17\") &amp;&amp; flag==1)\r\n\/\/     flag=1;\r\n\/\/    else\r\n\/\/     flag=0;\r\n   strcat(data,(char *)buff);\r\n\r\n   strcat(data, \"\\t\");\r\n   memset(buff,NULL,sizeof(buff));\r\n   SQLGetData(hstmt,(UWORD)5,SQL_C_CHAR,buff,sizeof(buff)-1,&amp;len);\r\n   if(!strcmp((char *)buff,\"0\"))\r\n       strcat(data,\"\u7537\");\r\n   else\r\n    strcat(data,\"\u5973\");\r\n\r\n   strcat(data, \"\\t\");\r\n   memset(buff,NULL,sizeof(buff));\r\n   SQLGetData(hstmt,(UWORD)7,SQL_C_CHAR,buff,sizeof(buff)-1,&amp;len);\r\n   strcat(data,(char *)buff);\r\n  }\r\n  else\r\n   return 1;\r\n\/\/  if(flag)\r\n      printf(\"%s\\n\",data);\r\n }\r\n}\r\n\r\nint main()\r\n{\r\n    SQLHSTMT hstmt;\r\n    char buff[100];\r\n    char query[100];\r\n char choose[10];\r\n int tmp;\r\n    int i,j;\r\n \r\n    system(\"color a\");\r\n    if(!Connect_SQLServer())\r\n    {\r\n        printf(\"\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\\n\");\r\n        getch();\r\n        return -1;\r\n    }\r\n    printf(\"\u6570\u636e\u5e93\u8fde\u63a5\u6210\u529f\\n\");\r\n printf(\"\u8bf7\u8f93\u5165\u68c0\u7d22\u65b9\u5f0f:\\n1.QQ\u53f7\\t2.\u59d3\u540d\\t3.\u7fa4\u53f7\\n\");\r\n fflush(stdin);\r\n if(scanf(\"%d\",&amp;tmp)!=1) return -1;\r\n memset(choose,NULL,sizeof(choose));\r\n switch (tmp)\r\n {\r\n case 1:\r\n  strcat(choose,\"QQNum\");\r\n  break;\r\n case 2:\r\n  strcat(choose,\"Nick\");\r\n  break;\r\n case 3:\r\n  strcat(choose,\"QunNum\");\r\n  break;\r\n default:\r\n  return -1;\r\n }\r\n\r\n \r\n    printf(\"\u8bf7\u8f93\u5165\u8981\u67e5\u8be2\u7684\u5185\u5bb9:\");\r\n    memset(query,NULL,sizeof(query));\r\n    fflush(stdin);\r\n    gets(query);\r\n \r\n printf(\"QQNum\\t\\tName\\tAge\\tSex\\tQunNum\\n\");\r\n    for(i=1,j=1; i&lt;=11; i++)\r\n    {\r\n  \/\/printf(\"\u6b63\u5728\u67e5\u8be2\u7b2c%d\u4e2a\u6570\u636e\u5e93\u3002\\n\",i);\r\n        memset(buff,NULL,sizeof(buff));\r\n        sprintf(buff,\"GroupData%d\",i);\r\n        if(!ChangeDB(buff))\r\n            break;\r\n        for(;; j++)\r\n        {\r\n   \/\/printf(\"\u6b63\u5728\u67e5\u8be2\u7b2c%d\u4e2a\u8868\u3002\\n\",j);\r\n   memset(buff,NULL,sizeof(buff));\r\n   sprintf(buff,\"select * from Group%d where %s like '%%%s%%';\",j,choose,query);\r\n            if(!QueryDB(&amp;hstmt,buff))\r\n    break;\r\n   \r\n            show_results(hstmt);\r\n        }\r\n    }\r\n printf(\"\u67e5\u8be2\u5b8c\u6210\u3002\\n\");\r\n system(\"pause\");\r\n \r\n    return 0;\r\n}<\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\n\t#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;windows.h&gt;<br \/>\n#include &lt;conio.h&gt;<br \/>\n#include &lt;sql.h&gt;<br \/>\n#include &lt;sqlext.h&gt;<br \/>\n#include &lt;sqltypes.h&gt;<br \/>\n#include &lt;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[324],"tags":[],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MSSQL_Connector - Wayne&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/weizn.net\/?p=62\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MSSQL_Connector - Wayne&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;windows.h&gt; #include &lt;conio.h&gt; #include &lt;sql.h&gt; #include &lt;sqlext.h&gt; #include &lt;sqltypes.h&gt; #include &lt;...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/weizn.net\/?p=62\" \/>\n<meta property=\"og:site_name\" content=\"Wayne&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2013-12-14T14:41:14+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"zinan\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/weizn.net\/#website\",\"url\":\"http:\/\/weizn.net\/\",\"name\":\"Wayne&#039;s Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/weizn.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/weizn.net\/?p=62#webpage\",\"url\":\"http:\/\/weizn.net\/?p=62\",\"name\":\"MSSQL_Connector - Wayne&#039;s Blog\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/#website\"},\"datePublished\":\"2013-12-14T14:41:14+00:00\",\"dateModified\":\"2013-12-14T14:41:14+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/weizn.net\/?p=62#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/weizn.net\/?p=62\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/weizn.net\/?p=62#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\\u9996\\u9875\",\"item\":\"http:\/\/weizn.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MSSQL_Connector\"}]},{\"@type\":\"Article\",\"@id\":\"http:\/\/weizn.net\/?p=62#article\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/?p=62#webpage\"},\"author\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"headline\":\"MSSQL_Connector\",\"datePublished\":\"2013-12-14T14:41:14+00:00\",\"dateModified\":\"2013-12-14T14:41:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=62#webpage\"},\"wordCount\":2,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"articleSection\":[\"C\/C++\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/weizn.net\/?p=62#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\",\"name\":\"zinan\",\"logo\":{\"@id\":\"http:\/\/weizn.net\/#personlogo\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MSSQL_Connector - Wayne&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/weizn.net\/?p=62","og_locale":"zh_CN","og_type":"article","og_title":"MSSQL_Connector - Wayne&#039;s Blog","og_description":"#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;windows.h&gt; #include &lt;conio.h&gt; #include &lt;sql.h&gt; #include &lt;sqlext.h&gt; #include &lt;sqltypes.h&gt; #include &lt;...","og_url":"http:\/\/weizn.net\/?p=62","og_site_name":"Wayne&#039;s Blog","article_published_time":"2013-12-14T14:41:14+00:00","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"zinan","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/weizn.net\/#website","url":"http:\/\/weizn.net\/","name":"Wayne&#039;s Blog","description":"","publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/weizn.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"WebPage","@id":"http:\/\/weizn.net\/?p=62#webpage","url":"http:\/\/weizn.net\/?p=62","name":"MSSQL_Connector - Wayne&#039;s Blog","isPartOf":{"@id":"http:\/\/weizn.net\/#website"},"datePublished":"2013-12-14T14:41:14+00:00","dateModified":"2013-12-14T14:41:14+00:00","breadcrumb":{"@id":"http:\/\/weizn.net\/?p=62#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/weizn.net\/?p=62"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/weizn.net\/?p=62#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/weizn.net\/"},{"@type":"ListItem","position":2,"name":"MSSQL_Connector"}]},{"@type":"Article","@id":"http:\/\/weizn.net\/?p=62#article","isPartOf":{"@id":"http:\/\/weizn.net\/?p=62#webpage"},"author":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"headline":"MSSQL_Connector","datePublished":"2013-12-14T14:41:14+00:00","dateModified":"2013-12-14T14:41:14+00:00","mainEntityOfPage":{"@id":"http:\/\/weizn.net\/?p=62#webpage"},"wordCount":2,"commentCount":0,"publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"articleSection":["C\/C++"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/weizn.net\/?p=62#respond"]}]},{"@type":["Person","Organization"],"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264","name":"zinan","logo":{"@id":"http:\/\/weizn.net\/#personlogo"}}]}},"_links":{"self":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=62"}],"version-history":[{"count":0,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/62\/revisions"}],"wp:attachment":[{"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}