Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 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.  
  9. namespace zlotaraczka.Model
  10. {
  11.     public class Worker
  12.     {
  13.  
  14.         public  string name ="";
  15.         public string surname ="";
  16.         public void GetWorkers()
  17.         {
  18.            //string imieszuk = "Baran";
  19.            
  20.  
  21.             try
  22.             {
  23.  
  24.                 if (App.connection != null && App.connection.State == ConnectionState.Closed)
  25.                 {
  26.                     App.connection.Open();
  27.                 }
  28.                
  29.                 SqlCommand comm = new SqlCommand("SELECT name FROM worker", App.connection);
  30.                
  31.                     //SqlCommand comm = new SqlCommand("SELECT * from usterka;", App.connection);
  32.                    
  33.  
  34.                     SqlDataReader reader = comm.ExecuteReader();
  35.  
  36.                     if (reader.Read())
  37.                     {
  38.                         name = reader.GetValue(1).ToString();
  39.                         surname = reader.GetValue(2).ToString();
  40.                     }
  41.                     reader.Close();
  42.                
  43.  
  44.                 //connection.Close();
  45.             }
  46.             catch (Exception)
  47.             {
  48.  
  49.                 //connection.Close();
  50.             }
  51.         }
  52.  
  53.  
  54.  
  55.  
  56.         public void AddWorker(int id, string imie, string nazwisko, string rozpoczecie, string zakonczenie)
  57.         {
  58.  
  59.             try
  60.             {
  61.  
  62.                
  63.  
  64.                 SqlDataAdapter adapter = new SqlDataAdapter();
  65.                 string select = "INSERT into worker (id_worker, name, surname) values(@id_worker, @name, @surname)";
  66.  
  67.                 SqlCommand cmd = new SqlCommand(select, App.connection);
  68.  
  69.                 if (App.connection != null && App.connection.State == ConnectionState.Closed)
  70.                 {
  71.                     App.connection.Open();
  72.                 }
  73.                     SqlCommand comm = new SqlCommand(select, App.connection);
  74.                     comm.Parameters.AddWithValue("@id_worker", id);
  75.                     comm.Parameters.AddWithValue("@name", imie);
  76.                     comm.Parameters.AddWithValue("@surname", nazwisko);
  77.                     comm.ExecuteNonQuery();
  78.                     App.connection.Close();
  79.  
  80.                    
  81.                    
  82.                
  83.  
  84.                 //connection.Close();
  85.             }
  86.             catch (Exception)
  87.             {
  88.                 App.connection.Close();
  89.                
  90.                 //connection.Close();
  91.             }
  92.         }
  93.  
  94.  
  95.         public void DeleteWorker(int id_worker)
  96.         {
  97.  
  98.             try
  99.             {
  100.                 SqlCommand comm = new SqlCommand("DELETE FROM worker WHERE id_worker='" + id_worker + "'");
  101.  
  102.                 SqlDataAdapter adapter = new SqlDataAdapter();
  103.  
  104.  
  105.                 App.connection.Open();
  106.                 comm.ExecuteNonQuery();
  107.                 App.connection.Close();
  108.  
  109.  
  110.  
  111.  
  112.                 //connection.Close();
  113.             }
  114.             catch (Exception)
  115.             {
  116.                 App.connection.Close();
  117.  
  118.                 //connection.Close();
  119.             }
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement