Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void LinkSubjectsToPerson(Person person)
- {
- SqlConnection connection = new SqlConnection();
- using (connection = CONECTIONSTRINGAS )
- {
- connection.Open();
- SqlCommand delete = new SqlCommand();
- delete.Connection = connection;
- delete.CommandType = CommandType.Text;
- delete.CommandText = "Delete FROM PersonSubject where ID = @ID ";
- delete.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 50, "ID"));
- SqlDataAdapter adapter = new SqlDataAdapter("select * from PersonSubject", connection);
- adapter.DeleteCommand = delete;
- DataSet ds = new DataSet();
- adapter.Fill(ds, "PersonSubject");
- for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
- {
- if (person.ID.Equals(ds.Tables[0].Rows[i]["PersonID"]))
- {
- DataRow row = ds.Tables[0].Rows[i];
- row.Delete();
- }
- }
- SqlCommand insert = new SqlCommand();
- insert.Connection = connection;
- insert.CommandType = CommandType.Text;
- insert.CommandText = "Insert into PersonSubject Values (@ID,@PI,@NAME)";
- insert.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 50, "ID"));
- insert.Parameters.Add(new SqlParameter("@PI", SqlDbType.UniqueIdentifier, 50, "PersonID"));
- insert.Parameters.Add(new SqlParameter("@NAME", SqlDbType.VarChar, 50, "Name"));
- foreach (var sub in person.SubjectList)
- {
- ds.Tables[0].Rows.Add(REIKIA IDETI UNIQUE ID, person.ID, sub.Name);
- }
- adapter.Update(ds.Tables[0]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement