Skip to main content

Beginners guide to accessing SQL Server through C#


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

in SERVER EXPLORER panel ,right click of DATA CONNECTIONS and CREATE NEW SQL SERVER CONNECTION





















6)Enter the information to connect the Sql Server,thenspecify the name of database to create

and SQL SERVER name is ".\sqlexpress" and my Database Name is MYDATABASE

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
8)here is SQL server TABLE i have define 2 column (Name whoseDataType is text & Age DataType is int)

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

Comments

Post a Comment