Programming with Access? Know this about JOIN.
If you’ve ever implemented a small-scale WinForms .NET project with modest database needs, you’ve probably been tempted to keep it “simple” by using Microsoft Acess (.mdb) files.
This approach has some nice advantages:
- Most users already have Microsoft Office, so if they need to run reports or just look at their data they already have the tools.
- On the other hand, Windows already comes with the OLEDB drivers for Access, so it’s okay if your user hasn’t acquired Access itself.
- You don’t have to acquire or pay for a separate database engine.
- Runtime overhead is relatively light.
Access doesn’t do well under moderate-to-heavy load, but for smal-scale data entry applications I find the performance more than acceptable.
Within .NET, it’s often easiest to create your own queries with the OleDbCommand class. Here’s a quick example:
using System.Data.OleDb;public int getFooCount()
{
string sql = "select count(*) from foo";
OleDbCommand cmd = new OleDbCommand(sql);
// SET cmd.Connection TO A VALID OLEDBCONNECTION OBJECT HERE.
return Convert.ToInt32(cmd.ExecuteScalar());
}
Now suppose you need a SQL JOIN:public int getBarValue() { string sql = "select bar.field1 from foo join bar on foo.key0 = bar.key0"; OleDbCommand cmd = new OleDbCommand(sql); // SET cmd.Connection TO A VALID OLEDBCONNECTION OBJECT HERE. return Convert.ToInt32(cmd.ExecuteScalar()); }So far so good. But what if you need to join three tables together? This should work, right?
public int getBazValue() { string sql = "select baz.field2 from foo join bar on foo.key0 = bar.key0 join baz on bar.key1 = baz.key1"; OleDbCommand cmd = new OleDbCommand(sql); // SET cmd.Connection TO A VALID OLEDBCONNECTION OBJECT HERE. return Convert.ToInt32(cmd.ExecuteScalar()); }Whoops. At runtime–not build time!–you get an error from OLEDB: “Syntax Error in JOIN Command.” I agree that there is nothing wrong with your command, in any sane SQL environment. Try nesting the JOIN statements in parentheses though:
public int getBazValue() { string sql = "select baz.field2 from (foo join bar on foo.key0 = bar.key0) join baz on bar.key1 = baz.key1"; OleDbCommand cmd = new OleDbCommand(sql); // SET cmd.Connection TO A VALID OLEDBCONNECTION OBJECT HERE. return Convert.ToInt32(cmd.ExecuteScalar()); }In other words, Access SQL doesn’t accept three-way JOINs. But it likes nested two-way JOINs. I have successfully joined about six tables at one time with this technique.
Twitter Trackbacks for Programming with Access? Know this about JOIN. « Critical Results [criticalresults.com] on Topsy.com
October 19, 2009 @ 4:57 pm
[…] Programming with Access? Know this about JOIN. « Critical Results blog.criticalresults.com/2009/10/19/access-oledb-tips-1 – view page – cached If you’ve ever implemented a small-scale WinForms .NET project with modest database needs, you’ve probably been tempted to keep it “simple” by using Microsoft Acess (.mdb) files. — From the page […]
Programming with Access? Know this about “No value given for one or more required parameters” « Critical Results
October 26, 2009 @ 9:19 am
[…] Today, it’s more about programming .NET with an Access database. Last time, I explained the unique way Access handles JOIN. This time, let’s talk about a syntax error that masquerades as a missing […]
Programming with Access? Know this about column names « Critical Results
November 3, 2009 @ 9:40 am
[…] week, I offered help with a confusing syntax error. Before that, I demonstrated the unique way Access handles JOIN. Today I have a tip on addressing poorly named columns in your code.Suppose someone hands you an […]
Access: Why not? « Critical Results
December 14, 2009 @ 2:06 pm
[…] do kind of what you want when you’re programming with .NET. There was this one about multiple JOIN syntax. Then this one about “parameter” errors. And finally this one about weird column names. […]