Guest User

Untitled

a guest
Jul 1st, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.08 KB | None | 0 0
  1.                
  2.  
  3.                 'MODULE CONNECTION
  4.  
  5. Imports System
  6. Imports System.Data
  7. Imports MySql.Data.MySqlClient
  8.  
  9. Module connection
  10.  
  11. Public connStr As String = "Server=localhost;Database=dbdatabase;User=root;Password=root;"
  12.     Public conn As MySqlConnection = New MySqlConnection(connStr)
  13.     Sub main()
  14.  
  15.         conn.Open()
  16.         conn.Close()
  17.         Application.Run(New Form1())
  18.     End Sub
  19.  
  20. End Module
  21.             'LOGIN  (Button)
  22.  
  23.         ' ----- [DECLAIRE] -----------------------------
  24.         Dim val As Boolean
  25.     Dim cmd As MySqlCommand
  26.     Dim rdr As MySqlDataReader
  27.        Dim SQL As String = "Select username,password from tuser where username='" & txtusername.Text & "' and password='" & txtpassword.Text & "';"
  28.  
  29.         ' ----- [OPEN CONNECTION] ------------------------------
  30.         conn.Open()
  31.         cmd = New MySqlCommand(SQL, conn)
  32.         rdr  = cmd.ExecuteReader
  33.  
  34.         ' ----- [READ] ------------------------------
  35.         While (rdr.Read())
  36.             val = True
  37.         End While
  38.  
  39.         ' ----- [CLOSE CONNECTION] ----------------------------
  40.         rdr.Close()
  41.         conn.Close()
  42.  
  43.         ' ----- [CONDITION] ---------------------------
  44.         If val = True Then
  45.             Form2.Show()
  46.             Me.Hide()
  47.             Exit Sub
  48.         End If
  49.  
  50.         ' ----- [FAILD]----------------------------
  51.         '-------------------------------------------
  52.         MsgBox("Incorrect Username Or Password ")
  53.         '-------------------------------------------
  54.  
  55.             'SAVE (Button)
  56.  
  57.         ' ----- [DECLAIRE] --------------------------
  58.         Dim cmd As MySqlCommand
  59.         Dim Sql As String = "INSERT into tuser (username,password) VALUES ('" & txtusername.Text & "','" & txtpassword.Text & "' )"
  60.  
  61.         ' ----- [OPEN CONNECTION] -----------------------------
  62.         conn.Open()
  63.  
  64.         ' ----- [READ] ---------------------------------------
  65.         cmd = New MySqlCommand(Sql, conn)
  66.         cmd.ExecuteNonQuery()
  67.  
  68.                 ' ---- [CLOSE CONNECTION] ---------------------------
  69.         conn.Close()
  70.  
  71.         ' ----- [CALL POPULATE TO REFRESH THE LISTVIEW] -----
  72.         PopulateData()
  73.         '--------- [CLEAR TEXTBOX VALUE] ----------------------
  74.         txtusername.Text = ""
  75.         txtpassword.Text = ""
  76.  
  77.                      'UPDATE (Button)
  78.  
  79.     ' -----  [DECLAIRE]
  80.         Dim cmd As MySqlCommand
  81.         Dim Sql As String = "UPDATE tuser SET username = '" & txtusername.Text & "', password = '" & txtpassword.Text & "' where user_id = '" & lblID.Text & "' "
  82.  
  83.         ' -----  [OPEN CONNECTION]
  84.         conn.Open()
  85.  
  86.         ' ------ [READ]
  87.         cmd = New MySqlCommand(Sql, conn)
  88.         cmd.ExecuteNonQuery()
  89.  
  90.         ' ------ [CLOSE CONNECTION]
  91.         conn.Close()
  92.  
  93.         ' ------ [CALL POPULATE TO REFRESH THE LISTVIEW]
  94.         PopulateData()
  95.  
  96.         '------- [CLEAR TEXTBOX VALUE]
  97.         txtusername.Text = ""
  98.         txtpassword.Text = ""
  99.  
  100.         ' ----  [MSG BOX]
  101.         MsgBox("Successfully Updated")
  102.  
  103.  
  104.             'DELETE (Button)
  105.         ' ----- [DECLAIRE] --------------------------
  106.         Dim cmd As MySqlCommand
  107.         Dim Sql As String = "DELETE FROM tuser WHERE user_id = '" & lblID.Text & "' "
  108.  
  109.         ' ----- [OPEN CONNECTION] -----------------------------
  110.         conn.Open()
  111.  
  112.         ' ----- [READ] ---------------------------------------
  113.         cmd = New MySqlCommand(Sql, conn)
  114.         cmd.ExecuteNonQuery()
  115.  
  116.         ' ----- [CLOSE CONNECTION] ---------------------------
  117.         conn.Close()
  118.  
  119.         ' ----- [CALL POPULATE TO REFRESH THE LISTVIEW] -----
  120.         PopulateData()
  121.  
  122.         ' ----- [MSG BOX] ---------------------------------------
  123.         MsgBox("Successfully Deleted")
  124.  
  125.  
  126.             'EDIT (Button)
  127.  
  128.  Dim itm As ListViewItem
  129.         For Each itm In ListView1.SelectedItems
  130.             lblID.Text = itm.Text
  131.             txtusername.Text = itm.SubItems(1).Text
  132.             txtpassword.Text = itm.SubItems(2).Text
  133.     Next
  134.  
  135. LISTVIEW MOUSE CLICK
  136.  
  137.    Dim itm As ListViewItem
  138.         For Each itm In ListView1.SelectedItems
  139.             lblID.Text = itm.Text
  140.     Next
  141.  
  142.  
  143.  
  144.  
  145.         'POPULATE DATA (ListView)
  146.  
  147.     Sub PopulateData()
  148.         ' ----- [DECLAIRE] --------------------------
  149.         Dim lst As ListViewItem
  150.         Dim col As Integer
  151.         Dim cmd As MySqlCommand
  152.         Dim rdr As MySqlDataReader
  153.         Dim Sql As String = "Select * From tuser"
  154.         ListView1.Items.Clear()
  155.  
  156.         ' ----- [OPEN CONNECTION] -------------------
  157.         conn.Open()
  158.         cmd = New MySqlCommand(Sql, conn)
  159.         rdr = cmd.ExecuteReader
  160.  
  161.         ' ----- [READ] ------------------------------
  162.         If rdr.HasRows Then
  163.             While rdr.Read()
  164.                 lst = ListView1.Items.Add(rdr.GetValue(0).ToString)
  165.                 For col = 1 To rdr.FieldCount - 1
  166.                     lst.SubItems.Add(rdr.GetValue(col).ToString())
  167.                 Next col
  168.             End While
  169.         End If
  170.  
  171.         ' ----- [CLOSE CONNECTION] -----------------
  172.         conn.Close()
  173.     End Sub
  174.  
  175.  
  176. 'Import this Every Form
  177. 'Imports System
  178. 'Imports System.Data
  179. 'Imports MySql.Data.MySqlClient
Add Comment
Please, Sign In to add comment