So, here is my current working code.
fromDate = DateTime.Now()
toDate = DateTime.Now.AddHours(-tempDate)
myConnection.ConnectionString = connString
myConnection.Open()
Dim xHours As String = SearchOpenTextBox.Text
Dim Str = "SELECT EventID, Userid, CreateDate, ShortSummary, Shift FROM OpenEvents WHERE CreateDate" _
& " BETWEEN #" & fromDate & "# AND #" & toDate & "# ORDER BY CreateDate DESC"
Using cmd As New OleDbCommand(Str, myConnection)
dr = cmd.ExecuteReader()
End Using
If dr.HasRows Then
While dr.Read()
Dim evntId = dr("EventId").ToString
Dim createDate = dr("CreateDate").ToString
Dim shortSummary = dr("ShortSummary").ToString
Dim usrId = dr("Userid").ToString
Dim shift = dr("Shift").ToString
Dim lvi As New ListViewItem(evntId)
lvi.SubItems.Add(createDate)
lvi.SubItems.Add(shortSummary)
lvi.SubItems.Add(usrId)
lvi.SubItems.Add(shift)
OpenSearchListView.Items.Add(lvi)
End While
Now, this works fine, however I'd REALLY like to use parameters. But everything gives me a "Data type mismatch" trying to write into the Access DB I've got....
I've tried all kinds of different ways (to many to list) to try and use parameters, but none work. Suggestions?
Example of non-working code:
Dim Str = "SELECT EventID, Userid, CreateDate, ShortSummary, Shift FROM OpenEvents WHERE CreateDate" _
& " BETWEEN '#' + @fromDate + '#' AND '#' + @toDate + '#' ORDER BY CreateDate DESC"
Using cmd As New OleDbCommand(Str, myConnection)
cmd.parameters.addwithvalue("@fromDate", fromDate)
cmd.parameters.addwithvalue("@toDate", toDate)
dr = cmd.ExecuteReader()
End Using
Also tried this:
Dim Str = "SELECT EventID, Userid, CreateDate, ShortSummary, Shift FROM OpenEvents WHERE CreateDate" _
& " BETWEEN @fromDate AND @toDate ORDER BY CreateDate DESC"
Using cmd As New OleDbCommand(Str, myConnection)
cmd.parameters.addwithvalue("@fromDate", "#" & fromDate & "#")
cmd.parameters.addwithvalue("@toDate", "#" & toDate & "#")
dr = cmd.ExecuteReader()
End Using
If I get the formatting wrong, I get a syntax exception, so I know I've got the format right on both the above, it's the date data itself it's not liking. I've got a working LIKE %XXXX% search working with parameters formatted like the above. " LIKE '%' + @SearchforThis + '%'" So I'm about 95% sure access has no problems with the syntax itself. It's the date. But I've tried a number of .tostring() solutions, and nothing gets me anything other than data mismatch.
I would try by using an integer as an index to the row position and then do it like this. It is used for SQL Server but I doubt it will be much different for Access.
Dim intIndex as Integer
intIndex = dr.GetOrdinal("CreateDate")
If Not dr.IsDBNull(intIndex) Then Dim evntDate As DateTime = dr.GetDateTime(intIndex)
Hope this helps.
I figured it out, I could never find this on google, so I'm just going to throw it out there in case anyone else has the same question. I was just thinking about it and it hit me that maybe you don't need the #'s in a parameter query. That just maybe access was smart enough to fill in the blanks. Turns out I was right. If you use strings for the query, you have to have the #'s, however parameters... you don't. Code that works follows...
fromDate = DateTime.Now()
toDate = DateTime.Now.AddHours(-tempDate)
myConnection.ConnectionString = connString
myConnection.Open()
Dim xHours As String = SearchOpenTextBox.Text
Dim Str = "SELECT EventID, Userid, CreateDate, ShortSummary, Shift FROM OpenEvents WHERE CreateDate" _
& " BETWEEN @fromDate AND @toDate ORDER BY CreateDate DESC"
Using cmd As New OleDbCommand(Str, myConnection)
cmd.Parameters.AddWithValue("@fromDate", fromDate.ToString("g"))
cmd.Parameters.AddWithValue("@toDate", toDate.ToString("g"))
dr = cmd.ExecuteReader()
End Using
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.