How to insert data in DATABASE?
In this article I plan to demonstrate how to insert data from a SQL Server . This code should work on both SQL Server , I am using SQL 2005 & Visual Studio 2008, This code should work with both C# windows applications and C# web applications.
Background:
Part of my current project required me too store information from a database. I decided to use C# as my target language since I am currently reading Apress.Illustrated.C.Sharp.2008.Feb.2008, which by the way is a must have book.
Working:
1)First open Visual Studio 2008:
2)Creat C# NEW PROJECT
3)Select C# WINDOWS FORM APPLICATION
4)Creat simple form which have 2 textboxes (textbox1 & textbox2) ,2 lable(Name & Age)and 1 button (INSERT)
5)go to VIEW then SERVER EXPLORER 7)go to SERVER EXPLORER ,Database(MyDataBase.dbo) have been created then open the Database explorer then go to TABLE,right click TABLE and ADD NEW TABLE
9)and save Table NAme is MyTable
10)then go to DATA and Add New DataSource
11)Click Next to NEW Connection
11)choose your Data Source ,we use Microsoft SQL SERVER then Continue
12)*Here you have to connect to the selected Data source or click change to different data source
*Server Name is ".\sqlexpress"
*Select your Database Name ,our database name is MyDataBase
*You can TEST CONNECTION ,it will be success,then OK
13)Next Step is to storing Connection String in your Application,VS 2008 automatically generate the connection string
*and our project coneection string is MyDataBaseConnectionString
14)Choose your DataObject
which Databse you want to choose?
check the Tables which is youu DataObject.. and then FINISH
15)the next Step is go to DATA then DATASOURCE
in DataSource here is the MyTable DataGrid where we show the data
when we Drag the MyTable DataGrid to the form it will create 5 thing automatically
*myDataBaseDataSet
*myTableBindingSource
*myTableTableAdapter
*tableAdapterManager
*myTableBindingNavigator
you can delete the BINDING NAVIGATOR BAR.it is not necessary
16)I have design Form ,create DataBase and Connect DatBase ,now just left coding..
Double click of INSERT button ,VS 2008 will creat automaticallyClick Event
then this is code which is use in Button Click event
MyDataBaseDataSet.MyTableRow mytable = myDataBaseDataSet.MyTable.NewMyTableRow();
//For NAME
mytable.Name = textBox1.Text;
//For Age
mytable.Age =Convert.ToInt32( textBox2.Text);
myDataBaseDataSet.MyTable.Rows.Add(mytable);
try
{
this.Validate();
this.myTableBindingSource.EndEdit();
this.myTableTableAdapter.Update(mytable);
}
catch(Exception ex)
{
MessageBox.Show(""+ex.Message);
}
after this coding Compile and Run the project..and insert the data...
this is data show in Grid
and you want to show your Data in DATABSE then go to EXPLORER SERVER and right click MyTablet, then Show Table Data
This is Databse SQL SERVER
IF any queries about this project and other u can ask me on comments..
(next article will be publish with in 1 week)
Download project:http://www.4shared.com/file/108097511/86163c0/DatabaseConnectivity.html
BE a part of My Network:
Thanks
& Regards,
Rizwan Ahmed
Software Engineer And Microsoft Dynamics Technical Consultant
Cell Phone:+92 332 3588699
GTalk|LinkedIn|Twitter|Facebook|Blog|Skype
kudos for your effort.
ReplyDeleteNice work
ReplyDeletenice work dude...
ReplyDeletenext article publish rapidly plz..
Thanks. Good Work
ReplyDeletenIce wOrk
ReplyDeletei like it
ReplyDeletegr8 man
ReplyDeletenice work.............by Cute Sonu:)
ReplyDeletegood work
ReplyDeleteDhinkaa Chikaa Dhinkaa Chikaa Dhinka Chika Dhinka Chika ....
ReplyDeleteO O Oh!
This is a great introduction. However, the link doesn't seem to work anymore. Any chance you can repost the sample?
ReplyDeletevery nice thanks a lot
ReplyDeleteOK i will post again
ReplyDelete