public void Save() { SqlConnection conn = DB.Connection(); conn.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO students (name, date) OUTPUT INSERTED.id VALUES (@StudentName, @StudentDate)", conn); SqlParameter nameParameter = new SqlParameter(); nameParameter.ParameterName = "@StudentName"; nameParameter.Value = this.GetName(); SqlParameter dateParameter = new SqlParameter(); dateParameter.ParameterName = "@StudentDate"; dateParameter.Value = this.GetDate(); cmd.Parameters.Add(nameParameter); cmd.Parameters.Add(dateParameter); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { this._id = rdr.GetInt32(0); } if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } }
public void Save() { SqlConnection conn = DB.Connection(); conn.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO courses (name, number) OUTPUT INSERTED.id VALUES (@CourseName, @CourseNumber)", conn); SqlParameter nameParameter = new SqlParameter(); nameParameter.ParameterName = "@CourseName"; nameParameter.Value = this.GetName(); SqlParameter numberParameter = new SqlParameter(); numberParameter.ParameterName = "@CourseNumber"; numberParameter.Value = this.GetNumber(); cmd.Parameters.Add(nameParameter); cmd.Parameters.Add(numberParameter); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { this._id = rdr.GetInt32(0); } if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } }