Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All,

i am trying or i am learning on how to insert/update data on SQL server.. Currently i have got database on the sql server named 'People' this db has a table called 'Persons'
the table on had an Id column and Persons name column.

What i am trying to do or learn is how i can update a persons name from windows console program. just something hard coded to say Update persons set name 'anything here' where id = 'something' .. basically what i am looking for in this how the console program has been been connected to sql server and also how its quering to the right database.
Any ideas on h ow to do this?

PS. I know sql statements and that.

Thank you for your time.
Posted

You need to use SqlConnection object which is passed connection string which points it to connect to the appropriate database.

Once you got connection object ready, you can create SqlCommand object which contains the actual insert/update/delete/select command to be executed.

You can do it as below:

C#
SqlConnection conn = new SQLConnection("Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;");
SqlCommand cmd = new SqlCommand("update persons set name='abc' where id =2 ");
cmd.Connection = conn;
cmd.ExecuteNonQuery();


To run the above code, you need to include import System.Data.SqlClient reference in your class.
 
Share this answer
 
Comments
1Future 5-Dec-14 6:25am    
Sir you are a genius ... I thank you soo much!
.NET has technology called ADO.NET to handle databases. You can find many examples on how to use ADO.NET here on CodeProject.

Look at this tutorial:
A Beginner's Tutorial for Understanding ADO.NET[^]

Good luck!
 
Share this answer
 
Comments
1Future 5-Dec-14 7:33am    
thank you
Marcin Kozub 5-Dec-14 7:34am    
You're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900