how to insert data in sql with C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jaydeep1991
    New Member
    • Mar 2013
    • 3

    how to insert data in sql with C#

    hi I dont know coding to Insert data in sql with the help of C# language and I want to save data with the click event of save button
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2

    Comment

    • jaydeep1991
      New Member
      • Mar 2013
      • 3

      #3
      I want to use my database by query and it is sql 2008

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Did you read the article I linked to above?

        Comment

        • vijay6
          New Member
          • Mar 2010
          • 158

          #5
          Hey jaydeep1991 take this code as example and change it as per your need. But my suggestion is read the article on the link 'r035198x' posted.

          Code:
          void Save_Click(object sender, EventArgs e)
          {
          try
          {
          SqlConnection con = new SqlConnection("Your Database Connection String");
          SqlCommand cmd = new SqlCommand("Insert into Table_1 (Name) values (@Name)", con);
          cmd.Parameters.AddWithValue("@Name", "Yourname");
          cmd.ExecuteNonQuery();
          
          Console.WriteLine("New row successfully inserted");
          }
          catch (Exception ex)
          {
          Console.WriteLine(ex.ToString());
          }
          finally
          {
          con.Close();
          }
          }

          Comment

          Working...