select statement in C#
Where's the problem in my code ?
string constr = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=C:\\Users\\Simon\\Desktop\\test5\\test5\\test5\\save.mdb";
OleDbConnection conn = new OleDbConnection(constr);
string sql = "SELECT users.user_name,naziv,obroki_save.开发者_Go百科datum"
+ "FROM zivila JOIN obroki_save ON zivila.ID=obroki_save.ID_zivila"
+ "JOIN users ON obroki_save.ID_uporabnika=users.ID";
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader reader;
reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.Write(reader.GetString(0).ToString() + " ,");
Console.Write(reader.GetString(1).ToString() + " ,");
Console.WriteLine("");
}
reader.Close();
conn.Close();
If you run it in the debugger you can see the problem:
SELECT users.user_name, naziv,obroki_save.datumFROM zivila JOIN obroki_save ...
^^^^
You're missing spaces in your SQL. It should be this:
string sql = "SELECT users.user_name,naziv,obroki_save.datum " // << here
+ "FROM zivila JOIN obroki_save ON zivila.ID=obroki_save.ID_zivila " // << here
+ "JOIN users ON obroki_save.ID_uporabnika=users.ID";
This is Access, so you need parentheses and you need to pick a JOIN type. LEFT and INNER are the usual choices, so:
SELECT users.user_name,naziv,obroki_save.datum
FROM (zivila
LEFT JOIN obroki_save ON zivila.ID=obroki_save.ID_zivila)
LEFT JOIN users ON obroki_save.ID_uporabnika=users.ID
精彩评论