Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' put a dataGridView1 control on your form
- ' and don't forget to insert your SQL Connection string and SQL query into the code
- Imports System.Data
- Imports System.Data.SqlClient
- Public Class Form1
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- ' Render data onto the screen
- DataGridView1.DataSource = FillData("<Your SQL Connection String here>", "<Your SQL Query here>")
- End Sub
- Private Function FillData(strConnection As String, SQLQuery As String) As DataTable
- Dim dt As New DataTable
- ' Open connection
- Using conn As SqlConnection = New SqlConnection(strConnection)
- conn.Open()
- Dim cmd = New SqlCommand(SQLQuery, conn)
- ' Create new DataAdapter
- Using reader As SqlDataReader = cmd.ExecuteReader()
- ' Use DataAdapter to fill DataTable
- Try
- ' fill the DataTable
- dt.Load(reader)
- Catch ex As Exception
- End Try
- End Using
- End Using
- ' return the DataTable
- Return dt
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement