c# - No errors or results when executing simple query on SQL Server database -
this first approach sql server. have exported access db sql server , want use in application. have added new sql db c# project , replaced oledb
sql
. unable execute queries working local db in access.
query:
string query = @"select sessionid, semestera, semesterb, roomid, sessiondate, sessiontimestart, sessiontimeend" + " [session] " + " roomid = @roomid " + " , sessiondate = getdate() ";
i have replaced date()
getdate()
instructed vs error, query not produce result (should return 1 record, access db does)
my roomselect form code:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.data.sqlclient; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace autoreg { public partial class roomselect : form { datatable queryresult = new datatable(); public string roomid; roomactivesession roomactivesessionform = new roomactivesession(); public roomselect() { initializecomponent(); } private void button1_click(object sender, eventargs e) { switch (listbox1.selecteditem.tostring()) { case "mb0302": roomid = listbox1.selecteditem.tostring(); roomquery(); break; case "mc1001": roomid = listbox1.selecteditem.tostring(); roomquery(); break; case "mc3203": roomid = listbox1.selecteditem.tostring(); roomquery(); break; case "mc3204": roomid = listbox1.selecteditem.tostring(); roomquery(); break; } } public void roomquery() { string connstr = "data source=duzy;initial catalog=autoregsql;integrated security=true"; sqlconnection myconn = new sqlconnection(connstr); myconn.open(); //sql query todays sessions given roomid string query = @"select sessionid, semestera, semesterb, roomid, sessiondate, sessiontimestart, sessiontimeend" + " [session] " + " roomid = @roomid " + " , sessiondate = getdate() "; sqlcommand command = new sqlcommand(query, myconn); command.parameters.add("roomid", sqldbtype.char).value = roomid; sqldataadapter adapter = new sqldataadapter(command); adapter.fill(queryresult); if (queryresult.rows.count == 0) { messagebox.show("no active sessions today given room number"); myconn.close(); } else { roomactivesessionform.setdatasouce(queryresult); this.hide(); roomactivesessionform.showdialog(); myconn.close(); } } } }
when run program, receive message "no active sessions today given room number" should executed when there no results query, know fact, should return 1 record)
the function getdate()
returns datetime
. try converting date:
and sessiondate = cast(getdate() date)
the time component problem -- preventing match between date , datetime.
Comments
Post a Comment