Advertisement
Guest User

VB insert records in database

a guest
Aug 12th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. Imports System.Data.SqlClient
  2. Imports System.Windows
  3. Imports System.Xml
  4.  
  5. Public Class Form1
  6.  
  7. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  8. Dim Username = txtUserName.Text
  9. Dim phone = txtPhone.Text
  10. Dim add = txtAddress.Text
  11. Dim comments = txtComments.Text
  12.  
  13. Dim connetionString As String
  14. Dim connection As SqlConnection
  15. Dim adapter As SqlDataAdapter
  16. Dim ds As New DataSet
  17. Dim sql As String
  18.  
  19. Try
  20.  
  21. connetionString = "Data Source=(local);Initial Catalog=cand;User ID=test;Password=test"
  22. connection = New SqlConnection(connetionString)
  23.  
  24. connection.Open()
  25.  
  26.  
  27. sql = "Insert Into person values ('" & txtUserName.Text & "','" & txtPhone.Text & "' , '" & txtAddress.Text & "', '" & txtComments.Text & "') "
  28.  
  29. adapter = New SqlDataAdapter(sql, connection)
  30. adapter.Fill(ds)
  31.  
  32. Dim insert As New SqlCommand(sql, connection)
  33.  
  34. insert.Parameters.AddWithValue(Username, txtUserName.Text)
  35. insert.Parameters.AddWithValue(phone, txtPhone.Text)
  36. insert.Parameters.AddWithValue(add, txtAddress.Text)
  37. insert.Parameters.AddWithValue(comments, txtComments.Text)
  38.  
  39. insert.ExecuteNonQuery()
  40.  
  41. MsgBox("Successfully Saved")
  42. Me.Visible = False
  43.  
  44. Catch ex As Exception
  45.  
  46. MessageBox.Show(ex.Message)
  47.  
  48.  
  49. End Try
  50.  
  51. End Sub
  52.  
  53. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  54. Dim connetionString As String
  55. Dim connection As SqlConnection
  56. Dim adapter As SqlDataAdapter
  57. Dim ds As New DataSet
  58. Dim sql As String
  59.  
  60. connetionString = "Data Source=(local);Initial Catalog=cand;User ID=test;Password=test"
  61. connection = New SqlConnection(connetionString)
  62. sql = "select * from person"
  63. Try
  64. connection.Open()
  65. adapter = New SqlDataAdapter(sql, connection)
  66. adapter.Fill(ds)
  67. connection.Close()
  68. ds.WriteXml("Person.xml")
  69. MsgBox("Done")
  70. Catch ex As Exception
  71. MsgBox(ex.ToString)
  72. End Try
  73.  
  74. End Sub
  75. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement