質問編集履歴
3
プログラムの更新
title
CHANGED
File without changes
|
body
CHANGED
@@ -18,7 +18,7 @@
|
|
18
18
|
Visual C++
|
19
19
|
|
20
20
|
```
|
21
|
-
|
21
|
+
#include "stdafx.h"
|
22
22
|
//include the below additional libraries
|
23
23
|
#include <iostream>
|
24
24
|
#include <windows.h>
|
2
プログラムの更新
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};
|
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
プログラムの更新
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 <
|
23
|
+
#include <iostream>
|
22
24
|
#include <windows.h>
|
25
|
+
#include <sqlext.h>
|
26
|
+
#include <sqltypes.h>
|
23
|
-
#include <
|
27
|
+
#include <sql.h>
|
28
|
+
//use the std namespace
|
24
29
|
using namespace std;
|
25
30
|
|
26
|
-
int main(
|
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
|
-
|
57
|
+
retconstring,
|
58
|
+
1024,
|
59
|
+
NULL,
|
60
|
+
SQL_DRIVER_NOPROMPT)) {
|
61
|
+
case SQL_SUCCESS:
|
28
|
-
|
62
|
+
cout << "Successfully connected to SQL Server";
|
29
|
-
|
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
|
-
|
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
|
-
|
99
|
+
SQLGetData(sqlStmtHandle, 1, SQL_CHAR, sqlVersion, SQL_RESULT_LEN, &ptrSqlVersion);
|
100
|
+
//display query result
|
32
|
-
|
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
|
-
|
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
|
おわかりの方がいらっしゃいましたらお教えください。
|