4
Answers

How can I insert data into a table using c#?

Hil Isabelle

Hil Isabelle

4y
1.2k
1
Hello everyone!
 
I've got a simple table 'Table' in a simple database 'Database1' with 2 rows (Id,Chemin) and I'm trying to insert data into my table but the opening of the connection to the database doesn't work (con.Open()). Can someone tell me why and help me please ? The code is below :
  1. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Database1;Integrated Security=True");  
  2. SqlCommand cmd = new SqlCommand("sp_insert", con);  
  3. cmd.CommandType = CommandType.StoredProcedure;  
  4. cmd.Parameters.AddWithValue("@Chemin", textBox1.Text); //cmd.Parameters.AddWithValue("@email", textBox2.Text);  
  5. con.Open();  
  6. int i = cmd.ExecuteNonQuery();  
  7. con.Close();  
Answers (4)