Integrating SQL database within the .NET project
Many of you have had develope applications that use the database for a proper functioning, the main problem comes when you consider to eploy you application at any other machine. The Database is not available at the machine, so your application gives error. To remove this error, there are two ways you create the database at all the machines using code or you integrate the database within the project. The first option is tough job as you will need to execute all the SQL commands at all the machines where it will be installed and will have to take care of it in extra. But the second approach takes care of all that itself. So its very easy to use the second approach. I am writing below about the second approach here.
Step 1> Open your Application. Goto Solution Explorer Window. Right Clcik on your application and choose add new item from the menu.
Step 2> From the dialog box that appears, choose SQL Database and write the name of your Database in the name field and Click OK.
Step 3> In the Dialog box that appears, Click Finish. You will be able to see the Database in teh solution explorer Window.
Step 4> Now to add tables, view etc. to you Database, Open Server Explorer Window, under Database Connections you will fin your Database, Double Click it, Right click on Tables, choose Add new Table from menu, A window opens write Definitions for the table an save it by a name.
Step 5> In the same manner you will be able to create View, Store Procedures, Functions etc..
Step 6> Now how to use the database in code, All the things shall remain same as in any otehr case, only teh Connection String would change. Connection String would be – “Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\YourDatabaseName.mdf;Integrated Security=True;User Instance=True”.
Step 7> Now use the SQL Command, SqlDataReader, SQL Connection classes for any operation to be performed. You can execute any comman of SQL using the ExecuteNonQuery function on SqlCommand Class.
So thats it. Now when you create the setup, just include the ContentFiles in your Appliaction Folder along with the Project Output.
If you face any problem in this. Comment me here. I will be quick to respond.
Click here if you need a Website
when i go to add a new database to my project, i don’t have SQL Database, but i’ve
1 – Local Database (.sdf)
2 – Service Based Database (.mdf)
.Other items
.
.
In this case what i’ve to select and what is the diff. between this two.
I’m using VS Pro 2008.
In VS 2008 we have two options for ading database.
SDF
MDF
If you just want to create tables in the Database go for SDF that is Local Database File.
If you want to Create TAbles, Views, Stored Procedures, Functions. Go for the MDF that is Service Based database.
You can chat withe me live by Clicking The Live Chat Support Button also.
thanks for your reply!