Quantcast

Connecting to a Database in a Visual C++ Application

Get the WebProNews Newsletter:
[ Business]

With the ease of working with a database in web based programming languages such as PHP and ASP, database connections in Visual C++ are often overlooked. But C++ offers the versatility and sheer power that web based languages can’t always muster up, and sometimes that comes in very handy.

This article will show you step by step how to develop your first Visual C++ application that communicates with a database. For a simple example, we’ll just grab a table from a database and display it in a grid. So, without further ado, lets get started.

In this example, you will use Microsoft’s ADO (ActiveX Data Object). ADO was created specifically for database communication, it utilizes OLE DB data access technology and has tons of great functionality. Although ADO isn’t the only way to go (for example, the older ODBC technology), it is the newest and most advanced method.

Open a new project in Visual C++Now For The FunStuff Open a new project in Visual C++, choose MFC AppWizard (exe), make it dialog based. In the wizard make sure the check box to include ActiveX controls is checked. Once in the workspace, choose "Project->Add to Project->Components and Controls" from the menu. Then go to the "Registered ActiveX Controls" folder and choose "Microsoft ADO Data Control".

Now there is a new control available in the controls window; add that control to your application. Next, repeat that same process to add the Microsoft DataGrid control. Now you should have both of these controls in your dialog. The ADO control will do all the work with the database, and the DataGrid control will be used to display the information.

Now to establish a connection, in the properties menu of the ADO control, go to the control tab and build a connection string. Here is where you will enter all the information that the ADO will use to communicate with the database. First choose the appropriate provider, then in the connection tab, enter all the connection information that is needed. For a Microsoft OLE DB Provider for SQL Server, your connection string should look like:

Provider=SQLOLEDB.1;Password=YourPassword;Persist Security Info=True; User ID=YourLoginID;Initial Catalog=YourDatabaseName;Data Source=YourSourceIP

Establish a connection string

Develop a program that communicates with a databaseOnce you have your connection completed, go to the RecordSource tab of the properties menu. If you want to retrieve a whole table, choose your command type to be "adCmdTable" and then select which table. If you want to retrieve a more specific set of data, choose "adCmdText" from the command menu and then enter an SQL query in the Command Text box.

Alright, you now have a program that communicates with a database. If you compile and run, the program will retrieve the information you told it to. However, you won’t see any of it because we haven’t specified how to display it yet. Fortunately, using the DataGrid control you added earlier, this task is very simple. Go to the properties menu of the DataGrid control. Go to the ‘all’ tab at the very end, scroll down to DataSource. If you click on the value for DataSource, you should see the ID of the ADO control, choose that.

Now you have a working Visual C++ program that pulls information from a database and displays it on the screen. But in actuality, it does more than just display. If you were to edit the value of one of the table cells, the ADO control will actually update it in the Now you have a working Visual C++ Program!database as well. This is because choosing the DataSource for your DataGrid control binds the grid to the source, allowing communication both ways. Data binding is handy but should be used carefully, for obvious reasons. For those of you used to web based programming for database interaction, where data is retrieved and stored in variables rather than bound, fret not, for there are plenty of ways to do this in Visual C++ as well. In fact, there are many options available when choosing how you want your program to communicate and interact with a database. For the purposes of this article, I chose a fairly simple method (that actually involved writing zero lines of code!) that is good for getting your feet wet. If you want to venture a bit deeper, I suggest going to the class view in your workspace pane and sift around through the C_Recordset class, it will give you a taste of what you can do on your own with a set of records (a Recordset object).

iEntry provides free highly informative newsletters for web developers, IT professionals and small business owners. We deliver 50 million email newsletters per month and have over 4,000,000 unique opt-in subscribers. From our extensive range of email newsletters we can provide you with a selection of newsletters that best meet your interests.

Connecting to a Database in a Visual C++ Application
Comments Off
About IT Writer
iEntry provides free highly informative newsletters for web developers, IT professionals and small business owners. We deliver 50 million email newsletters per month and have over 4,000,000 unique opt-in subscribers. From our extensive range of email newsletters we can provide you with a selection of newsletters that best meet your interests. WebProNews Writer
Top Rated White Papers and Resources

Comments are closed.