Thursday, March 1, 2018
Creating a Database Project with C NET
Creating a Database Project with C NET
Creating a Database Project with C# .NET
What were going to do now is to use the Employees database we created in the previous section. Well 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 didnt manage to create a database then you can use ours. Youll 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:
Double click on properties to see a new screen appear. Click on the Resources tab on the left of the new screen:
Click on the Add Resource dropdown list at the top and select Add Existing File:
When you click on Add Existing File, youll see a standard Open File dialogue box appear. Navigate to where you saved your Employees.mdf database. (If you didnt create a database, navigate to the one you downloaded from our extra files, here: extra files.) Click Open on Open File dialogue box.
When you click Open, youll see a Data Source Configuration screen appear, if you have Visual Studio Express 2010 or 2010. (2013 users wont see this screen.) Select Dataset and then click the Next button at the bottom. You should then see a Choose Your Database Objects screen. Select Tables, and type a new DataSet name at the bottom. Type ds_employees:
Click on Finish to return to the Resources screen. All VS users will then see this:
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 2010 or 2012 of Visual Studio Express, youll see this:
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 the database. The Name will show up in the IntelliSense list a little later. For VS 2010 and 2012 users, you can now scroll down this page a bit, until you come to the section All Visual Studio Users.
However, 2013 users wont see any entries on the Settings page. Youll 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 to Application. For the Value, click inside the long text box. Youll see a button to the right:
Click the button to see this dialogue box:
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:
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:
Click on Open and youll get back to the Connection Properties dialogue box:
Click OK (or click Test Connection first, if you want), and youll get back to the Settings screen. Take a look at your Value text box:
This is a connection string we can use to connect to the Employees.mdf database. Because weve added it as setting, we dont 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". Theres 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 belowEmployeesConnectionString. Type SQL. For the Type, leave it on string. Change the Scope toApplication. For the Value, type the following:
SELECT * FROM tbl_employees
Your Settings page will then look like this:
Save your work and close the Properties page. We can now make a start with coding our database project.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.