teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

プログラムの更新

2020/05/28 12:46

投稿

yasu2711
yasu2711

スコア7

title CHANGED
File without changes
body CHANGED
@@ -18,7 +18,7 @@
18
18
  Visual C++
19
19
 
20
20
  ```
21
- //#include "stdafx.h"
21
+ #include "stdafx.h"
22
22
  //include the below additional libraries
23
23
  #include <iostream>
24
24
  #include <windows.h>

2

プログラムの更新

2020/05/28 12:46

投稿

yasu2711
yasu2711

スコア7

title CHANGED
File without changes
body CHANGED
@@ -52,7 +52,7 @@
52
52
  switch (SQLDriverConnect(sqlConnHandle,
53
53
  NULL,
54
54
  //(SQLWCHAR*)L"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=ONAMBA;UID=ONAMBA",
55
- (SQLWCHAR*)L"DRIVER={SQL Server};SERVER=DESKTOP-T1O410V, 1433;DATABASE=ONAMBA;Trusted=true;",
55
+ (SQLWCHAR*)L"DRIVER={SQL Server};Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ONAMBA;Data Source=DESKTOP-T1O410V;",
56
56
  SQL_NTS,
57
57
  retconstring,
58
58
  1024,

1

プログラムの更新

2020/05/12 10:31

投稿

yasu2711
yasu2711

スコア7

title CHANGED
File without changes
body CHANGED
@@ -18,21 +18,101 @@
18
18
  Visual C++
19
19
 
20
20
  ```
21
+ //#include "stdafx.h"
22
+ //include the below additional libraries
21
- #include <stdio.h>
23
+ #include <iostream>
22
24
  #include <windows.h>
25
+ #include <sqlext.h>
26
+ #include <sqltypes.h>
23
- #include <string>
27
+ #include <sql.h>
28
+ //use the std namespace
24
29
  using namespace std;
25
30
 
26
- int main(void){
31
+ int main() {
32
+ #define SQL_RESULT_LEN 240
33
+ #define SQL_RETURN_CODE_LEN 1000
34
+ //define handles and variables
35
+ SQLHANDLE sqlConnHandle;
36
+ SQLHANDLE sqlStmtHandle;
37
+ SQLHANDLE sqlEnvHandle;
38
+ SQLWCHAR retconstring[SQL_RETURN_CODE_LEN];
39
+ //initializations
40
+ sqlConnHandle = NULL;
41
+ sqlStmtHandle = NULL;
42
+ //allocations
43
+ if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlEnvHandle))
44
+ goto COMPLETED;
45
+ if (SQL_SUCCESS != SQLSetEnvAttr(sqlEnvHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
46
+ goto COMPLETED;
47
+ if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, sqlEnvHandle, &sqlConnHandle))
48
+ goto COMPLETED;
49
+ //output
50
+ cout << "Attempting connection to SQL Server...";
51
+ cout << "\n";
52
+ switch (SQLDriverConnect(sqlConnHandle,
53
+ NULL,
54
+ //(SQLWCHAR*)L"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=ONAMBA;UID=ONAMBA",
55
+ (SQLWCHAR*)L"DRIVER={SQL Server};SERVER=DESKTOP-T1O410V, 1433;DATABASE=ONAMBA;Trusted=true;",
56
+ SQL_NTS,
27
- std::string^ str;
57
+ retconstring,
58
+ 1024,
59
+ NULL,
60
+ SQL_DRIVER_NOPROMPT)) {
61
+ case SQL_SUCCESS:
28
- std::string^ SqlConnection^ sqlConn = gcnew SqlConnection("Data Source=DESKTOP-T1O410V\Owner;Initial Catalog=ONAMBA;Integrated Security=True;User Instance=True");
62
+ cout << "Successfully connected to SQL Server";
29
- std::string^ sqlConn->Open();
63
+ cout << "\n";
64
+ break;
65
+ case SQL_SUCCESS_WITH_INFO:
66
+ cout << "Successfully connected to SQL Server";
67
+ cout << "\n";
68
+ break;
69
+ case SQL_INVALID_HANDLE:
70
+ cout << "Could not connect to SQL Server";
71
+ cout << "\n";
72
+ goto COMPLETED;
73
+ case SQL_ERROR:
74
+ cout << "Could not connect to SQL Server";
75
+ cout << "\n";
76
+ goto COMPLETED;
77
+ default:
78
+ break;
79
+ }
80
+ //if there is a problem connecting then exit application
81
+ if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, sqlConnHandle, &sqlStmtHandle))
82
+ goto COMPLETED;
83
+ //output
84
+ cout << "\n";
30
- std::string^ str = "Select test FROM T_Forcast";
85
+ cout << "Executing T-SQL query...";
86
+ cout << "\n";
87
+ //if there is a problem executing the query then exit application
88
+ //else display query result
89
+ if (SQL_SUCCESS != SQLExecDirect(sqlStmtHandle, (SQLWCHAR*)L"SELECT @@VERSION", SQL_NTS)) {
90
+ cout << "Error querying SQL Server";
91
+ cout << "\n";
92
+ goto COMPLETED;
93
+ }
94
+ else {
95
+ //declare output variable and pointer
96
+ SQLCHAR sqlVersion[SQL_RESULT_LEN];
97
+ SQLINTEGER ptrSqlVersion;
98
+ while (SQLFetch(sqlStmtHandle) == SQL_SUCCESS) {
31
- std::string^ SqlCommand^ sqlCmd = gcnew SqlCommand(str, sqlConn);
99
+ SQLGetData(sqlStmtHandle, 1, SQL_CHAR, sqlVersion, SQL_RESULT_LEN, &ptrSqlVersion);
100
+ //display query result
32
- std::string^ SqlDataReader^ exeReader = sqlCmd->ExecuteReader();
101
+ cout << "\nQuery Result:\n\n";
102
+ cout << sqlVersion << endl;
103
+ }
104
+ }
105
+ //close connection and free resources
106
+ COMPLETED:
107
+ SQLFreeHandle(SQL_HANDLE_STMT, sqlStmtHandle);
33
- std::string^ sqlConn->Close();
108
+ SQLDisconnect(sqlConnHandle);
109
+ SQLFreeHandle(SQL_HANDLE_DBC, sqlConnHandle);
110
+ SQLFreeHandle(SQL_HANDLE_ENV, sqlEnvHandle);
111
+ //pause the console window - exit when key is pressed
112
+ cout << "\nPress any key to exit...";
113
+ getchar();
114
+ }
34
115
 
35
- }
36
116
  ```
37
117
  まだDebugにも至っておりません。
38
118
  おわかりの方がいらっしゃいましたらお教えください。