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. Inserting data into SQL Server database

Inserting data into SQL Server database

Scheduled Pinned Locked Moved C#
databasecsharpsql-serversysadmin
4 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.
  • C Offline
    C Offline
    cshivaprasad
    wrote on last edited by
    #1

    I need to develop a small application, from which i need to insert data into the SQL Server. I am using ADO.Net. pls give me some sample code if possible. or refer me to a suitable article. Thank U.

    F K 2 Replies Last reply
    0
    • C cshivaprasad

      I need to develop a small application, from which i need to insert data into the SQL Server. I am using ADO.Net. pls give me some sample code if possible. or refer me to a suitable article. Thank U.

      F Offline
      F Offline
      freshonlineMax
      wrote on last edited by
      #2

      Hello dear friend Please notice below codes We have a table with 3 fields as : (field1:Int, field2:NVarChar, field3:Bit) At first of your form add these namespaces: using System.Data; using System.Data.SqlClient; Now add these sample lines to your Button_Click() event : string strInsert="INSERT INTO table (field1,field2,field3) VALIUES (@field1,@field2,@field3)"; if (SqlConnection1.state.toString() != "Open") SqlConnection1.Open(); sqlCommand cmd = new SqlCommand(strInsert,sqlConnection1); cmd.Parameters.Add(new SqlParameter("@field1",SqlDbType.Int,4,"field1")); cmd.Parameters["@field1"].Value = 1000; cmd.Parameters.Add(new SqlParameter("@field2",SqlDbType.NVarChar,10,"field2")); cmd.Parameters["@field2"].Value = "California"; cmd.Parameters.Add(new SqlParameter("@field3",SqlDbType.Bit,1,"field3")); cmd.Parameters["@field3"].Value = true; cmd.ExecuteNonQuery(); Don't remember to define SqlConnection if you have any problem mail me Bye Bye -- modified at 2:13 Monday 8th May, 2006

      C 1 Reply Last reply
      0
      • F freshonlineMax

        Hello dear friend Please notice below codes We have a table with 3 fields as : (field1:Int, field2:NVarChar, field3:Bit) At first of your form add these namespaces: using System.Data; using System.Data.SqlClient; Now add these sample lines to your Button_Click() event : string strInsert="INSERT INTO table (field1,field2,field3) VALIUES (@field1,@field2,@field3)"; if (SqlConnection1.state.toString() != "Open") SqlConnection1.Open(); sqlCommand cmd = new SqlCommand(strInsert,sqlConnection1); cmd.Parameters.Add(new SqlParameter("@field1",SqlDbType.Int,4,"field1")); cmd.Parameters["@field1"].Value = 1000; cmd.Parameters.Add(new SqlParameter("@field2",SqlDbType.NVarChar,10,"field2")); cmd.Parameters["@field2"].Value = "California"; cmd.Parameters.Add(new SqlParameter("@field3",SqlDbType.Bit,1,"field3")); cmd.Parameters["@field3"].Value = true; cmd.ExecuteNonQuery(); Don't remember to define SqlConnection if you have any problem mail me Bye Bye -- modified at 2:13 Monday 8th May, 2006

        C Offline
        C Offline
        cshivaprasad
        wrote on last edited by
        #3

        Really i am very thankfull 2 U.

        1 Reply Last reply
        0
        • C cshivaprasad

          I need to develop a small application, from which i need to insert data into the SQL Server. I am using ADO.Net. pls give me some sample code if possible. or refer me to a suitable article. Thank U.

          K Offline
          K Offline
          kalyanhere
          wrote on last edited by
          #4

          for vb.net and asp.net imports system.data.sqlclient dim con as new sqlconnection dim cmd as new sqlcommand con.connectionstring = "Provider= the provider u use;user id= ;password= ; data source= sql server name ;initial catalog= database name " cmd.connection = con con.open() cmd.commandtext = "insert into tablename values('" & tid.text & "')" cmd.executenonquery() con.close() if u have any problem with this revert back vil help u out regards kalyan

          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