Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. public void LinkSubjectsToPerson(Person person)
  2.         {
  3.             SqlConnection connection = new SqlConnection();
  4.             using (connection = CONECTIONSTRINGAS  )
  5.             {
  6.                 connection.Open();
  7.                 SqlCommand delete = new SqlCommand();
  8.                 delete.Connection = connection;
  9.                 delete.CommandType = CommandType.Text;
  10.                 delete.CommandText = "Delete FROM PersonSubject where ID = @ID ";
  11.  
  12.                 delete.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 50, "ID"));
  13.  
  14.                 SqlDataAdapter adapter = new SqlDataAdapter("select * from PersonSubject", connection);
  15.  
  16.                 adapter.DeleteCommand = delete;
  17.  
  18.                 DataSet ds = new DataSet();
  19.  
  20.                 adapter.Fill(ds, "PersonSubject");
  21.  
  22.                 for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
  23.                 {
  24.                     if (person.ID.Equals(ds.Tables[0].Rows[i]["PersonID"]))
  25.                     {
  26.                         DataRow row = ds.Tables[0].Rows[i];
  27.                         row.Delete();
  28.                     }
  29.                 }
  30.  
  31.                 SqlCommand insert = new SqlCommand();
  32.                 insert.Connection = connection;
  33.                 insert.CommandType = CommandType.Text;
  34.                 insert.CommandText = "Insert into PersonSubject Values (@ID,@PI,@NAME)";
  35.  
  36.                 insert.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 50, "ID"));
  37.                 insert.Parameters.Add(new SqlParameter("@PI", SqlDbType.UniqueIdentifier, 50, "PersonID"));
  38.                 insert.Parameters.Add(new SqlParameter("@NAME", SqlDbType.VarChar, 50, "Name"));
  39.  
  40.                 foreach (var sub in person.SubjectList)
  41.                 {
  42.                     ds.Tables[0].Rows.Add(REIKIA IDETI UNIQUE ID, person.ID, sub.Name);
  43.                 }
  44.                 adapter.Update(ds.Tables[0]);
  45.             }
  46.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement