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 PostjTodd you need to use the
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database1.accdb;Persist Security Info=False;"
or if passworded
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database1.accdb;Jet OLEDB:Database Password=MyPassword;"
for your connection string since yu are using an Access 2007/2010 database. I am suprised you didn't get an error when you ran it. You should wrap …
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.