Vb.net 我想在sqlite中使用两个文本框(textbox4.text和textbox5.text=variable)在两个日期之间进行查询
Dim query As String=从mytable中选择*,其中strftime“%Y-%m-%d”介于“TextBox4.text”和“TextBox5.text”之间 无法分配查询变量? Datagridview结果=空 信息:Datagridview日期格式=2018-01-01 17:42:24Vb.net 我想在sqlite中使用两个文本框(textbox4.text和textbox5.text=variable)在两个日期之间进行查询,vb.net,sqlite,Vb.net,Sqlite,Dim query As String=从mytable中选择*,其中strftime“%Y-%m-%d”介于“TextBox4.text”和“TextBox5.text”之间 无法分配查询变量? Datagridview结果=空 信息:Datagridview日期格式=2018-01-01 17:42:24 Dim btarihyil As String btarihyil = DateTimePicker2.Value.ToString.Substring(6, 4)
Dim btarihyil As String
btarihyil = DateTimePicker2.Value.ToString.Substring(6, 4)
Dim btarihay As String
btarihay = DateTimePicker2.Value.ToString.Substring(3, 2)
Dim btarihgun As String
btarihgun = DateTimePicker2.Value.ToString.Substring(0, 2)
TextBox4.Text = btarihyil & "-" & btarihay & "-" & btarihgun
Dim bttarihyil As String
bttarihyil = DateTimePicker3.Value.ToString.Substring(6, 4)
Dim bttarihay As String
bttarihay = DateTimePicker3.Value.ToString.Substring(3, 2)
Dim bttarihgun As String
bttarihgun = DateTimePicker3.Value.ToString.Substring(0, 2)
TextBox4.Text = btarihyil & "-" & btarihay & "-" & btarihgun
TextBox5.Text = bttarihyil & "-" & bttarihay & "-" & bttarihgun
Dim Yol As String = "Data Source=database1.s3db;version=3;new=False"
Using MyConn As New SQLiteConnection(Yol)
If (MyConn.State = ConnectionState.Closed) Then
MyConn.Open()
End If
Dim Sorgu As String = "select * from mytable where strftime('%Y-%m-%d',tarih) between 'TextBox4.text' and 'TextBox5.text' "
Using MyCmd As New SQLiteCommand(Sorgu, MyConn)
Dim Da As New SQLiteDataAdapter(MyCmd)
Dim Ds As New DataSet
Dim Dt As New DataTable
Ds.Reset()
Da.Fill(Ds)
Dt = Ds.Tables(0)
Dim Bs As New BindingSource With {.DataSource = Dt}
DataGridView1.DataSource = Bs
Bs.MoveLast()
MyConn.Close()
MyCmd.Dispose()
MyConn.Dispose()
End Using
End Using
End Sub
您正在查询中使用TextBox4.Text和TextBox5.Text。这不会将文本框的值传递给查询。因此,它将导致错误
您还需要使用参数化查询来避免语法错误。
另外,我不知道为什么要使用strftime函数。该函数仅用于格式化
遵循我使用参数化查询方法重新编写的代码
Dim Yol As String = "Data Source=database1.s3db;version=3;new=False"
Using MyConn As New SQLiteConnection(Yol)
If (MyConn.State = ConnectionState.Closed) Then
MyConn.Open()
End If
Dim Sorgu As String = "select * from mytable where tarih between @startDate and @endDate "
Using MyCmd As New SQLiteCommand(Sorgu, MyConn)
Dim startDate as new SQLiteParameter("@startDate")
startDate.Value = DateTimePicker2.Value
Dim endDate as new SQLiteParameter("@endDate")
endDate.Value = DateTimePicker3.Value
MyCmd.Parameters.Add(startDate)
MyCmd.Parameters.Add(endDate)
Dim Da As New SQLiteDataAdapter(MyCmd)
Dim Ds As New DataSet
Dim Dt As New DataTable
Ds.Reset()
Da.Fill(Ds)
Dt = Ds.Tables(0)
Dim Bs As New BindingSource With {.DataSource = Dt}
DataGridView1.DataSource = Bs
Bs.MoveLast()
MyConn.Close()
MyCmd.Dispose()
MyConn.Dispose()
End Using
End Using
这将帮助您解决问题。C代码在哪里?很抱歉我重新编辑了一个很长的错误。您在查询中使用的是文本字符串“TextBox4.text”,而不是文本框的值。使用SqliteParameters添加值。例如:Dim Sorgu As String=select*from mytable,其中strftime“%Y-%m-%d”,tarih介于values@firstdate和values@lastdateMyCmd.Parameters.AddWithValue@firstdate,TextBox4.Text MyCmd.Parameters。AddWithValue@lastdate,TextBox5.TextThank you Dim Sorgu As String=select*from Hareketler,其中strftime“%Y-%m-%d”,介于@firstdate和@lastdate worke之间。我使用strftime函数,因为datagridview data=2018-01-01 17:42:24。。。很乐意帮忙