Database Development using Visual C and OLE DB: Establishing the connection. Would you know off-hand if these classes can also connect to SQL Server Express. You can also create engaging, differentiated applications that predict and act using machine learning and SQL Server Machine Learning Services—built into SQL Server and Azure SQL Database—backed by Python and R support for Visual Studio. Explore big data solutions; Try SQL Server Machine Learning Services. Also through out the article I will refer to SQL Server, MSDE is a free version of SQL Server that does not have some of the GUI tools and has a few other limits such as database size. This code will work on both without problem. Making the Love Connection.
This post is aimed at C and C++ developers trying to connect to Azure SQL DB. It is broken down into sections so you can jump to the section that best captures your interest.
Make sure you have the following items:
Azure SQL is built on Microsoft SQL Server and is designed to provide a high-availability, performant, and scalable service. There are many benefits to using SQL Azure over your proprietary database running on premises. With SQL Azure you don't have to install, set up, maintain, or manage your database but only the content and the structure of your database. Typical things that we worry about with databases like fault tolerance and redundancy are all built in.
Azure currently has two options for hosting SQL server workloads: Azure SQL database, database as a service and SQL server on Virtual Machines (VM). We will not get into detail about the differences between these two except that Azure SQL database is your best bet for new cloud-based applications to take advantage of the cost savings and performance optimization that cloud services provide. If you are considering migrating or extending your on-premises applications to the cloud, SQL server on Azure virtual machine might work out better for you. To keep things simple for this article, let's create an Azure SQL database.
Connecting to Azure SQL DB is no different and currently there are two ways to connect to databases: ODBC (Open Database connectivity) and OLE DB (Object Linking and Embedding database). In recent years, Microsoft has aligned with ODBC for native relational data access. ODBC is relatively simple, and also much faster than OLE DB. The only caveat here is that ODBC does use an old C-style API.
See the getting started page to learn how to create a sample database. Alternatively, you can follow this short two-minute video to create an Azure SQL database using the Azure portal.
After your Azure SQL database has been provisioned, you need to carry out the following steps to determine connection information and add your client IP for firewall access.
In Azure portal, go to your Azure SQL database ODBC connection string by using the Show database connection strings listed as a part of the overview section for your database:
Copy the contents of the ODBC (Includes Node.js) [SQL authentication] string. We use this string later to connect from our C++ ODBC command-line interpreter. This string provides details such as the driver, server, and other database connection parameters.
Go to the firewall section for your Database server and add your client IP to the firewall using these steps to make sure we can establish a successful connection:
At this point, you have configured your Azure SQL DB and are ready to connect from your C++ code.
You can easily connect to your Azure SQL DB using ODBC on Windows using this sample that builds with Visual Studio. The sample implements an ODBC command-line interpreter that can be used to connect to our Azure SQL DB. This sample takes either a Database source name file (DSN) file as a command-line argument or the verbose connection string that we copied earlier from the Azure portal. Bring up the property page for this project and paste the connection string as a command argument as shown here:
Make sure you provide the right authentication details for your database as a part of that database connection string.
Launch the application to build it. You should see the following window validating a successful connection. You can even run some basic SQL commands like create table to validate your database connectivity:
Alternatively, you could create a DSN file using the wizard that is launched when no command arguments are provided. We recommend that you try this option as well. You can use this DSN file for automation and protecting your authentication settings:
Congratulations! You have now successfully connected to Azure SQL using C++ and ODBC on Windows. You can continue reading to do the same for Linux platform as well.
In case you haven't heard the news yet, Visual Studio now allows you to develop C++ Linux application as well. You can read about this new scenario in the Visual C++ for Linux Development blog. To build for Linux, you need a remote machine where your Linux distro is running. If you don't have one available, you can set one up quickly using Linux Azure Virtual machines.
For this tutorial, let us assume that you have an Ubuntu 16.04 Linux distribution set up. The steps here should also apply to Ubuntu 15.10, Red Hat 6, and Red Hat 7.
The following steps install the libraries needed for SQL and ODBC for your distro:
Drumazon vst plugin download. Launch Visual Studio. Under Tools -> Options -> Cross Platform -> Connection Manager, add a connection to your Linux box:
After connection over SSH is established, create an Empty project (Linux) template:
You can then add a new C source file and replace it with this content. Using the ODBC APIs SQLAllocHandle, SQLSetConnectAttr, and SQLDriverConnect, you should be able to initialize and establish a connection to your database.Like with the Windows ODBC sample, you need to replace the SQLDriverConnect call with the details from your database connection string parameters copied from the Azure portal previously.
The last thing to do before compiling is to add odbc as a library dependency:
To launch your application, bring up the Linux Console from the Debug menu:
If your connection was successful, you should now see the current database name printed in the Linux Console:
Congratulations! You have successfully completed the tutorial and can now connect to your Azure SQL DB from C++ on Windows and Linux platforms.
You can find the GetStarted solution that contains all the samples in this article at GitHub: