Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I can read from SQL database using the following code, now I want to learn how to write into a database.


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ReportOrders
{
    class Report
    {
        static void Main(string[] args)
        {
            SqlConnection dataConn = new SqlConnection();

            try
            {
                dataConn.ConnectionString =
                    "Integrated Security=true;Initial Catalog=AdventureWorks;" +
                    "Data Source=.";
                dataConn.Open();
                Console.Write("Please enter a Manager ID ");
                string EmployeeID = Console.ReadLine();
                SqlCommand dataComm = new SqlCommand();
                dataComm.Connection = dataConn;
                dataComm.CommandText =
                    "SELECT EmployeeID, ContactID,Title, BirthDate " +
                    "FROM HumanResources.Employee WHERE ManagerID='" + EmployeeID + "'";
                Console.WriteLine("About to execute: {0}\n\n", dataComm.CommandText);
                SqlDataReader dataReader = dataComm.ExecuteReader();
                while (dataReader.Read())
                {
                    int ContactID = dataReader.GetInt32(0);
                    string Title = dataReader.GetString(2);
                    int BirthDate = dataReader.GetInt32(1);                   
                    Console.WriteLine(
                        "EmployeeID: {0}\nID: {1}\n{2}\n{3}\n", EmployeeID, ContactID, Title, BirthDate);
                }
                dataReader.Close();
            }
            catch (SqlException e)
            {
                Console.WriteLine("Error accessing the database: {0}", e.Message);

            }
            finally
            {
                dataConn.Close();
            }
        }
    }
}
Posted
Updated 9-Dec-12 16:54pm
v2

1 solution

For writing back in DB, you need to udpate DB, If you're not aware of this kinda example, here you go....
http://www.youtube.com/watch?v=yh0A9rGXp1M[^]
http://www.daniweb.com/software-development/csharp/code/290540/sql-databases-using-c-connecting-and-updating[^]
 
Share this answer
 
Comments
Shehzaada Salim 9-Dec-12 22:58pm    
Thanks Krunal.. will check if it works :)
[no name] 9-Dec-12 23:00pm    
Yes it does.. Good Luck :)
Shehzaada Salim 9-Dec-12 23:04pm    
Krunal..this is really helpful, but what I was looking for is, how to add employee details to this existing table Employe. Can I have a quick fix solution. Please
[no name] 9-Dec-12 23:07pm    
Yes, you can fire the UPDATE query as per your requirements on Employee Table...
Shehzaada Salim 9-Dec-12 23:13pm    
But I want to do the same through coding.. I hope you understand my problem here.. :(

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