How to insert values in data base using sql queries…

Now many folks out there are worry that how to insert values in database using SQL queries in C#. Inserting values  while working on oracle would be easy with just a single query but for some people it gets difficult while inserting values from the code in C#.

Now its not too hard as it sounds in fact its very easy. We will first have to add the required namespace for the database we are using and then we start with coding the insert query.

So to add namespace first thing you should know is that your data is on which database platform.

Let say you are working on simple Microsoft platform and your data is on access database then you would be using the OLE DB (Object Linking and Embedding, Database) this is an API designed by Microsoft to work on access database.

STEP 1:

So you will start with

Using System;

Using System.Data.OLeDb;

STEP 2:

after adding the namespace now its time for coding in main.

First you  have to create database connection so it will be

OLeDbConnection conn=new OLeDbConnection();

To set the connection string for the connection you need to have a database link. You can either find this while connection database in visual studio or

you can write this connection string.

this is the connection string for access database 2007.

String Connection=(“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Files\database1.accdb;”)

For all the connection strings you can visit my previous post on this blog.
www.farazahraf.wordpress.com

STEP3:

To connect OleDb connection with the connection string you can use this line of code.

conn.ConnectionString=connection;

where conn is the object name OLeDb connection class created above you can use any name.

while connection is string name you can also use any name for this word.

STEP4:

Now coming to our main topic of inserting values in the database.
so for this you will have to open connection with database by using this command.

conn.Open();

this will open connection with the database.

now we will write the query for database insertion.

OLeDBCommand cmd =new OLeDbCommand(“INSERT INTO TABLE1 (SID,SNAME,GPA) Values (‘”Txt_Id”‘,'”Faraz”‘,”Numaric_GPA”),conn);
cmd.ExecuteNonQuery();

conn.Close();

The command class is used here to insert the command in the data base which will be executed. Query contains a table name which is  “TABLE1” 3 columns name where we need to insert data and values contains the actual data from the program to be inserted into database. “Txt_Id” is the name of the text box that contains the id. you can use any control name here or you can also add values of your own as i have inserted my name directly instead of the text box control name. Now apostrophe  around the first 2 values would be bit surprising but i have used apostrophe to show that these values are string in the database and the GPA is not string so apostrophe is not used with it.

Remember to always close the connection with con.Close(); after you are done with the insertion or any other command.

I hope you would have liked this.
For any queries regarding database please comment . I will be pleased to answer your queries …

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s