Advertisement
calfred2808

MyFirstSQL_vb.net__sourceCode

Jun 7th, 2014
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.93 KB | None | 0 0
  1. Imports System.Data.Sql
  2. Imports System.Data.SqlClient
  3.  
  4.  
  5. 'Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
  6.  
  7. 'Server=HP-AC\SQLEXPRESS;Database=FirstSQL;Trusted_Connection=True;
  8.  
  9.  
  10.  
  11. '================================================================================
  12. 'database name = FirstSQL                                                       ||
  13. 'table name    = MyTbl                                                          ||
  14. '                                                                               ||
  15. 'column names:                                                                  ||
  16. '                                                                               ||
  17. 'MyID     NVARCHAR(50)                                                          ||
  18. 'MyFNAME  NCHAR(10)                                                             ||
  19. 'MyLNAME     NCHAR(10)                                                          ||
  20. 'MyMNAME     NCHAR(10)                                                          ||
  21. 'MyNUMBER NVARCHAR(50)                                                          ||
  22. '=================================================================================
  23.  
  24.  
  25. Public Class MainForm
  26.  
  27.     Dim SQL As New sqlControlClass
  28.     Public Sub CheckConn()
  29.         If SQL.HasConn = True Then
  30.             Status.Text = "Connection has been Established"
  31.         Else
  32.             Status.BackColor = Color.Red
  33.             Status.Text = "Connection has   NOT     Established"
  34.         End If
  35.  
  36.     End Sub
  37.  
  38.  
  39.     Public Sub Populate()
  40.         Dim CONN As New SqlConnection With {.ConnectionString = "Server=HP-AC\SQLEXPRESS;Database=FirstSQL;Trusted_Connection=True;"}
  41.         Dim cmd As New SqlCommand
  42.         Dim Q As String = "SELECT * from MyTbl"
  43.  
  44.         Dim DA As New SqlDataAdapter
  45.         Dim DT As New DataTable
  46.  
  47.  
  48.  
  49.  
  50.         CONN.Open()
  51.         cmd.Connection = CONN
  52.  
  53.  
  54.         LVnames.Columns.Clear()
  55.         LVnames.Items.Clear()
  56.  
  57.         With LVnames
  58.             .Columns.Add("ID", 30, HorizontalAlignment.Left)
  59.             .Columns.Add("First Name", 1000, HorizontalAlignment.Left)
  60.             .Columns.Add("Last Name", 1000, HorizontalAlignment.Left)
  61.             .Columns.Add("Middle Name", 1000, HorizontalAlignment.Left)
  62.             .Columns.Add("Number", 1000, HorizontalAlignment.Left)
  63.         End With
  64.  
  65.  
  66.  
  67.         'cmd
  68.         With cmd
  69.             .CommandText = Q
  70.             .Connection = CONN
  71.         End With
  72.  
  73.  
  74.         'adapter
  75.         With DA
  76.             .SelectCommand = cmd
  77.             .Fill(DT)
  78.         End With
  79.  
  80.  
  81.         For i = 0 To DT.Rows.Count - 1
  82.  
  83.             With LVnames
  84.                 .Items.Add(DT.Rows(i)("MyID"))
  85.                 .Items.Add(DT.Rows(i)("MyFNAME"))
  86.                 .Items.Add(DT.Rows(i)("MyLNAME"))
  87.                 .Items.Add(DT.Rows(i)("MyMNAME"))
  88.                 .Items.Add(DT.Rows(i)("MyNUMBER"))
  89.             End With
  90.  
  91.  
  92.         Next
  93.  
  94.  
  95.  
  96.  
  97.  
  98.     End Sub
  99.  
  100.     Public Sub rECords()
  101.         Dim Q As String = "INSERT INTO MyTbl (MyID,MyFNAME,MyLNAME,MyMNAME,MyNUMBER) VALUES ('" & txtID.Text & "','" & txtFNAME.Text & "','" & txtLNAME.Text & "','" & txtMNAME.Text & "','" & txtNumber.Text & "' )"
  102.         Dim CMD As New SqlCommand
  103.         Dim CONN As New SqlConnection With {.ConnectionString = "Server=HP-AC\SQLEXPRESS;Database=FirstSQL;Trusted_Connection=True;"}
  104.  
  105.         With CMD
  106.             .Connection = CONN
  107.             .CommandText = Q
  108.             .ExecuteNonQuery()
  109.  
  110.         End With
  111.  
  112.  
  113.  
  114.     End Sub
  115.  
  116.     Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  117.         CheckConn()
  118.         Populate()
  119.  
  120.        
  121.  
  122.     End Sub
  123.  
  124.    
  125.     Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
  126.  
  127.         Dim CONN As New SqlConnection With {.ConnectionString = "Server=HP-AC\SQLEXPRESS;Database=FirstSQL;Trusted_Connection=True;"}
  128.         Dim cmd As New SqlCommand
  129.  
  130.         CONN.Open()
  131.         cmd.Connection = CONN
  132.  
  133.  
  134.         cmd.CommandText = "INSERT INTO MyTbl (MyID, MyFNAME, MyLNAME, MyMNAME, MyNUMBER) VALUES (@MyID, @MyFNAME, @MyLNAME, @MyMNAME, @MyNUMBER)"
  135.  
  136.         cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MyID", txtID.Text))
  137.         cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MyFNAME", txtFNAME.Text))
  138.         cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MyLNAME", txtLNAME.Text))
  139.         cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MyMNAME", txtMNAME.Text))
  140.         cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MyNUMBER", txtNumber.Text))
  141.  
  142.  
  143.         cmd.ExecuteNonQuery()
  144.         CONN.Close()
  145.  
  146.         MsgBox("Successfull")
  147.  
  148.     End Sub
  149.  
  150.     Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
  151.  
  152.         Try
  153.             Dim CONN As New SqlConnection With {.ConnectionString = "Server=HP-AC\SQLEXPRESS;Database=FirstSQL;Trusted_Connection=True;"}
  154.             Dim cmd As New SqlCommand
  155.  
  156.             Dim DA As New SqlDataAdapter
  157.             Dim DT As New DataTable
  158.  
  159.  
  160.  
  161.             CONN.Open()
  162.             cmd.Connection = CONN
  163.  
  164.             cmd.CommandText = "SELECT * FROM MyTbl where MyID = @MyID"
  165.             cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MyID", txtSearch.Text))
  166.  
  167.             DA.SelectCommand = cmd
  168.             DA.Fill(DT)
  169.  
  170.  
  171.             txtID.Text = DT.Rows(0)(0).ToString()
  172.             txtFNAME.Text = DT.Rows(0)(1).ToString()
  173.             txtLNAME.Text = DT.Rows(0)(2).ToString()
  174.             txtMNAME.Text = DT.Rows(0)(3).ToString()
  175.             txtNumber.Text = DT.Rows(0)(4).ToString()
  176.  
  177.  
  178.  
  179.             cmd.ExecuteNonQuery()
  180.             CONN.Close()
  181.         Catch ex As Exception
  182.             MsgBox(ex.Message)
  183.         Finally
  184.  
  185.         End Try
  186.  
  187.  
  188.     End Sub
  189.  
  190.     Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
  191.         Dim CONN As New SqlConnection With {.ConnectionString = "Server=HP-AC\SQLEXPRESS;Database=FirstSQL;Trusted_Connection=True;"}
  192.         Dim cmd As New SqlCommand
  193.  
  194.         CONN.Open()
  195.         cmd.Connection = CONN
  196.  
  197.         cmd.CommandText = "UPDATE MyTbl set MyID=@MyID, MyFNAME=@MyFNAME, MyLNAME=@MyLNAME, MyMNAME=@MyMNAME, MyNUMBER=@MyNUMBER    WHERE MyID=@MyID"
  198.         cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MyID", Convert.ToInt16(txtID.Text)))
  199.         cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MyFNAME", txtFNAME.Text))
  200.         cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MyLNAME", txtLNAME.Text))
  201.         cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MyMNAME", txtMNAME.Text))
  202.         cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MyNUMBER", txtNumber.Text))
  203.  
  204.         cmd.ExecuteNonQuery()
  205.         CONN.Close()
  206.         MsgBox("Success")
  207.        
  208.  
  209.     End Sub
  210.  
  211.     Private Sub btnDel_Click(sender As Object, e As EventArgs) Handles btnDel.Click
  212.  
  213.  
  214.         Try
  215.             Dim CONN As New SqlConnection With {.ConnectionString = "Server=HP-AC\SQLEXPRESS;Database=FirstSQL;Trusted_Connection=True;"}
  216.             Dim cmd As New SqlCommand
  217.  
  218.  
  219.             CONN.Open()
  220.             cmd.Connection = CONN
  221.  
  222.             cmd.CommandText = "DELETE FROM MyTbl  WHERE MyID = '" & txtID.Text & " '"
  223.  
  224.             MsgBox("Success")
  225.  
  226.             Populate()
  227.  
  228.             txtSearch.Text = ""
  229.             txtID.Text = ""
  230.             txtFNAME.Text = ""
  231.             txtLNAME.Text = ""
  232.             txtMNAME.Text = ""
  233.             txtNumber.Text = ""
  234.  
  235.  
  236.             cmd.ExecuteNonQuery()
  237.             CONN.Close()
  238.  
  239.         Catch ex As Exception
  240.             MsgBox(ex.Message)
  241.         End Try
  242.  
  243.     End Sub
  244.  
  245.     Private Sub btnRefr_Click(sender As Object, e As EventArgs) Handles btnRefr.Click
  246.         Populate()
  247.  
  248.         txtSearch.Text = ""
  249.         txtID.Text = ""
  250.         txtFNAME.Text = ""
  251.         txtLNAME.Text = ""
  252.         txtMNAME.Text = ""
  253.         txtNumber.Text = ""
  254.  
  255.     End Sub
  256. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement