Advertisement
JayBeeOH

Bare Bones DataReader

May 11th, 2016
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.31 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Bare Bones Data Reader
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   11 MAY 2016
  16. '
  17. ' Description:    This database inquiry uses the DataReader to quickly
  18. '                 retrieve records from a table and display them in a datagridview.
  19. '                 For this demonstration, the Customers table from the MS Access
  20. '                 database Northwind is being used. The database's file location
  21. '                 has been hardcoded.
  22. '
  23. '                 To choose the correct ConnectionString,
  24. '                   see http://www.connectionstrings.com/ .
  25. '
  26. '                 Documentation is at:
  27. '                   App's screen image is at: http://imgur.com/dQEpAZt
  28. '                   App's Visual Basic .NET code is at http://pastebin.com/S7mJtk0Q
  29. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  30. '-------------------------------------------------------------------------------------------
  31.  
  32. Imports System.Data.OleDb       ' Using a MS Access database
  33.  
  34. Public Class InquiryForm
  35.  
  36.     ' Normally, the ConnectionString is retrieved from the App.config file.
  37.     Const CONN_STRING As String = "Provider=Microsoft.ACE.OLEDB.12.0;" &
  38.                             "Data Source=U:\Databases\AccessDatabases\Northwind.accdb;" &
  39.                             "Persist Security Info=False;"
  40.  
  41.     Private bs As New BindingSource
  42.  
  43.     ' Form Load - Initialization and Housekeeping.
  44.     Private Sub InquiryForm_Load(sender As Object, e As EventArgs) _
  45.         Handles MyBase.Load
  46.  
  47.         ' DataGridView property changes that could be done in the design mode.
  48.         With InquiryDGV
  49.             '.Dock = DockStyle.Fill
  50.             .AllowUserToAddRows = False
  51.             .AllowUserToDeleteRows = False
  52.             .AllowUserToOrderColumns = True
  53.             .AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke
  54.             .Anchor = (AnchorStyles.Left Or AnchorStyles.Top Or
  55.                 AnchorStyles.Right Or AnchorStyles.Bottom)
  56.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
  57.             .BorderStyle = BorderStyle.Fixed3D
  58.             .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
  59.             .ReadOnly = True
  60.  
  61.             ' DataGridView Column Headers Bold – must be set in run mode.
  62.             .ColumnHeadersDefaultCellStyle.Font =
  63.                 New Font(.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold)
  64.         End With
  65.  
  66.     End Sub
  67.  
  68.     ' Filter and sort data from the database and display in a datagridview.
  69.     Private Sub SearchToolStripButton_Click(sender As Object, e As EventArgs) _
  70.         Handles SearchToolStripButton.Click
  71.  
  72.         ' Best Practise is to list fields to be selected. NOT THE WILDCARD!
  73.         ' Dim query As String = "SELECT * " &
  74.         Dim query As String = "SELECT Company, [Last Name], [First Name], " &
  75.                                     "[Job Title], [Business Phone], [Fax Number] " &
  76.                                 "FROM Customers " &
  77.                                 "WHERE [Last Name] Like @p1 " &
  78.                                 "ORDER BY [Last Name], [First Name];"
  79.  
  80.         Try
  81.             Using con As New OleDbConnection(CONN_STRING)
  82.                 Using cmd As New OleDbCommand(query, con)
  83.                     ' ANSI-89 wildcard character is the asterisk (*).
  84.                     ' ANSI-92 wildcard characters is the percent sign (%).
  85.                     cmd.Parameters.AddWithValue("@p1", LastNameToolStripTextBox.Text & "%")
  86.  
  87.                     con.Open()
  88.                     Using rdr As OleDbDataReader = cmd.ExecuteReader
  89.                         If rdr.HasRows Then
  90.                             bs.DataSource = rdr
  91.                             InquiryDGV.DataSource = bs
  92.                         Else
  93.                             MessageBox.Show("Search criteria yielded no records.",
  94.                                             Me.Text,
  95.                                             MessageBoxButtons.OK,
  96.                                             MessageBoxIcon.Information)
  97.                         End If
  98.                     End Using
  99.                 End Using
  100.             End Using
  101.         Catch ex As Exception
  102.             MessageBox.Show(ex.Message,
  103.                             Me.Text,
  104.                             MessageBoxButtons.OK,
  105.                             MessageBoxIcon.Error)
  106.         End Try
  107.  
  108.         ' Place cursor back into Textbox for next search.
  109.         LastNameToolStripTextBox.SelectAll()
  110.         LastNameToolStripTextBox.Focus()
  111.  
  112.     End Sub
  113.  
  114. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement