Hey guys! I have limited experience with Visual Studio and have a question about Querying an Access database. I have created a Visual Studio application that displays and modifies an access database. I have use datagridview and detailed view to insert data and view data in the Access database. I am now trying to figure out the best way to query the data in the database. I have tried right clicking a table in the database explorer and creating a new query. Everything works and I can view the data but I am unsure how to display this query result in datagridview. So my question is how do I view this result in datagrid view? Thank you all in advance.
jtodd
0
Junior Poster in Training
Recommended Answers
Jump to PostHere is a C# sample for your reference:
using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0;User Id=;Password=;Data Source=" + fileName); conn.Open(); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query_txt.Text, conn); DataSet ds = new DataSet(); dataAdapter.Fill(ds); dataGridView.DataSource = ds.tables[0]; conn.Close();
Jump to PostHere's the code in VB.
Dim ds As New DataSet Dim AccessConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=access.mdb;") Dim sql As String = "Your_Query" Dim AccessCommand As New OleDbCommand(sql, AccessConn) Dim da As New OleDbDataAdapter(AccessCommand) da.Fill(ds) DataGridview1.DataSource = ds.Tables(0)
Jump to PostThank Plasma! I tried the code below and when I try to run it the datagrid is still blank. Any thoughts? Am I doing something wrong? Thanks again for your help!
Imports System.Data.OleDb Public Class Form1 Private Sub DataGridView1_Click(sender As Object, e As System.EventArgs) Handles DataGridView1.Click Dim ds …
All 9 Replies
catherine sea
15
Junior Poster
jtodd
0
Junior Poster in Training
Phasma
11
Junior Poster in Training
jtodd
0
Junior Poster in Training
jtodd
0
Junior Poster in Training
adam_k
239
Master Poster
jtodd
0
Junior Poster in Training
Phasma
11
Junior Poster in Training
jtodd
0
Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.