What's my mistake?

using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\admin\Мои документы\Visual Studio 2010\Projects\BLR\BLR\bin\Debug\Database1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")) { conn.Open(); using (SqlCommand com = conn.CreateCommand()) { com.CommandText = "INSERT INTO TableStudents VALUES (@Name, @Education)"; com.Parameters.AddWithValue("@Name", 1); com.Parameters.AddWithValue("@Education", 2); //com.CommandText = string.Format("INSERT INTO TableStudents (Name, Education)"); //com.Parameters.Add("Name", SqlDbType.Int).Value = 1; //com.Parameters.Add("Education", SqlDbType.Int).Value = 2; temp = com.ExecuteNonQuery(); } conn.Close(); return temp; } 

with the SELECT command, this construct works, with INSERT in no way ... what to do, please tell me

  • one
    INSERT INTO [TableStudents] (Name, Education) VALUES (@Name, @Education) try - Dmitry Chistik
  • Since you can sometimes insert, but the number of columns and column types must match. - nick_n_a

1 answer 1

You do not prescribe columns, but only values. See the answer to the question here .

  • I do so string query = "INSERT INTO TableStudents (Name,Education) VALUES (@Name, @Education)"; SqlCommand comm = new SqlCommand(query, conn); comm.Parameters.Add("@Name", 1); comm.Parameters.Add("@Education", 2); temp = comm.ExecuteNonQuery(); string query = "INSERT INTO TableStudents (Name,Education) VALUES (@Name, @Education)"; SqlCommand comm = new SqlCommand(query, conn); comm.Parameters.Add("@Name", 1); comm.Parameters.Add("@Education", 2); temp = comm.ExecuteNonQuery(); No result - Anton Burak