how to check time values in databasehttp://forums.asp.net/t/1744678.aspx/1?how+to+check+time+values+in+databaseSat, 03 Dec 2011 23:48:30 -050017446784707099http://forums.asp.net/p/1744678/4707099.aspx/1?how+to+check+time+values+in+databasehow to check time values in database <p>Hi<br> I'm working on a project that require to check for times of university courses so there is no clash between courses</p> <p>this is sample of database.</p> <p>the constraint of these courses is &quot;Morning&quot; &gt;&gt;&gt; this mean that End_time must be &lt;= 12:00pm<br> now how to access database and check if:<br> 1- End_time &lt;= 12:00pm (it's match the constraint)<br> 2- check for days if different no clash<br> 3- if same days &gt; check for time (courseB Start_time must be &gt;= courseA End_time</p> <p>* I need to emplement a button to do all these checking using C#</p> <table border="1" cellpadding="0" cellspacing="0" height="47" width="582"> <tbody> <tr> <td valign="top" width="148"> <p>&nbsp;course name</p> </td> <td valign="top" width="148"> <p>&nbsp;Start_time</p> </td> <td valign="top" width="148"> <p>&nbsp;End_time</p> </td> <td valign="top" width="148"> <p>&nbsp;day</p> </td> </tr> <tr> <td valign="top" width="148"> <p>&nbsp;A</p> </td> <td valign="top" width="148"> <p>&nbsp;8:30</p> </td> <td valign="top" width="148"> <p>10:30</p> </td> <td valign="top" width="148"> <p>&nbsp;sun</p> </td> </tr> <tr> <td valign="top" width="148"> <p>&nbsp;B</p> </td> <td valign="top" width="148"> <p>&nbsp;9:30</p> </td> <td valign="top" width="148"> <p>&nbsp;10:30</p> </td> <td valign="top" width="148"> <p>&nbsp;sun</p> </td> </tr> </tbody> </table> 2011-11-29T19:32:53-05:004709474http://forums.asp.net/p/1744678/4709474.aspx/1?Re+how+to+check+time+values+in+databaseRe: how to check time values in database <p></p> <blockquote><span class="icon-blockquote"></span> <h4>speed1</h4> 1- End_time &lt;= 12:00pm (it's match the constraint)<br> </blockquote> <p></p> <p>Hello</p> <p>For the first checkingI think you can read the StartTime and EndTime to check whether the time is smaller than 12:00Here's my sample for checking</p> <pre style="background:white; color:black; font-family:NSimSun">&nbsp;<span class="comment" style="color:green">//Suppose&nbsp;this&nbsp;is&nbsp;the&nbsp;read-out&nbsp;values</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color:blue">string</span>&nbsp;<span class="identifier">s</span>&nbsp;<span class="operator">=</span>&nbsp;<span class="string" style="color:#a31515">&quot;13:00&quot;</span>;<br> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color:green">//Convert&nbsp;to&nbsp;timespan</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="User Types(Value Types)" style="color:#2b91af">TimeSpan</span>&nbsp;<span class="identifier">t</span>&nbsp;<span class="operator">=</span>&nbsp;<span class="User Types(Value Types)" style="color:#2b91af">TimeSpan</span><span class="operator">.</span><span class="identifier">Parse</span>(<span class="identifier">s</span>);<br> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color:blue">if</span>&nbsp;(<span class="identifier">t</span>&nbsp;<span class="operator">&lt;=</span>&nbsp;<span class="User Types(Value Types)" style="color:#2b91af">TimeSpan</span><span class="operator">.</span><span class="identifier">Parse</span>(<span class="string" style="color:#a31515">&quot;12:00&quot;</span>))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="User Types" style="color:#2b91af">Console</span><span class="operator">.</span><span class="identifier">WriteLine</span>(<span class="string" style="color:#a31515">&quot;OK&quot;</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color:blue">else</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="User Types" style="color:#2b91af">Console</span><span class="operator">.</span><span class="identifier">WriteLine</span>(<span class="string" style="color:#a31515">&quot;Time&nbsp;is&nbsp;overdue!&quot;</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</pre> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>speed1</h4> check for days if different no clash<br> </blockquote> <p></p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>speed1</h4> 3- if same days &gt; check for time (courseB Start_time must be &gt;= courseA End_time</blockquote> <p></p> <p>In fact you can do this</p> <p><strong>Select count(*) from xxx where StartTime=XXX and EndTime=YYY and day=zzz</strong></p> <p><strong>If the result &gt;1this means you must be conflictedYou should either adjust the &quot;day&quot; or the startime</strong></p> <p>&nbsp;</p> 2011-12-01T01:53:05-05:004713332http://forums.asp.net/p/1744678/4713332.aspx/1?Re+how+to+check+time+values+in+databaseRe: how to check time values in database <p><br> Hi<br> Do I have to use &quot;<span class="code-keyword">using</span> System.Data.SqlClient;&quot; and how to access the data base and read values<br> I have to access database and read value of time such as Start_time1 = 8:00am End_time1 = 10:00am<br> and then compare another start time with it, so if Start_time2 = 9:00 it must detect it <br> <br> </p> 2011-12-03T07:01:34-05:004714026http://forums.asp.net/p/1744678/4714026.aspx/1?Re+how+to+check+time+values+in+databaseRe: how to check time values in database <p></p> <blockquote><span class="icon-blockquote"></span> <h4>speed1</h4> Do I have to use &quot;<span class="code-keyword">using</span> System.Data.SqlClient;&quot;</blockquote> <p></p> <p>YesIt's a must if you want to use it to read out data contents from db</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>speed1</h4> how to access the data base and read values</blockquote> <p></p> <p>Just use SqlDataAdapter and Fill method to read into DataTableand do comparationFor more about this usageplease see</p> <p><a href="http://msdn.microsoft.com/en-us/library/905keexk.aspx">http://msdn.microsoft.com/en-us/library/905keexk.aspx</a></p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>speed1</h4> have to access database and read value of time such as Start_time1 = 8:00am End_time1 = 10:00am<br> and then compare another start time with it, so if Start_time2 = 9:00 it must detect it <br> </blockquote> <p></p> <p>WellI think you should change your column's type as DateTime by using its constructor to assign yearmonthdayhourminute and secondsThen do a cast by using DateTime.Parse(Your DateTime) and use its property called &quot;TimeOfDay&quot; to do comparation within a foreach loop</p> <p>Sample codes similar like this</p> <p>foreach(DataRow row in DataTable.Rows)<br> {<br> &nbsp;&nbsp;&nbsp; TimeSpan ts = DateTime.Parse(row[&quot;TimeColumn&quot;].ToString()).TimeOfDay;<br> &nbsp;&nbsp;&nbsp; //Here do comparation<br> }</p> 2011-12-03T23:48:30-05:00