Creating a Database Project with C# .NET

What we’re going to do now is to use the Employees database we created in the previous section. We’ll add it as a resource to a new project. We can then create a form with buttons that allow us to scroll back and forward through the records in the database. If you didn’t manage to create a database then you can use ours. You’ll find the Employees database in the extra files here on our site: extra files.

Adding a Database to a Project

Create a new project. Call it EmployeesDatabase. To add a database to your new project, have a look at the Solution Explorer on the right. Locate the Properties item:

Description: Solution Explorer, Properties item

Double click on properties to see a new screen appear. Click on the Resources tab on the left of the new screen:

Description: Resources tab

Click on the Add Resource dropdown list at the top and select Add Existing File:

Description: Add a resource to a C# NET project

When you click on Add Existing File, you’ll see a standard Open File dialogue box appear. Navigate to where you saved yourEmployees.mdf database. (If you didn’t create a database, navigate to the one you downloaded from our extra files, here: extra files.) ClickOpen on Open File dialogue box. You will then see this:

Description: Database added as a resource to a C# NET project

The database has been added to the project. You can see that it has been added by looking at the Solution Explorer on the right.

Have a look at the Properties screen again. Now click on the Settings tab, just below Resources. If you have version 2012 of Visual Studio Express, you’ll see this:

Description: The settings tab

A Name, a Type, a Scope and a Value have been filled in for you. The Value is the connection string needed when we connect to thedatabase. The Name will show up in the IntelliSense list a little later. For VS 2012 users, you can now scroll down this page a bit, until you come to the section All Visual Studio Users.

However, 2013 and 2015 users won’t see any entries on the Settings page. You’ll have to fill them out for yourself. To do that, click into the Name box and type EmployeesConnectionString. From the Type dropdown list select Connection String. Change the Scope toApplication. For the Value, click inside the long text box. You’ll see a button to the right:

Description: Addng a connection string setting

Click the button to see this dialogue box:

Description: Connection properties

The default Data Source is for an Access database, so we need to change this. Click the Change button to see a new dialogue box:

Description: Change data source to a SQL Server database

Select the option Microsoft SQL Server Database File, and then click OK to go back to the previous screen.

You now need to browse for your MDF database. So click the Browse button to see an Open File dialogue box. Navigate to your project folder (the EmployeesDatabase project that you have open). Double click your Resources folder to see your database:

Description: Browsing for the Employees database

Click on Open and you’ll get back to the Connection Properties dialogue box:

Description: Testing the database connection

Click Test Connection and it should report success. In Visual Studio Community 2015, you may get an error when you click Test Connection. If you do, click the Advanced button at the bottom of the Connection Properties dialogue box:

Description: Connection Properties dialogue box

From the Advanced Properties box, select LocalDb from the Data Source drop down box:

Description: Advanced properties dialogue box

Then click AttachDbFilename in the area just above Data Source. Click the button on the right of the text area:

Description: Attach DB Filename

Navigate to where your database is saved, which is in the Resources folder of the project you have open. Click the Open button when you have located your database. Click OK on your Advanced Properties dialogue box. Your Database file name area will then read something like this:

C:\Users\Ken\Documents\Visual Studio 2015\Projects\employees_database\employees_database\Resources\Employees.mdf

(Instead of Ken you’ll have the name of your own computer. And instead of employees_database you’ll have the name of your Visual Studio project.)

Click Test Connection and hopefully you’ll see a message saying Test Connection Succeeded. Click OK to return to your Settings page. The Value area when look something like this:

Description: Data Source properties


For Visual Studio Express 2012 and 2013 users, your Value area should look something like this:

Description: Connection string

This is a connection string we can use to connect to the Employees.mdf database. Because we’ve added it as setting, we don’t need to type out the full string above.

All Visual Studio users

To gain access to the table in the database, you need something called a SQL String. This takes the form SELECT * FROM tbl_employees. The * symbol means “All records”. There’s quite a lot you can do with SQL as a language. For our purposes, though, we just want all the data from the database, so we can use a SELECT ALL statement.

Rather than typing the SQL statement in our code, we can add it as a setting. We can then retrieve this settings quite easily.

On the Settings tab, then, click in the Name text box, the one just below EmployeesConnectionString. Type SQL. For the Type, leave it onstring. Change the Scope to Application. For the Value, type the following:

SELECT * FROM tbl_employees

Your Settings page will then look like this:

Description: A SQL statement added as a setting

Save your work and close the Properties page. We can now make a start with coding our database project.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s