embedding SQL in visual studio C++ code - embedded-sql

Can anyone tell me (showing sample code) how to have embedded MS SQL Server SQL queries in a visual studio Win32 console application C++ program (header files, keywords, etc.)? I've set up a C++ project with source file and the standard EXEC SQL notation doesn't work. I did connect to the database through the server explorer window. So far, I've had no luck. I just want a little sample to show my class. Here's the basic code I've tried:
// DB TEST PROGRAM
#include <iostream>
using namespace std;
void main()
{
int customer;
double balance;
customer = 10013;
EXEC SQL SELECT CUS_BALANCE INTO :balance
FROM CUSTOMER
WHERE CUS_NUMBER = :Customer;
cout << "Customer " << customer << " has a balance of " << balance;
cin.ignore(); // to hold screen
}

Related

Visual Studio 2022 not allowing c++20 even though i have got /std:c++20

I have got the following code:
#include<iostream>
#include<ranges>
#include<string>
auto f(auto& x, auto& y)
{
std::cout << x << y << "\n";
}
struct s
{
int x;
std::string y;
};
int main()
{
std::cout << "Hello World" << "\n";
}
and I have the /std:c++20 in my configuration:
ISO C++20 Standard (/std:c++20)
I have tried using that and I have the following error:
Severity Code Description Project File Line Suppression State
Error (active) E1598 'auto' is not allowed here Project1 C:\Users\robert\source\repos\Project1\Project1\Source.cpp 5
1>Source.cpp
1>The contents of <ranges> are available only with C++20 or later.
1>C:\Users\rober\source\repos\Project1\Project1\Source.cpp(5,15): error C3533: a parameter cannot have a type that contains 'auto'
1>C:\Users\rober\source\repos\Project1\Project1\Source.cpp(5,24): error C3533: a parameter cannot have a type that contains 'auto'
I am expecting my code to run fine because all of the c++20 headers are fine like std::ranges etc. It is by default on Visual Studio 2022 that the language is C++14 but I do not know why it is not running. So, it just resulted in the error shown above.
I have seen the other question about auto is not allowed here but that was in c++17 and the answer to that was it will come with c++20, but i have /std:c++20 in my projects -> propeties bit of visual studio 2022

Linking gRPC on Windows for VisualC++

I am trying to use gRPC in a Visual C++ project.
So far I have:
1) Build gRPC with vcpkg: vcpkg install grpc:x64-windows
2) Integrated the vcpgk libraries with visual studio: vcpkg integrate install
So far, so good - intellisense autocompletes the namespace etc.
My client cpp file looks like this:
#include "pch.h"
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp\grpcpp.h>
#include "GRPCServerInterface.grpc.pb.h"
#include "FileFormat.pb.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using namespace GRPCServerInterface;
int main()
{
std::cout << "Hello World!\n";
// prepare send message & payload
IsFormatSupportedInput msg;
msg.set_fileextension(".asp");
// prepare reply
IsFormatSupportedOutput rpl;
// connect
FileHandler::Stub ClientStub = FileHandler::Stub(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));
ClientContext context;
// execute rpc
Status status = ClientStub.IsFormatSupported(&context, msg, &rpl);
// handle result
if (status.ok())
{
std::cout << "Format supported says:" << std::endl << "\t formats read: " << rpl.readsupportedformats() << std::endl << "\t formats write: " << rpl.writesupportedformats() << std::endl;
}
else
{
std::cout << status.error_code() << ": " << status.error_message() << std::endl;
}
}
All messages & proto files exits and work in general, since I already use them in python and c# projects.
When building, Visual Studio generates a boatload of 125 errors, all in files I never touched.
In GRPCServerInterface.pb.h, there is identifier GOOGLE_DCHECK is undefined
All other errors are member abc may not be initialized in various header files in the grpc includes, for example
member "google::protobuf::Any::kIndexInFileMessages" may not be initialized in file any.pb.h. Many more in type.pb.h and descriptor.pbp.h.
Last but not least, I get prompted add #iclude "pch.h" to the auto-generated protobuf classes grpcserverinterface.grpc.pb.cc and grpcserverinterface.pb.cc - adding it changes a bit, but basically all errors are still undefined symbol and member may not be initialized. And I really do not want to modify auto-generated code every time.
What am I missing? Or is it just a fruitless endeavor to try using grpc with Visual Studio and should I just move to a build framework like bazel?
Solved it!
Two steps for solving:
1) I disabled precompiled headers for the whole project - this made the #include "pch.h go away. You could probalby get away with disabling it just for the protobuf files, as it can be done on a per-file basis.
2) One of the last errors listed was unresolved external symbol __imp_WSASocketA, which finally led me to this question Unresolved external symbol LNK2019. I just included #pragma comment(lib, "Ws2_32.lib") in one source file, and now everything works just perfect.

Unable to debug c++ program with class implementation

I am trying to setup debugging environment in visual studio and I hope I was successful in doing it,because when I am trying to debug a normal math c++ program then my break point is hitting(or first line of code as default breakpoint) but when I am trying to debug the main program with class header file name included in the main program(i.e with class object or class implementation) then my breakpoint are not hitting. Any help where I am going wrong??
#include <iostream>
#include "Sales_item.h"
using namespace std;
int main()
{
cout<<"hello";
Sales_item item1, item2;
cin >> item1 >> item2; // read a pair of transactions
cout << item1 + item2 << endl; // print their sum
return 0;
}
This is showing in cppdgb console:
C:\Users\admin\Documents\cpp>c:\Users\admin.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe
--stdin=Microsoft-MIEngine-In-w2a4a5fw.4hb --stdout=Microsoft-MIEngine-Out-q2oc4jsv.b14 --stderr=Microsoft-MIEngine-Error-rlvf2qhl.2bw --pid=Microsoft-MIEngine-Pid-3fayxkbu.ji4 --dbgExe=C:\MinGW\bin\gdb.exe --interpreter=mi

Visual Studio Intel Threading Building Blocks DLL error

I am using an example of Intel TBB code I found on SO:
#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"
#include "tbb/task_scheduler_init.h"
#include <iostream>
#include <vector>
struct mytask {
mytask(size_t n)
:_n(n)
{}
void operator()() {
for (int i=0;i<1000000;++i) {} // Deliberately run slow
std::cerr << "[" << _n << "]";
}
size_t _n;
};
struct executor
{
executor(std::vector<mytask>& t) :_tasks(t)
{}
executor(executor& e,tbb::split) :_tasks(e._tasks)
{}
void operator()(const tbb::blocked_range<size_t>& r) const {
for (size_t i=r.begin();i!=r.end();++i)
_tasks[i]();
}
std::vector<mytask>& _tasks;
};
int main(int,char**) {
tbb::task_scheduler_init init; // Automatic number of threads
// tbb::task_scheduler_init init(2); // Explicit number of threads
std::vector<mytask> tasks;
for (int i=0;i<1000;++i){
tasks.push_back(mytask(i));
}
executor exec(tasks);
tbb::parallel_for(tbb::blocked_range<size_t>(0,tasks.size()),exec);
std::cerr << std::endl;
return 0;
}
The code builds fine, but when I go to run I get the error:
The program can't start because tbb_debug.dll is missing from your
computer. Try reinstalling the program to fix this problem.
I think this is probably the path I need to set somewhere within VS2012:
C:\Program Files (x86)\Intel\Composer XE 2013 SP1\redist\ia32\tbb\vc11
because it contains tbb_debug.dll.
Where does this path need to be set?
EDIT: I tried setting in the "Executable directories" path section but that didnt work.
In solution explorer, right click the project and open "Properties"
Open "Configuration Properties" -> "Debugging".
In the "Environment" line, add "PATH=C:\Program Files (x86)\Intel\Composer XE 2013"
Make sure "Merge Environment" is set to "Yes".

Phantom syntax errors in Visual C++ 2010 Express

I just started learning C++, and I'm getting weird 'phantom' syntax errors in Visual C++ 2010 Express.
There are red lines under seemingly random pieces of code, and when I hover my cursor over them it shows errors that seem to make no sense at all. However, when I hit F5 the program compiles and runs successfully.
It's hard to learn C++ like this because I can't quickly distinguish between real syntax errors and 'fake' ones.
The syntax errors:
http://i.stack.imgur.com/O0UbD.png
The program:
#include <iostream>
#include "conio.h"
#include "windows.h"
class test2
{
public:
int i;
};
class testc
{
public:
test2 hi;
};
int main()
{
testc hello;
hello.hi.i = 23;
std::cout << hello.hi.i << "\n";
system("pause");
}
I reinstalled entire Visual Studio, not just Visual C++. Now it works correctly.

Resources