Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to store data from c# into sql server data base

how to store data from c# into sql server data base

Scheduled Pinned Locked Moved C#
databasecsharpsql-serversysadmintutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rao raja
    wrote on last edited by
    #1

    i need to know that if many values are entered into a c# form, then i want to add all those in the dataBase.so what will be the code lines.plz reply someone

    C V 2 Replies Last reply
    0
    • R rao raja

      i need to know that if many values are entered into a c# form, then i want to add all those in the dataBase.so what will be the code lines.plz reply someone

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Create a SqlCommand to INSERT the data in the database.

      SqlCommand cmd = new SqlCommand();
      cmd.Connection = aConnection;
      cmd.CommandText = "INSERT TableName(Column1, Column2, Column3) "+
      "VALUES (@Value1, @Value2, Value3";
      cmd.Parameters.Add("@Value1", theFirstValue);
      cmd.Parameters.Add("@Value2", theSecondValue);
      cmd.Parameters.Add("@Value3", theThirdValue);
      cmd.ExecuteNonQuery();

      You will have to replace theFirstValue, theSecondValue and theThirdValue with appropriate values. Also, you will have to restructure the INSERT statement to match your database. I've also assumed that you have a connection (aConnection) to the database.


      Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

      1 Reply Last reply
      0
      • R rao raja

        i need to know that if many values are entered into a c# form, then i want to add all those in the dataBase.so what will be the code lines.plz reply someone

        V Offline
        V Offline
        Vinay Dornala
        wrote on last edited by
        #3

        hi First write StoredProcedure like for Insert Create StoredProc nameofStoredProc ( @firstname varchar(20), @lastname varchar(20)) as insert into tablename ( firstname, lastname ) values ( @fisrtname, @lastname ) then start writing ADO.net code SqlConnection con=new SqlConnection("userid=""; password="";DataSource=""); con.Open(); SqlCommand cmd=new SqlCommand("nameofStroredProc",con); cmd.CommandType=CommandType.StoredProcedure; cmd.Parameter.Add(new SqlParameter("@firstname",SqlDbType.varchar); cmd.Parameter.Add(new SqlParameter("@lastname",SqlDbType.varchar); cmd.parameter[0].value= txtFirstName.text;//textbox value cmd.Parameter[1].value=txtlastName.Text;//textbox value cmd.ExecuteNonQuery(); con.Close(); WINNING IS NOT OUR DREAM, IT'S A HABIT HAVE A NICE DAY

        C 1 Reply Last reply
        0
        • V Vinay Dornala

          hi First write StoredProcedure like for Insert Create StoredProc nameofStoredProc ( @firstname varchar(20), @lastname varchar(20)) as insert into tablename ( firstname, lastname ) values ( @fisrtname, @lastname ) then start writing ADO.net code SqlConnection con=new SqlConnection("userid=""; password="";DataSource=""); con.Open(); SqlCommand cmd=new SqlCommand("nameofStroredProc",con); cmd.CommandType=CommandType.StoredProcedure; cmd.Parameter.Add(new SqlParameter("@firstname",SqlDbType.varchar); cmd.Parameter.Add(new SqlParameter("@lastname",SqlDbType.varchar); cmd.parameter[0].value= txtFirstName.text;//textbox value cmd.Parameter[1].value=txtlastName.Text;//textbox value cmd.ExecuteNonQuery(); con.Close(); WINNING IS NOT OUR DREAM, IT'S A HABIT HAVE A NICE DAY

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          dornala wrote:

          SqlConnection con=new SqlConnection("userid=""; password="";DataSource="");

          A SqlConnection needs both a server and database name. Also, It is better to use a trusted connection than supply a user name and password in plain text.


          Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

          V 1 Reply Last reply
          0
          • C Colin Angus Mackay

            dornala wrote:

            SqlConnection con=new SqlConnection("userid=""; password="";DataSource="");

            A SqlConnection needs both a server and database name. Also, It is better to use a trusted connection than supply a user name and password in plain text.


            Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

            V Offline
            V Offline
            Vinay Dornala
            wrote on last edited by
            #5

            hi Just Iam specifing a simple connection for an example. It is good practice to give entire connection object.

            C 1 Reply Last reply
            0
            • V Vinay Dornala

              hi Just Iam specifing a simple connection for an example. It is good practice to give entire connection object.

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              dornala wrote:

              It is good practice to give entire connection object.

              I don't understand what you mean. What is a simple connection? The only way to connect to a database with ADO.NET is with a connection object.


              Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups