1 //++++++++++++++++++++++++++
2 // SQLprog2.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 UCHAR szSqlStr[128]= "select build_date_no from DATECTL";
27
28 ///Var
29 SDWORD cbModel;
30 RETCODE retcode;
31 //char* buf="abc";
32
33 ///File_Var
34 FILE *outfile;
35
36 ///Open outfile
37 outfile=fopen("mid.txt","wb");
38
39 ///SQL alloc memory
40 SQLAllocEnv (&hEnv); // Allocate memory for ODBC Environment handle
41
42 ///SQL alloc connect
43 SQLAllocConnect (hEnv, &hDBC); // Allocate memory for the connection handle
44
45 ///SQLDriver connect
46 SQLTCHAR *srv = (SQLTCHAR *)"SERVER=10.11.168.100;DSN=SQL_DAT;UID=SQLabc;PWD=SQL123";
47 retcode = SQLDriverConnect(hDBC, NULL, srv, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
48
49 ///if retcode =0_or_1
50 if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
51 {
52 ///SQL alloc statement
53 retcode = SQLAllocStmt (hDBC, &hStmt); // Allocate memory for the statement handle
54 fputc(retcode,outfile);
55
56 ///SQL prepare
57 retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr)); // Prepare the SQL statement by assigning it to the statement handle
58 fputc(retcode,outfile);
59
60 ///SQL execute
61 retcode = SQLExecute (hStmt);// Execute the SQL statement handle
62 fputc(retcode,outfile);
63
64 ///Temp declare
65 #define NAME_LEN 10
66 SQLINTEGER sCustID, cbName, cbAge, cbBirthday,xyz ;
67 SQLCHAR szName[NAME_LEN];
68 char sbuf[10];
69
70 ///SQL fetch
71 retcode = SQLFetch(hStmt);
72
73 ///SQL getdata
74 SQLGetData(hStmt,1, SQL_C_CHAR, szName, NAME_LEN, &xyz);
75
76 ///OutFile
77 fputc(retcode,outfile);
78 //fprintf(outfile, "%s", szName );
79 sprintf(sbuf, "%s", szName );
80 fprintf(outfile, "%s", sbuf );
81
82 ///Display
83 MessageBox(NULL, sbuf, "Date in DMY format", MB_OK);
84
85 ///SQL bindcol
86 SQLBindCol (hStmt, 1, SQL_C_CHAR, szModel, sizeof(szModel), &cbModel);// Project only column 1 which is the models
87
88 ///SQL fetch
89 retcode = SQLFetch (hStmt);// Get row of data from the result set defined above in the statement
90
91 ///OutFile
92 fputc(retcode,outfile);
93
94 ///SQL free statement
95 SQLFreeStmt (hStmt, SQL_DROP);// Free the allocated statement handle
96
97 ///SQL disconnect
98 SQLDisconnect (hDBC);// Disconnect from datasource
99
100 ///+
101 }//end if
102
103 ///else= FALSE
104 else
105 {
106 ///Write "ng"
107 fprintf(outfile,"ng");
108
109 ///+
110 }//end else
111
112 ///OutFile
113 std::cout << retcode << "\n";
114 fputc(retcode,outfile);
115
116 ///Close outfile
117 fclose(outfile);
118
119 ///SQL free connect
120 SQLFreeConnect (hDBC);// Free the allocated connection handle
121
122 ///SQL free_env
123 SQLFreeEnv (hEnv);// Free the allocated ODBC environment handle
124
125 ///main_end
126 return 0;
127 } //main_end
128
This Blog is dedicated to Sir Johnson Lin
Sample C program to fetch data from SQL database 使用C語言程式讀取資料庫內容
@ 下載程式碼 Download source code
標籤:
data source,
database,
fetch,
SQL
Simple C program to connect SQL database 簡易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 連接至目錄首頁 +++
標籤:
data source,
database,
SQL
訂閱:
文章 (Atom)

