Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9.  
  10. namespace TeachingPlan
  11. {
  12.     class SqlExecutor
  13.     {
  14.         public static void Insert(DataTable table, DataGridViewRow row)
  15.         {
  16.             try
  17.             {
  18.                 string subjectName = (string)row.Cells[0].Value;
  19.                 int hours = (int)row.Cells[8].Value;
  20.                 string classType = (string)row.Cells[10].Value;
  21.                 int ects = (int)row.Cells[11].Value;
  22.  
  23.                 String insertSubjectQuery = Properties.Resources.DodajPrzedmiot;
  24.  
  25.                 using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.teachingPlanConnectionString))
  26.                 {
  27.                     SqlCommand insertSubjectCommand = new SqlCommand(insertSubjectQuery, connection);
  28.  
  29.                     int classTypeId = CheckForClassType(classType);
  30.                     if (classTypeId < 0)
  31.                     {
  32.                         MessageBox.Show("Wprowadzono nieprawidłową wartosć w kolumnie Tryb_zajec");
  33.                         return;
  34.                     }
  35.  
  36.                     insertSubjectCommand.Parameters.Add(new SqlParameter("@nazwa", subjectName));
  37.                     insertSubjectCommand.Parameters.Add(new SqlParameter("@id_rodzaj_zajec", classTypeId));
  38.                     insertSubjectCommand.Parameters.Add(new SqlParameter("@ects", ects));
  39.                     insertSubjectCommand.Parameters.Add(new SqlParameter("@godziny", hours));
  40.  
  41.                     using (SqlDataAdapter dataAdapter = new SqlDataAdapter(insertSubjectCommand))
  42.                     {
  43.                         dataAdapter.InsertCommand = insertSubjectCommand;
  44.                         dataAdapter.Update(table);
  45.  
  46.                         MessageBox.Show("Aktualizacja bazy powiodła się.");
  47.                     }
  48.                 }
  49.             }
  50.             catch (InvalidCastException)
  51.             {
  52.                 MessageBox.Show("Pozostawiono puste pole(a). Aby dodać wpis uzupełnij dane.");
  53.             }
  54.         }
  55.  
  56.         private static int CheckForClassType(string classType)
  57.         {
  58.             switch (classType)
  59.             {
  60.                 case "Wyklad":
  61.                     return 1;
  62.                 case "Cwiczenia":
  63.                     return 2;
  64.                 case "Laboratoria":
  65.                     return 3;
  66.                 case "Projekt":
  67.                     return 4;
  68.                 default:
  69.                     return -1;
  70.             }
  71.         }
  72.  
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement