paulpaul003

vb.netMSSQL

May 1st, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. //create project
  2. //create db
  3.  
  4. //add module
  5.  
  6. 1.module1.vb
  7.  
  8. Imports System.Data.SqlClient
  9. Module Module1
  10.  
  11. Public conn As SqlConnection
  12. Public Query As String
  13. Public DA As SqlDataAdapter
  14. Public DS As DataSet
  15. Public cmd As SqlCommand
  16. Public DR As SqlDataReader
  17.  
  18.  
  19.  
  20. Sub connect()
  21.  
  22. conn = New SqlConnection
  23. conn.ConnectionString = "data source=APEPURIL\SQLEXPRESS;initial catalog=test;user=paulpaul003;password=paul091725"
  24.  
  25. Try
  26. If conn.State = ConnectionState.Closed Then
  27. conn.Open()
  28. MsgBox("connected")
  29. End If
  30. Catch ex As Exception
  31.  
  32. MsgBox("connection closed" & Err.Description)
  33.  
  34. End Try
  35.  
  36. End Sub
  37.  
  38. End Module
  39.  
  40.  
  41.  
  42. 2. SQLSERVER = right click Security/login , Server roles = check sysadmin
  43.  
  44.  
  45. 3. form1.vb
  46.  
  47. Imports System.Data.SqlClient
  48. Public Class Form1
  49.  
  50. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  51. connect()
  52.  
  53. displayData()
  54.  
  55.  
  56. End Sub
  57.  
  58. Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
  59.  
  60. cmd = New SqlCommand
  61. cmd.Connection = conn
  62.  
  63. Query = "INSERT INTO tbl_info VALUES('" & txtFirstname.Text & "', '" & txtLastname.Text & "', '" & txtAge.Text & "') "
  64. cmd.CommandText = Query
  65. cmd.ExecuteNonQuery()
  66.  
  67. displayData()
  68.  
  69.  
  70.  
  71. End Sub
  72.  
  73. Sub displayData()
  74. DS = New DataSet
  75. Query = "SELECT * FROM tbl_info"
  76. DA = New SqlDataAdapter(Query, conn)
  77. DA.Fill(DS)
  78.  
  79. dgvInfo.DataSource = DS.Tables(0)
  80. End Sub
  81.  
  82. Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
  83. cmd = New SqlCommand
  84. cmd.Connection = conn
  85.  
  86. Query = "UPDATE tbl_info SET firstname='" & txtFirstname.Text & "', lastname='" & txtLastname.Text & "', age='" & txtAge.Text & "' WHERE id='" & lblId.Text & "'"
  87. cmd.CommandText = Query
  88. cmd.ExecuteNonQuery()
  89.  
  90. displayData()
  91.  
  92. End Sub
  93.  
  94. Private Sub dgvInfo_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvInfo.CellClick
  95.  
  96. Dim i As Integer
  97. i = dgvInfo.CurrentRow.Index
  98.  
  99. lblId.Text = dgvInfo(0, i).Value
  100. txtFirstname.Text = dgvInfo(1, i).Value
  101. txtLastname.Text = dgvInfo(2, i).Value
  102. txtAge.Text = dgvInfo(3, i).Value
  103.  
  104.  
  105.  
  106. End Sub
  107.  
  108.  
  109.  
  110. Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
  111.  
  112. If MsgBox("Delete Confirmation", vbInformation + vbYesNo) = vbYes Then
  113.  
  114. cmd = New SqlCommand
  115. cmd.Connection = conn
  116.  
  117. Query = "DELETE FROM tbl_info WHERE id='" & lblId.Text & "'"
  118. cmd.CommandText = Query
  119. cmd.ExecuteNonQuery()
  120.  
  121. displayData()
  122.  
  123. End If
  124.  
  125. End Sub
  126. End Class
  127.  
  128.  
  129. //Crystal Report
  130. 4. Project/projectname properties/compile/advance compile/tager framework = .net framework 4
  131.  
  132. add new item Reporting/Crystal Report
  133.  
  134. Create new connection/ OLE DB(ADO)/ provider for SQL SERVER
  135.  
  136. form2.show()
  137.  
  138. //form2
  139. 5. create form2.vb
  140.  
  141. Imports CrystalDecisions.CrystalReports.Engine
  142. Imports System.Data.SqlClient
  143. Public Class Form2
  144.  
  145.  
  146. Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  147.  
  148. Dim reportPath As String = "C:\testproject\testproject\CrystalReport1.rpt"
  149.  
  150. Dim report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
  151.  
  152. DS = New DataSet
  153.  
  154. Query = "SELECT * FROM tbl_info WHERE id='" & Form1.lblId.Text & "'"
  155.  
  156. DA = New SqlDataAdapter(Query, conn)
  157.  
  158. DA.Fill(DS)
  159.  
  160. report.Load(reportPath)
  161. report.SetDataSource(DS.Tables(0))
  162.  
  163. CrystalReportViewer1.ReportSource = report
  164. CrystalReportViewer1.Refresh()
  165.  
  166.  
  167.  
  168.  
  169. End Sub
  170. End Class
  171.  
  172. //app.config
  173. 6.
  174. <startup useLegacyV2RuntimeActivationPolicy="true">
  175.  
  176.  
  177. 7.login.vb
  178.  
  179. //create new tbl_users from sql server
  180.  
  181. Imports System.Data.SqlClient
  182.  
  183. Public Class login
  184.  
  185. Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
  186.  
  187.  
  188. DS = New DataSet
  189.  
  190.  
  191.  
  192. cmd = New SqlCommand
  193. cmd.Connection = conn
  194.  
  195. Query = "SELECT * FROM tbl_users WHERE username= '" & txtUsername.Text & "' and password= '" & md5(txtPassword.Text) & "'"
  196.  
  197. cmd.CommandText = Query
  198.  
  199. DR = cmd.ExecuteReader()
  200.  
  201.  
  202. If DR.HasRows Then
  203.  
  204. MsgBox("success")
  205. Form1.Show()
  206. Me.Hide()
  207. Else
  208. MsgBox("error")
  209.  
  210. End If
  211.  
  212.  
  213.  
  214.  
  215.  
  216. End Sub
  217.  
  218. Private Function md5(sPassword As String) As String
  219. Dim x As New System.Security.Cryptography.MD5CryptoServiceProvider()
  220. Dim bs As Byte() = System.Text.Encoding.UTF8.GetBytes(sPassword)
  221. bs = x.ComputeHash(bs)
  222. Dim s As New System.Text.StringBuilder()
  223. For Each b As Byte In bs
  224. s.Append(b.ToString("x2").ToLower())
  225. Next
  226. Return s.ToString()
  227. End Function
  228.  
  229. Private Sub login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  230. connect()
  231. End Sub
  232.  
  233. Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
  234. register.show()
  235. Me.Hide()
  236.  
  237. End Sub
  238. End Class
  239.  
  240.  
  241. 8. register user
  242.  
  243. Imports System.Data.SqlClient
  244.  
  245.  
  246. Public Class register
  247.  
  248.  
  249. Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
  250. login.Show()
  251. Me.Hide()
  252.  
  253.  
  254. End Sub
  255.  
  256. Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
  257.  
  258. If txtpassword.Text = txtRepassword.Text Then
  259.  
  260. cmd = New SqlCommand
  261. cmd.Connection = conn
  262.  
  263.  
  264.  
  265. Query = "INSERT INTO tbl_users VALUES('" & txtUsername.Text & "', '" & md5(txtpassword.Text) & "')"
  266. cmd.CommandText = Query
  267. cmd.ExecuteNonQuery()
  268.  
  269. MsgBox("success register")
  270. login.Show()
  271. Me.Hide()
  272.  
  273.  
  274. End If
  275.  
  276.  
  277. End Sub
  278.  
  279. Private Function md5(sPassword As String) As String
  280. Dim x As New System.Security.Cryptography.MD5CryptoServiceProvider()
  281. Dim bs As Byte() = System.Text.Encoding.UTF8.GetBytes(sPassword)
  282. bs = x.ComputeHash(bs)
  283. Dim s As New System.Text.StringBuilder()
  284. For Each b As Byte In bs
  285. s.Append(b.ToString("x2").ToLower())
  286. Next
  287. Return s.ToString()
  288. End Function
  289.  
  290. Private Sub register_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  291.  
  292. End Sub
  293. End Class
Add Comment
Please, Sign In to add comment