简体   繁体   中英

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.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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM