MbahAgis

Update If Exists Else Insert - SQL

Nov 24th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.98 KB | None | 0 0
  1.  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.  
  3.         'jika stat_printer = 0 Or stat_cd1 = 0 Or stat_cd2 = 0 And stat_network = ON
  4.         'maka kolom ttl_status = "ON"
  5.  
  6.         'Else
  7.  
  8.         'jika stat_printer = 1 Or stat_cd1 = 0 Or stat_cd2 = 0 And stat_network = ON
  9.         'maka kolom ttl_status ="OFF"
  10.  
  11.         For Each x As DataGridViewRow In DataGridView1.Rows
  12.             If Not x.IsNewRow Then
  13.                 If x.Cells("stat_printer").Value = 0 Or x.Cells("stat_cd1").Value = 0 Or
  14.                     x.Cells("stat_cd2").Value = 0 AndAlso x.Cells("stat_network").Value = "ON" Then
  15.                     x.Cells("ttl_status").Value = "ON"
  16.                 Else
  17.                     x.Cells("ttl_status").Value = "OFF"
  18.                 End If
  19.             End If
  20.         Next
  21.  
  22.         Dim query, queryInsert As New System.Text.StringBuilder
  23.         query.AppendLine("IF EXISTS (SELECT * FROM tbl_monitoring Where kd_store=@kd) ")
  24.         query.Append("update tbl_monitoring Set")
  25.         queryInsert.Append("Insert Into tbl_monitoring values(")
  26.  
  27.         For Each x As DataGridViewColumn In DataGridView1.Columns
  28.             If Not x.Name = "kd_store" Then
  29.                 query.AppendFormat(" {0}=@p{1},", x.Name, x.Index)
  30.                 queryInsert.AppendFormat("@p{0},", x.Index)
  31.             Else
  32.                 queryInsert.Append("@kd,")
  33.             End If
  34.         Next
  35.  
  36.         query.Length -= 1
  37.         queryInsert.Length -= 1
  38.         queryInsert.Append(")")
  39.         query.AppendLine(" where kd_store = @kd ")
  40.         query.AppendLine("Else ")
  41.         query.Append(queryInsert.ToString)
  42.  
  43.         Using con As New SqlConnection("Server=.;Database=dummy;Trusted_Connection=True")
  44.             Using cmd As New SqlCommand(query.ToString, con)
  45.                 Try
  46.                     con.Open()
  47.                     For Each x As DataGridViewRow In DataGridView1.Rows
  48.                         If Not x.IsNewRow Then
  49.                             cmd.Parameters.Clear()
  50.                             For Each y As DataGridViewColumn In DataGridView1.Columns
  51.                                 If y.Name = "kd_store" Then
  52.                                     cmd.Parameters.Add("@kd", SqlDbType.VarChar, 100).Value =
  53.                                                                       x.Cells(y.Index).Value
  54.                                 Else
  55.                                     cmd.Parameters.Add("@p" & y.Index, SqlDbType.VarChar, 100).Value =
  56.                                                                    x.Cells(y.Index).Value
  57.                                 End If
  58.                             Next
  59.                             cmd.ExecuteNonQuery()
  60.                         End If
  61.                     Next
  62.                     con.Close()
  63.                     MsgBox("Done")
  64.                 Catch ex As Exception
  65.                     MsgBox(ex.ToString)
  66.                 End Try
  67.             End Using
  68.         End Using
  69.        
  70.   End Sub
Advertisement
Add Comment
Please, Sign In to add comment