c# - quering a database in visual studio -


i use local access database in c# project. need develop complex sql query, i'm bit stuck. table structure is:

student: (pk)tagid  studentid (fk)courseid 4855755    huj564334    25 4534664    red231232    33  course (pk)courseid coursename 25           computer science 33           biology  courseid-moduleid (fk)courseid (fk)moduleid 25              cmp2343 25              cmp3456 33              bio3422 33              bio2217  module (pk)moduleid modulename cmp2343      networking cmp3456      databases bio3422      human body bio2217      genetics  moduleid-sessionid (fk)moduleid (fk)sessionid cmp2343      1acmp2343 cmp2343      2acmp2343  cmp3456      1acmp3456 cmp3456      2acmp3456 bio3422      1abio3422       bio3422      2abio3422 bio2217      1abio2217       bio2217      2abio2217   session (pk)sessionid  sessiondate      sessiontimestart sessiontimeend 1acmp2343      09/05/2013          12:00         14:00 pm 2acmp2343      05/05/2013          09:00         11:00 pm 1acmp3456      15/05/2013          12:00         13:00 pm 2acmp3456      01/05/2013          10:00         13:00 pm 1abio3422      30/04/2013          11:00         13:00 pm 2abio3422      01/04/2013          14:00         16:00 pm 1abio2217      12/05/2013          16:00         18:00 pm 2abio2217      03/05/2013          12:00         14:00 pm     attendance (fk)tagid (fk)sessionid scanningtime 

i query find "current" sessionid (by looking date , time of session, saved in db) given tagid, , display tagid , sessionid in attendance table, along time of scanning.

i use folowing method set connection db, , display data in datagridview2:

public void setsql()         {             string connstr = "provider=microsoft.ace.oledb.12.0;data source=c:\\users\\jasper\\desktop\\autoreg\\autoreg\\autoreg.accdb;";              oledbconnection myconn = new oledbconnection(connstr);             myconn.open();              dataset ds = new dataset();              //query ask             string query = @"select s.tagid, se.sessionid, " + "'" +             datetime.now.tostring("mm/dd/yy hh:mm:ss tt") + @"' scanningtime                               student s,                             courseid-moduleid cm,                             moduleid-sessionid ms,                             session se                              1=1                              , s.tagid = 4820427                             , s.courseid = cm.courseid                             , ms.moduleid = cm.moduleid                             , ms.sessionid = se.sessionid                             , se.sessiondate = date();";              oledbcommand command = new oledbcommand(query, myconn);             oledbdataadapter adapter = new oledbdataadapter(command);                  adapter.fill(ds);                 datagridview2.datasource = ds.tables[0];                 myconn.close();          } 

when execute above code, error:

an unhandled exception of type 'system.data.oledb.oledbexception' occurred in system.data.dll additional information: ierrorinfo.getdescription failed e_fail(0x80004005). 

i sure method structure ok can run , display simple query, such as: "select * student";

i have managed let query work. have resolved adding square brackets around table names. first, session word reserved keyword in ms-access jet sql, need encapsulate name in square brackets. weird part need add square brackets around courseid-moduleid , moduleid-sessionid tables. minus character not accepted ms-access - oledb engine, infact if try simple "select * courseid-moduleid" syntax error disappears if put square brackets around table name

string query = @"select s.tagid, se.sessionid, '" +                 datetime.now.tostring("mm/dd/yy hh:mm:ss tt") +                 "' scanningtime " +                 "from (((student s " +                 " left join [courseid-moduleid] cm on s.courseid = cm.courseid) " +                 " left join [moduleid-sessionid] ms on ms.moduleid = cm.moduleid) " +                 " left join [session] se on ms.sessionid = se.sessionid) " +                 "where s.tagid = 4820427 , se.sessiondate = date()"; 

using access interface square bracket automatically added query text , error not visible.

as side note, se.sessiondata = date() remove result because there no sessiondate equals current date.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -