Read SQL Statement from ACCESS View
I have searched the net and cannot find a clue as to how to read the SQL statement from an ACCESS VIEW/QUERY using C#. Can anyone help please.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vilas GiteRank #951 · 2.1K reputationPosted Sep 13, 2011, 11:03 PM EDT
Hi Michael..
Refer this link, may be helpful for you…
http://www.codeproject.com/KB/database/DatabaseSpy_CS.aspx
Thanks!
----------------------------------------------------------------------
If this reply helps your post…then check "This is correct answer"
Michael Griffith25 reputationPosted Sep 14, 2011, 7:52 AM EDT
I have looked at the link and I have no trouble at all in accessing the database and looking at columns and data etc. What I cannot find is the location of the actual SQL string in a View. I can find all kinds of information about columns and their fields but nothing that is the SQL string.
I am now trying to go down the route of running Access from C# but it isn't really getting me anywhere although I have come across an interesting item 'oAccess.DoCmd.SelectObject(readSQLStatements)' but cannot seem to get any further with it and cannot find any reference to it on the web (googling).
All I wish to do is to extract the SQL statement string and save it elsewhere.
If i have missed something then I do apologise and can only plead being a complete amateur.
Kind regards
Michael
Michael Griffith25 reputationPosted Sep 14, 2011, 7:52 AM EDT
I have looked at the link and I have no trouble at all in accessing the database and looking at columns and data etc. What I cannot find is the location of the actual SQL string in a View. I can find all kinds of information about columns and their fields but nothing that is the SQL string.
I am now trying to go down the route of running Access from C# but it isn't really getting me anywhere although I have come across an interesting item 'oAccess.DoCmd.SelectObject(readSQLStatements)' but cannot seem to get any further with it and cannot find any reference to it on the web (googling).
All I wish to do is to extract the SQL statement string and save it elsewhere.
If i have missed something then I do apologise and can only plead being a complete amateur.
Kind regards
Michael
Michael Griffith25 reputationPosted Sep 14, 2011, 8:27 AM EDT
My apologies
Michael
Thomas74 reputationPosted Sep 14, 2011, 2:43 PM EDT
Either way you will need to open a connection to the DB with an OleDbConnection.
the connection sting I use is
OleDbConnection lConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source="
+ MYAccessDBPathAndName + ";Mode=Share Deny None;Extended Properties='';Jet OLEDB:System database='';Jet OLEDB:Registry Path='';Jet OLEDB:Engine Type=4;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False");
lConn.Open(); //Just make sure to close it when you are done
Examples of the command and adapter....
string MySQL = "Select * from MyTable";
DataTable MyCTable = new DataTable();
OleDbDataAdapter dadapt = new OleDbDataAdapter(MySQL, lConn);
dadapt.Fill(MyCTable);
(then you can do whatever you want with the data table now in C#)
OR
OleDbCommand lAccCmd = new OleDbCommand();
lAccCmd.Connection = lConn;
MySQL = "Delete * from MyAccessTable;
lAccCmd.CommandText = MySQL;
lAccCmd.ExecuteNonQuery();
lConn.Close();
Thomas74 reputationPosted Sep 14, 2011, 2:49 PM EDT
As far as I know, outside of access Views are just seen as other tables. I don't think you can get to the SQL behind the view unless you are physically in that access database.
I have more experience with tables than views though. sorry.