Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi How can i get data from access with time(hour) condition?

What I have tried:

Dim TIME_C = TIME.Text  
'  text=  (09:00)

"SELECT * FROM RAPOR WHERE INVOICETIME > '"& TIME_C &"'
Posted
Updated 10-Mar-20 1:10am
Comments
Member 14588284 10-Mar-20 7:27am    
i changed my codes....

Dim dtTARIH As DateTime = TARIH.Value
Dim S1 = Strings.Left(SAAT.Text, 2)
Dim S2 = Strings.Right(SAAT.Text, 2)
Dim srg_1 = "SELECT TARIH,SAAT,FISNO,TUTAR FROM ARACTAKIP WHERE PLAKA='" & PLAKA.Text & "' AND TARIH = @ST AND LEFT(SAAT,2) >= " & S1 & " AND MID(SAAT,4,2) >= " & S2 & ""
Dim srg_2 = "SELECT TARIH,SAAT,FISNO,TUTAR FROM ARACTAKIP WHERE PLAKA='" & PLAKA.Text & "' AND TARIH > @ST "
Dim SRG = "SELECT * FROM (" & srg_1 & " UNION ALL " & srg_2 & ") ORDER BY TARIH,SAAT"
Dim SRG1 As New OleDbCommand(srg, cn)
SRG1.Parameters.AddWithValue("@ST", dtTARIH)

1 solution

First off, never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

The code you show won;t do that, but it's an indicator that the rest of your codee does, and that's very, very dangerous.

Then, don't use a string. Start by looking at exactly what you have stored in INVOICETIME, and what data type it is stored in. Ideally, it'll be a DATETIME or DATETIME2 column - if it isn't, then change your DB so it is; it'll make everything a whole load easier for everything you want to do with it past basic SELECT and INSERT operations.

Then use a DateTime value in your code:
VB
Dim now As DateTime = DateTime.Now
Dim limit As DateTime = now.AddHours(9 - now.Hour)
You can now pass that as a parameter to your SQL query, and compare the values directly.
 
Share this answer
 
Comments
Member 14588284 10-Mar-20 7:25am    
WHY u send same message all questions??
OriginalGriff 10-Mar-20 7:46am    
Because idiots don't listen, and continue to produce code that has major flaws.
And if they aren't told that it's stupid, dangerous, and lazy code then how are they to know that they need to fix it as a priority?
Member 14588284 10-Mar-20 8:50am    
u cant say anyone "idiot"

if you keep saying , it shows you are idiot
OriginalGriff 10-Mar-20 8:56am    
If someone persists in doing something dangerous, unnecessary, and that potentially risks personal data; if they won't listen no matter how often you tell them "don't do that!" and explain how to do it safely; if the "proper way" is more readable, maintainable, and secure than the lazy way they persist in; what else can you call them but idiots?

Well quite a lot of things, but most of them are unprintable...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900