質問編集履歴

3

プログラムの更新

2020/05/28 12:46

投稿

yasu2711
yasu2711

スコア7

test CHANGED
File without changes
test CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
  ```
40
40
 
41
- //#include "stdafx.h"
41
+ #include "stdafx.h"
42
42
 
43
43
  //include the below additional libraries
44
44
 

2

プログラムの更新

2020/05/28 12:46

投稿

yasu2711
yasu2711

スコア7

test CHANGED
File without changes
test CHANGED
@@ -106,7 +106,7 @@
106
106
 
107
107
  //(SQLWCHAR*)L"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=ONAMBA;UID=ONAMBA",
108
108
 
109
- (SQLWCHAR*)L"DRIVER={SQL Server};SERVER=DESKTOP-T1O410V, 1433;DATABASE=ONAMBA;Trusted=true;",
109
+ (SQLWCHAR*)L"DRIVER={SQL Server};Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ONAMBA;Data Source=DESKTOP-T1O410V;",
110
110
 
111
111
  SQL_NTS,
112
112
 

1

プログラムの更新

2020/05/12 10:31

投稿

yasu2711
yasu2711

スコア7

test CHANGED
File without changes
test CHANGED
@@ -38,36 +38,196 @@
38
38
 
39
39
  ```
40
40
 
41
+ //#include "stdafx.h"
42
+
43
+ //include the below additional libraries
44
+
41
- #include <stdio.h>
45
+ #include <iostream>
42
46
 
43
47
  #include <windows.h>
44
48
 
49
+ #include <sqlext.h>
50
+
51
+ #include <sqltypes.h>
52
+
45
- #include <string>
53
+ #include <sql.h>
54
+
55
+ //use the std namespace
46
56
 
47
57
  using namespace std;
48
58
 
49
59
 
50
60
 
51
- int main(void){
52
-
53
- std::string^ str;
54
-
55
- std::string^ SqlConnection^ sqlConn = gcnew SqlConnection("Data Source=DESKTOP-T1O410V\Owner;Initial Catalog=ONAMBA;Integrated Security=True;User Instance=True");
56
-
57
- std::string^ sqlConn->Open();
58
-
59
- std::string^ str = "Select test FROM T_Forcast";
60
-
61
- std::string^ SqlCommand^ sqlCmd = gcnew SqlCommand(str, sqlConn);
62
-
63
- std::string^ SqlDataReader^ exeReader = sqlCmd->ExecuteReader();
64
-
65
- std::string^ sqlConn->Close();
66
-
67
-
61
+ int main() {
62
+
63
+ #define SQL_RESULT_LEN 240
64
+
65
+ #define SQL_RETURN_CODE_LEN 1000
66
+
67
+ //define handles and variables
68
+
69
+ SQLHANDLE sqlConnHandle;
70
+
71
+ SQLHANDLE sqlStmtHandle;
72
+
73
+ SQLHANDLE sqlEnvHandle;
74
+
75
+ SQLWCHAR retconstring[SQL_RETURN_CODE_LEN];
76
+
77
+ //initializations
78
+
79
+ sqlConnHandle = NULL;
80
+
81
+ sqlStmtHandle = NULL;
82
+
83
+ //allocations
84
+
85
+ if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlEnvHandle))
86
+
87
+ goto COMPLETED;
88
+
89
+ if (SQL_SUCCESS != SQLSetEnvAttr(sqlEnvHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
90
+
91
+ goto COMPLETED;
92
+
93
+ if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, sqlEnvHandle, &sqlConnHandle))
94
+
95
+ goto COMPLETED;
96
+
97
+ //output
98
+
99
+ cout << "Attempting connection to SQL Server...";
100
+
101
+ cout << "\n";
102
+
103
+ switch (SQLDriverConnect(sqlConnHandle,
104
+
105
+ NULL,
106
+
107
+ //(SQLWCHAR*)L"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=ONAMBA;UID=ONAMBA",
108
+
109
+ (SQLWCHAR*)L"DRIVER={SQL Server};SERVER=DESKTOP-T1O410V, 1433;DATABASE=ONAMBA;Trusted=true;",
110
+
111
+ SQL_NTS,
112
+
113
+ retconstring,
114
+
115
+ 1024,
116
+
117
+ NULL,
118
+
119
+ SQL_DRIVER_NOPROMPT)) {
120
+
121
+ case SQL_SUCCESS:
122
+
123
+ cout << "Successfully connected to SQL Server";
124
+
125
+ cout << "\n";
126
+
127
+ break;
128
+
129
+ case SQL_SUCCESS_WITH_INFO:
130
+
131
+ cout << "Successfully connected to SQL Server";
132
+
133
+ cout << "\n";
134
+
135
+ break;
136
+
137
+ case SQL_INVALID_HANDLE:
138
+
139
+ cout << "Could not connect to SQL Server";
140
+
141
+ cout << "\n";
142
+
143
+ goto COMPLETED;
144
+
145
+ case SQL_ERROR:
146
+
147
+ cout << "Could not connect to SQL Server";
148
+
149
+ cout << "\n";
150
+
151
+ goto COMPLETED;
152
+
153
+ default:
154
+
155
+ break;
156
+
157
+ }
158
+
159
+ //if there is a problem connecting then exit application
160
+
161
+ if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, sqlConnHandle, &sqlStmtHandle))
162
+
163
+ goto COMPLETED;
164
+
165
+ //output
166
+
167
+ cout << "\n";
168
+
169
+ cout << "Executing T-SQL query...";
170
+
171
+ cout << "\n";
172
+
173
+ //if there is a problem executing the query then exit application
174
+
175
+ //else display query result
176
+
177
+ if (SQL_SUCCESS != SQLExecDirect(sqlStmtHandle, (SQLWCHAR*)L"SELECT @@VERSION", SQL_NTS)) {
178
+
179
+ cout << "Error querying SQL Server";
180
+
181
+ cout << "\n";
182
+
183
+ goto COMPLETED;
184
+
185
+ }
186
+
187
+ else {
188
+
189
+ //declare output variable and pointer
190
+
191
+ SQLCHAR sqlVersion[SQL_RESULT_LEN];
192
+
193
+ SQLINTEGER ptrSqlVersion;
194
+
195
+ while (SQLFetch(sqlStmtHandle) == SQL_SUCCESS) {
196
+
197
+ SQLGetData(sqlStmtHandle, 1, SQL_CHAR, sqlVersion, SQL_RESULT_LEN, &ptrSqlVersion);
198
+
199
+ //display query result
200
+
201
+ cout << "\nQuery Result:\n\n";
202
+
203
+ cout << sqlVersion << endl;
204
+
205
+ }
206
+
207
+ }
208
+
209
+ //close connection and free resources
210
+
211
+ COMPLETED:
212
+
213
+ SQLFreeHandle(SQL_HANDLE_STMT, sqlStmtHandle);
214
+
215
+ SQLDisconnect(sqlConnHandle);
216
+
217
+ SQLFreeHandle(SQL_HANDLE_DBC, sqlConnHandle);
218
+
219
+ SQLFreeHandle(SQL_HANDLE_ENV, sqlEnvHandle);
220
+
221
+ //pause the console window - exit when key is pressed
222
+
223
+ cout << "\nPress any key to exit...";
224
+
225
+ getchar();
68
226
 
69
227
  }
70
228
 
229
+
230
+
71
231
  ```
72
232
 
73
233
  まだDebugにも至っておりません。