Quantcast
Channel: CodesMeshCodesMesh
Viewing all articles
Browse latest Browse all 21

Sql Create Database with Visual C#

$
0
0

To get a complete working Database click the download link at the bottom of this tutorial.

First of all you need to make a new project of type winform application. After doing that you will see a form and on the right side of screen there is a solution explorer window do open it and right click on your project and go to Add and then to Add New Item as shown below.

Now a new window will open from that window select Service-Based-Database as shown below and give it name of your choice.

 

Now press Ctrl+W,L to see your database which you added to your application. Or simply go to view on top toolbar and goto other windows and click database explorer. On the left of your screen you will be able to see your database, expand it to see more of its features and to add an entity click on table and right click and then click Add New Tabel as shown below.

 

 

 

 

 

 

 

 

 

Now you will see a table definition window on middle of your screen. There is a ‘Column Name’    column a ‘Data Type’ column and ‘Allow Nulls’ column. Now the first column would be you ID and name it as your application requires you to default is mostly ID. After that give it its data type which would be int. Third column named ‘Allow Nulls’ asks that whether this column be left null or not. As we know ID cant be null so we untick Allow Null check box. Now there is a window on bottom of screen just below our table defintion named ‘Column Properties’ scroll it down a bit and expand ‘Identity Specification’ and select Yes as shown below to make this column an identity.

 This column is done with now add other attributes of this entity that your application asks be careful when setting allow nulls and selecting datatypes of your attribute.

Now save this table and give it a name a window will pop up asking you to give it name. Give it name that your corresponding entity has. This is just half or you can say quarter of work done. We have just made a database not set its interface with C# and never added queries etc. I wil only discuss here database insertion. For more detailed queries and database download and run code given at the end.

Sql Insert With C# Interface:

In our database like ID we have added 2 other attributes Name and Phone Number. This entity is just a person or an employee with very little detail offcourse you can add more detail to your project as your application demands.

Add two labels,two textboxes and a button from toolbox on to the form. Adjust them on form and adjust form size to look more representable as shown below. Name the text of label1 as Name and label2 as Phone Number and name the text of button as Add.

 

 

 

 

 

 

Here what we want is that when a user enters a Name and Phone Number and hits Add button his entry with this info will be added in Database table. Now the main thing how to do it?

We need to handle the click event of this Add button for going to the coding side of Add button double click it and your cursor will get into its coding view of this button’s click event. On this same page go to top and add


This is necessary name space to access Sql features. After this under the puclic partial class scope add these two lines


Now this string named connection(its value) is taken as follows.

Click on Data toolbar on top and click Add New Data Source then check Data and hit next from that window expand connection string and copy it as shown below and past it between (“”) and assign it to you connection above. After this add backslash ‘\’ to two points where compiler shows red on this string when you paste it down.

After this its simple coding and query making. Now come back to button1 click event and write this code in it


This will now run perfect and your entries will be added to you database. Whew!

For further queries please comment below it will be appreciated. Thanks!

For downloading a detailed database that can insert images also click here.


Viewing all articles
Browse latest Browse all 21

Trending Articles