1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include<stdio.h> #include<windows.h> #include<sql.h> #include<sqlext.h> #include<sqltypes.h> int main() { SQLRETURN ret; SQLHENV henv; SQLHDBC hdbc; SQLHSTMT hstmt;
ret = SQLAllocHandle(SQL_HANDLE_ENV, NULL, &henv); ret = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER); ret = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
ret = SQLConnect(hdbc, (SQLCHAR*)"testlyl", SQL_NTS, (SQLCHAR*)"abc", SQL_NTS, (SQLCHAR*)"def010", SQL_NTS); if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)) { printf("连接数据库失败!\n"); return -1; }
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); SQLCHAR sql1[] = "use DCWLW"; SQLCHAR sql2[] = "SELECT [report_5001_id]\ , [report_str]\ , isnull([row_state],0) as row_state\ , [create_date]\ , [barcode]\ , [states]\ , [dy]\ , [dl]\ , [wdmax]\ , [wdmin]\ , [soc]\ , [err_str]\ , [err_str2]\ , [err_str3]\ , [err_str4]\ FROM[DCWLW].[dbo].[report_5001]\ where cast([dl] as decimal) > 50.0 and create_date > '2021'"; ret = SQLExecDirect(hstmt, sql1, SQL_NTS); ret = SQLExecDirect(hstmt, sql2, SQL_NTS); if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) { SQLCHAR str1[50], str2[50], str3[50], str4[50], str5[50], str6[50]; SQLLEN len_str1, len_str2, len_str3, len_str4, len_str5, len_str6; while (SQLFetch(hstmt) != SQL_NO_DATA) { SQLGetData(hstmt, 1, SQL_C_CHAR, str1, 50, &len_str1); SQLGetData(hstmt, 2, SQL_C_CHAR, str2, 50, &len_str2); SQLGetData(hstmt, 3, SQL_C_CHAR, str3, 50, &len_str3); SQLGetData(hstmt, 4, SQL_C_CHAR, str4, 50, &len_str4); SQLGetData(hstmt, 5, SQL_C_CHAR, str5, 50, &len_str5); SQLGetData(hstmt, 6, SQL_C_CHAR, str6, 50, &len_str6); printf("%s\t%s\t%s\t%s\t%s\t%s\n", str1, str2, str3, str4, str5, str6); } }
SQLFreeHandle(SQL_HANDLE_DBC, hdbc); SQLFreeHandle(SQL_HANDLE_ENV, henv); return 0; }
|