Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class frmUpdates
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents grdDataGrid As System.Windows.Forms.DataGrid
    Friend WithEvents btnLoad As System.Windows.Forms.Button
    Friend WithEvents btnUpdate As System.Windows.Forms.Button
    Friend WithEvents SqlDataAdapter1 As System.Data.SqlClient.SqlDataAdapter
    Friend WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
    Friend WithEvents SqlInsertCommand1 As System.Data.SqlClient.SqlCommand
    Friend WithEvents SqlUpdateCommand1 As System.Data.SqlClient.SqlCommand
    Friend WithEvents SqlDeleteCommand1 As System.Data.SqlClient.SqlCommand
    Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.grdDataGrid = New System.Windows.Forms.DataGrid
        Me.btnLoad = New System.Windows.Forms.Button
        Me.btnUpdate = New System.Windows.Forms.Button
        Me.SqlDataAdapter1 = New System.Data.SqlClient.SqlDataAdapter
        Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlUpdateCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlDeleteCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
        CType(Me.grdDataGrid, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'grdDataGrid
        '
        Me.grdDataGrid.DataMember = ""
        Me.grdDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.grdDataGrid.Location = New System.Drawing.Point(136, 16)
        Me.grdDataGrid.Name = "grdDataGrid"
        Me.grdDataGrid.Size = New System.Drawing.Size(512, 368)
        Me.grdDataGrid.TabIndex = 0
        '
        'btnLoad
        '
        Me.btnLoad.Location = New System.Drawing.Point(16, 16)
        Me.btnLoad.Name = "btnLoad"
        Me.btnLoad.Size = New System.Drawing.Size(104, 24)
        Me.btnLoad.TabIndex = 1
        Me.btnLoad.Text = "Wczytaj"
        '
        'btnUpdate
        '
        Me.btnUpdate.Location = New System.Drawing.Point(16, 48)
        Me.btnUpdate.Name = "btnUpdate"
        Me.btnUpdate.Size = New System.Drawing.Size(104, 24)
        Me.btnUpdate.TabIndex = 2
        Me.btnUpdate.Text = "Aktualizuj"
        '
        'SqlDataAdapter1
        '
        Me.SqlDataAdapter1.DeleteCommand = Me.SqlDeleteCommand1
        Me.SqlDataAdapter1.InsertCommand = Me.SqlInsertCommand1
        Me.SqlDataAdapter1.SelectCommand = Me.SqlSelectCommand1
        Me.SqlDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "tblPracownicy", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("Imie", "Imie"), New System.Data.Common.DataColumnMapping("Nazwisko", "Nazwisko"), New System.Data.Common.DataColumnMapping("IDDzialu", "IDDzialu"), New System.Data.Common.DataColumnMapping("Pensja", "Pensja"), New System.Data.Common.DataColumnMapping("ID", "ID")})})
        Me.SqlDataAdapter1.UpdateCommand = Me.SqlUpdateCommand1
        '
        'SqlSelectCommand1
        '
        Me.SqlSelectCommand1.CommandText = "SELECT Imie, Nazwisko, IDDzialu, Pensja, ID FROM tblPracownicy"
        Me.SqlSelectCommand1.Connection = Me.SqlConnection1
        '
        'SqlInsertCommand1
        '
        Me.SqlInsertCommand1.CommandText = "INSERT INTO tblPracownicy(Imie, Nazwisko, IDDzialu, Pensja) VALUES (@Imie, @Nazwi" & _
        "sko, @IDDzialu, @Pensja); SELECT Imie, Nazwisko, IDDzialu, Pensja, ID FROM tblPr" & _
        "acownicy WHERE (ID = @@IDENTITY)"
        Me.SqlInsertCommand1.Connection = Me.SqlConnection1
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Imie", System.Data.SqlDbType.VarChar, 50, "Imie"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Nazwisko", System.Data.SqlDbType.VarChar, 70, "Nazwisko"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@IDDzialu", System.Data.SqlDbType.Int, 4, "IDDzialu"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Pensja", System.Data.SqlDbType.Money, 8, "Pensja"))
        '
        'SqlUpdateCommand1
        '
        Me.SqlUpdateCommand1.CommandText = "UPDATE tblPracownicy SET Imie = @Imie, Nazwisko = @Nazwisko, IDDzialu = @IDDzialu" & _
        ", Pensja = @Pensja WHERE (ID = @Original_ID) AND (IDDzialu = @Original_IDDzialu " & _
        "OR @Original_IDDzialu IS NULL AND IDDzialu IS NULL) AND (Imie = @Original_Imie) " & _
        "AND (Nazwisko = @Original_Nazwisko) AND (Pensja = @Original_Pensja OR @Original_" & _
        "Pensja IS NULL AND Pensja IS NULL); SELECT Imie, Nazwisko, IDDzialu, Pensja, ID " & _
        "FROM tblPracownicy WHERE (ID = @ID)"
        Me.SqlUpdateCommand1.Connection = Me.SqlConnection1
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Imie", System.Data.SqlDbType.VarChar, 50, "Imie"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Nazwisko", System.Data.SqlDbType.VarChar, 70, "Nazwisko"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@IDDzialu", System.Data.SqlDbType.Int, 4, "IDDzialu"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Pensja", System.Data.SqlDbType.Money, 8, "Pensja"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_ID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "ID", System.Data.DataRowVersion.Original, Nothing))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_IDDzialu", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "IDDzialu", System.Data.DataRowVersion.Original, Nothing))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Imie", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Imie", System.Data.DataRowVersion.Original, Nothing))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Nazwisko", System.Data.SqlDbType.VarChar, 70, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Nazwisko", System.Data.DataRowVersion.Original, Nothing))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Pensja", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Pensja", System.Data.DataRowVersion.Original, Nothing))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ID", System.Data.SqlDbType.Int, 4, "ID"))
        '
        'SqlDeleteCommand1
        '
        Me.SqlDeleteCommand1.CommandText = "DELETE FROM tblPracownicy WHERE (ID = @Original_ID) AND (IDDzialu = @Original_IDD" & _
        "zialu OR @Original_IDDzialu IS NULL AND IDDzialu IS NULL) AND (Imie = @Original_" & _
        "Imie) AND (Nazwisko = @Original_Nazwisko) AND (Pensja = @Original_Pensja OR @Ori" & _
        "ginal_Pensja IS NULL AND Pensja IS NULL)"
        Me.SqlDeleteCommand1.Connection = Me.SqlConnection1
        Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_ID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "ID", System.Data.DataRowVersion.Original, Nothing))
        Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_IDDzialu", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "IDDzialu", System.Data.DataRowVersion.Original, Nothing))
        Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Imie", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Imie", System.Data.DataRowVersion.Original, Nothing))
        Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Nazwisko", System.Data.SqlDbType.VarChar, 70, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Nazwisko", System.Data.DataRowVersion.Original, Nothing))
        Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Pensja", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Pensja", System.Data.DataRowVersion.Original, Nothing))
        '
        'SqlConnection1
        '
        Me.SqlConnection1.ConnectionString = "workstation id=WER;packet size=4096;integrated security=SSPI;data source=WER;pers" & _
        "ist security info=False;initial catalog=Nowinki"
        '
        'frmUpdates
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(664, 405)
        Me.Controls.Add(Me.btnUpdate)
        Me.Controls.Add(Me.btnLoad)
        Me.Controls.Add(Me.grdDataGrid)
        Me.Name = "frmUpdates"
        Me.Text = "Aktualizacje"
        CType(Me.grdDataGrid, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub frmUpdates_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private dsEmployeeInfo As DataSet
    Private daEmployees As SqlDataAdapter
    Private conn As New SqlConnection( _
        "server=localhost;uid=sa;database=Nowinki")

    ' Automatyczne generowanie poleceń aktualizacyjnych
    ' za pomocą obiektu SqlCommandBuilder
    Private cbEmployees As SqlCommandBuilder
    Private Sub btnLoad_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnLoad.Click

        dsEmployeeInfo = New DataSet

        ' LoadCommandBuilder()
        ' LoadExplicitCode()
        LoadWizardCode()

        ' Konfigurowanie siatki
        Me.grdDataGrid.PreferredColumnWidth = 110
        Me.grdDataGrid.AllowSorting = True
        ' Wypełnianie zestawu danych
        ' daEmployees.Fill(dsEmployeeInfo, "Pracownicy")
        SqlDataAdapter1.Fill(dsEmployeeInfo, "Pracownicy")

        ' Wiązanie zestawu danych z siatką
        Me.grdDataGrid.DataSource = _
           dsEmployeeInfo.Tables("Pracownicy")
        Me.btnUpdate.Enabled = True
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnUpdate.Click
        Try
            ' daEmployees.Update(dsEmployeeInfo, "Pracownicy")
            SqlDataAdapter1.Update(dsEmployeeInfo, "Pracownicy")
        Catch es As SqlException
            MessageBox.Show(es.Message)
        End Try
    End Sub

    Private Sub LoadCommandBuilder()
        Dim param As SqlParameter
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If

        ' Tworzenie nowego obiektu DataAdapter
        Dim SQL As String
        SQL = "Select Imie, Nazwisko, IDDzialu, Pensja, ID from tblPracownicy"
        daEmployees = New SqlDataAdapter(SQL, conn)

        ' Automatyczne generowanie poleceń aktualizacyjnych
        ' za pomocą obiektu SqlCommandBuilder
        cbEmployees = New SqlCommandBuilder(daEmployees)
    End Sub

    Private Sub LoadExplicitCode()
        Dim param As SqlParameter
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If
        ' Tworzenie nowego obiektu DataAdapter
        daEmployees = New SqlDataAdapter

        ' Konfigurowanie polecenia SELECT (procedura składowana)
        daEmployees.SelectCommand = New SqlCommand
        With daEmployees.SelectCommand
            .Connection = conn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "WybieraniePracownikow"
        End With

        ' Konfigurowanie polecenia INSERT (procedura składowana)
        daEmployees.InsertCommand = New SqlCommand
        With daEmployees.InsertCommand
            .Connection = conn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "WstawianiePracownika"
        End With

        param = daEmployees.InsertCommand.Parameters.Add( _
         New SqlParameter("@Imie", SqlDbType.VarChar, 50))
        param.Direction = ParameterDirection.Input
        param.SourceColumn = "Imie"
        param.SourceVersion = DataRowVersion.Current

        param = daEmployees.InsertCommand.Parameters.Add( _
         New SqlParameter("@Nazwisko", SqlDbType.VarChar, 70))
        param.Direction = ParameterDirection.Input
        param.SourceColumn = "Nazwisko"
        param.SourceVersion = DataRowVersion.Current

        param = daEmployees.InsertCommand.Parameters.Add( _
          New SqlParameter("@IDDzialu", SqlDbType.Int))
        param.Direction = ParameterDirection.Input
        param.SourceColumn = "IDDzialu"
        param.SourceVersion = DataRowVersion.Current

        param = daEmployees.InsertCommand.Parameters.Add( _
          New SqlParameter("@Pensja", SqlDbType.Money))
        param.Direction = ParameterDirection.Input
        param.SourceColumn = "Pensja"
        param.SourceVersion = DataRowVersion.Current

        ' Konfigurowanie polecenia UPDATE (procedura składowana)
        daEmployees.UpdateCommand = New SqlCommand
        With daEmployees.UpdateCommand
            .Connection = conn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "AktualizacjaPracownika"
        End With

        param = daEmployees.UpdateCommand.Parameters.Add( _
         New SqlParameter("@Imie", SqlDbType.VarChar, 50))
        param.Direction = ParameterDirection.Input
        param.SourceColumn = "Imie"
        param.SourceVersion = DataRowVersion.Current

        param = daEmployees.UpdateCommand.Parameters.Add( _
         New SqlParameter("@Nazwisko", SqlDbType.VarChar, 70))
        param.Direction = ParameterDirection.Input
        param.SourceColumn = "Nazwisko"
        param.SourceVersion = DataRowVersion.Current

        param = daEmployees.UpdateCommand.Parameters.Add( _
          New SqlParameter("@IDDzialu", SqlDbType.Int))
        param.Direction = ParameterDirection.Input
        param.SourceColumn = "IDDzialu"
        param.SourceVersion = DataRowVersion.Current

        param = daEmployees.UpdateCommand.Parameters.Add( _
          New SqlParameter("@Pensja", SqlDbType.Money))
        param.Direction = ParameterDirection.Input
        param.SourceColumn = "Pensja"
        param.SourceVersion = DataRowVersion.Current

        param = daEmployees.UpdateCommand.Parameters.Add( _
         New SqlParameter("@Pierwotny_ID", SqlDbType.Int))
        param.Direction = ParameterDirection.Input
        param.SourceColumn = "ID"
        param.SourceVersion = DataRowVersion.Original

        ' Konfigurowanie polecenia SELECT (procedura składowana)
        daEmployees.DeleteCommand = New SqlCommand
        With daEmployees.DeleteCommand
            .Connection = conn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "UsuwaniePracownika"
        End With

        param = daEmployees.DeleteCommand.Parameters.Add( _
         New SqlParameter("@Pierwotny_ID", SqlDbType.Int))
        param.Direction = ParameterDirection.Input
        param.SourceColumn = "ID"
        param.SourceVersion = DataRowVersion.Original

    End Sub

    Private Sub LoadWizardCode()
        If SqlConnection1.State = ConnectionState.Closed Then
            SqlConnection1.Open()
        End If
    End Sub

End Class
