This Blog is dedicated to Sir Johnson Lin
Learn flow chart from C language , master C with the chart!

Simple C program to connect SQL database 簡易C語言程式連結資料庫

D:\cppCoding\xi_code\SQLprog1\SQLprog1.c++
 1 //++++++++++++++++++++++++++
 2  //      SQLprog1.cpp
 3  //++++++++++++++++++++++++++
 4  
 5  ///#include header
 6  #include <windows.h>
 7  #include <sqlext.h>
 8  #include <sql.h>
 9  //#include <cstdlib>
10  #include <iostream>
11  #include <stdio.h>
12  
13  ///main_in
14  int main(int argc,char *argv[])
15  {
16      ///declare Handle_Var
17      HENV hEnv = NULL;         // Env Handle from SQLAllocEnv()
18      HDBC hDBC = NULL;         // Connection handle
19      HSTMT hStmt = NULL;        // Statement handle
20      
21      ///declare UCHAR_sz
22      UCHAR szDSN[SQL_MAX_DSN_LENGTH+1] = "SQL_DAT";
23      UCHAR szUID[9] = "SQLabc";      // User ID buffer
24      UCHAR szPasswd[9] = "SQL123";      // Password buffer
25      UCHAR szModel[128];        // Model buffer
26      
27      ///Var
28      SDWORD cbModel;
29      RETCODE retcode;
30      
31      ///File_Var
32      FILE *outfile;
33      
34      ///Open outfile
35      outfile=fopen("mid.txt","wb");
36      
37      ///SQL alloc memory
38      SQLAllocEnv (&hEnv);       // Allocate memory for ODBC Environment handle
39      
40      ///SQL alloc connect
41      SQLAllocConnect (hEnv, &hDBC);     // Allocate memory for the connection handle
42      
43      ///SQL connect
44      retcode = SQLConnect (hDBC, szDSN, 8, szUID, 5, szPasswd,5);// Connect to the data source "szDSN" using userid and password.
45      
46      ///if retcode =0_or_1
47      if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
48      {
49          ///Write "ok"
50          fprintf(outfile,"ok");
51          
52          ///+
53      }//end if
54      
55      ///else= FALSE
56      else
57      {
58          ///Write "ng"
59          fprintf(outfile,"ng");
60          
61          ///+
62      }//end else
63      
64      ///Close outfile
65      fclose(outfile);
66      
67      ///SQL free connect
68      SQLFreeConnect (hDBC);// Free the allocated connection handle
69      
70      ///SQL free_env
71      SQLFreeEnv (hEnv);// Free the allocated ODBC environment handle
72      
73      ///main_end
74      return 0;
75  } //main_end
76  

@ 下載程式碼 Download source code

+++ Link To Home Page Index 連接至目錄首頁 +++

沒有留言:

張貼留言