views:

201

answers:

2

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();
+5  A: 

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";
Mark Byers
it still says i have an error in the from statement
simon
@simon : Can you post the full error message please?
Mark Byers
Syntax error in FROM clause.
simon
+2  A: 

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
Remou
@Remou now i'm past that problem so thank you! But there is a new problem in here that happens at the line while(reader.Read()){here's the mistake}-Specified cast is not valid.
simon