How to do an SQL query in C++ to define a variable - c++

I've been searching all day to figure out how to do an SQL query in c++ and then store the result as a variable.
I am using Visual Studio 2012, connected to the SQL database, and ran the following query successfully inside Visual Studio:
SELECT Beta FROM Equity WHERE Ticker = 'AAPL'
I want to be able to run that query in my c++ program and then store the result in a variable so that it can be used in a calculation. How can this be done?
Thanks for the help

You will need to get an SQL library, depending on that you will need to write some set-up code and execute the query.

BY C++ I'm sure you mean a native C++ application, not a managed C++ one. You need to use one of libraries that connect to SQL Server. Choices are:
ODBC, see ODBC database sample
OLE DB, see Sample OLE DB Consumer Application
ADO, a programability extension on top of OLE DB. See ADO Code Examples in Visual C++
My recommendation would be to use ODBC. You have a quite a lot to read and learn before you can actually get an example running with the query you asked. Good luck.

Related

Connect an SQL to c++ project in visual studio 2015

I'm writting a tic-tac-toe game in c++ and I want to create a hight scores view. The problem is I'm new to DB and now just theory of SQL.
So, how can I add a simple SQL table to my c++ project?
(id INTEGER PRIMARY KEY, name TEXT, hightScore INTEGER)
Looks like just adding to project SQL script file and creating this table not gonna work. How to connect it with my cpp files? Where and how I'll be able to make an INSERT after each new game was played?
Thanks in advance!
To have a simple table you'll need to have a database to set the table up within. The library you will use to communicate with the database is going to depend on which database you are running.
Setting up the needed libraries usually means installing them and updating your linker. This will require some research and effort on your part to make it work, depending on what your particular configuration is.
some ideas:
(for mysql there is mysql/mysql.h which is a free library. )
How to make #include <mysql.h> work?
(for other SQL databases (not free))
http://www.sqlapi.com/
(possible free alternative to sqlapi )
Free alternative to SQLAPI++?
Hopefully that will get you started in the right direction! Good luck.
I have researched about c++ database connectivity and i could find following ways to connect to SQL server database from c++ project.
Using Microsoft's ODBC connection method. Which is a little bit complicate method and requires thorough research to use further.
Ref: https://www.techhowtos.com/programming/how-to-connect-to-sql-server-from-visual-c-plus-plus/
Using SQLAP++ Library which is very convenient way to connect to a databse . It Requires to purchase one time license to use commercially.
Using CPPCMS project's CppDB library.

SQL Statement Capture from within Visual Studio 2012 or MS SQL 2012

I'm using Visual Studio 2012 and MS SQL Server 2012 for a C++ application.
I understand there's a way to capture SQL statements from both programs (some sort of trace profiler) for individual tables but I've run across very few tutorials on how to do perform this, and unfortunately they tend to be a bit vague on specific steps- almost as if they assume you should already know how to do this. Is there a way to perform this capture/trace for statements that span multiple tables? If so, how?
Unfortunately, the code I inherited is 20K lines of spaghetti code so it makes sense to capture the resulting statement before it's sent to the DB rather than spending a few weeks stepping through it.
SQL Server Profiler from the Tools menu in SSMS. You can filter to capture the calls from certain logins and depending on the connection string used you can also filter by application name. For more details msdn.microsoft.com/en-us/library/ms173799(v=sql.110).aspx
To make it easier to profile just the calls you are looking for, if you add: Application Name=MyAppName; to the connection string your app uses you can then filter in SQL Profiler by Application Name Like MyAppName.

Making SQL query from C++

I have connected to an MS Access database from my project. Now I want to make a query, for example to take out data from third and fourth column.
How can I write SQL query in C++/CPP, using visual studio 2008?
If you're using a framework like MFC, Qt, or wxWidgets, you probably want to use the database connection classes they already include.
If you're not using one of those, you probably want to look into another library specifically for the purpose, such as DTL or SOCI.
You can obviously write your own code to talk to the database via something like OLE DB or ODBC, but they both involve quite a lot of boiler-plate, so a library helps out a lot.

Getting started with SQL express from a c++ console application

Right now I am writing a little c++ console program using VS2010 Express. The next thing I want to do is to access a SQL database from within this program.
I've downloaded SQL server 2008 express. I've managed to set up a little db using the db gui.
My question now is, how do I access this db from within my program. Which header files do I need, how do I connect? About the basic tempering with the db itself I have found many tutorials, but this tiny bit that closes the gap between a program and the connection to the db drives me nuts... If any one has a nice tip to a tutorial or book, please let me know.
I've also tried to start a new project in VS, and was hoping to find some kind of setup wizard for a "sql project" that would get me one the way but did not find such a wiz...
Microsoft doesn't provide much for SQL connectivity from C++ console applications. There are a number of libraries such as SOCI and DTL that can help though.

Connecting to a SQL Server Compact Edition (.sdf) from an MFC application

I'm building an MFC app in Visual Studio 2008 which classifies textures and I need some sort of lightweight database to hold the characteristics (just some doubles and strings) which can be:
Carried around with the app on different computers
Be able to perform queries on it from the app (searches , updates ,inserts ,etc)
Currently I'm looking into SQL Server Compact Edition because it was very easy to create from Visual Studio (I also need only one table). But I;m having a hard time connecting and updating the database from C++.
This is what I've found on MSDN regarding C++ and SQLCE:
public:
void createSqlCeConnection(){
SqlCeConnection* myConnection = new SqlCeConnection();
myConnection->ConnectionString = "DataSource = blabla.sdf";
MessageBox::Show(String::Format( S"Connection State: {0}", __box(myConnection->State)));
}
Unfortunately my experience with .NET apps is pretty limited.
Hopefully you bright minds could tell me if I'm on the right path and what links and includes should I add for this to work with an C++ MFC projects.
For C++ applications, you're going to want to use the OLE DB Provider for SQL CE. For example, take a look here for a code snippet on initializing a Session (you might have to explicitly click the C++ tab in the Examples section).