Vb选择使用日期

时间:2014-06-09 09:19:34

标签: vb.net string sql-server-2008 smalldatetime

这是我的代码,我想从两个日期之间的数据库中选择数据:

          Dim Sql As String = "Select strCodSeccao,strAbrevTpDoc,strCodExercicio,strNumero From Mov_Venda_Cab where dtmdate between " & date1 & " and " & date2 & ";"

sqlserver数据库dtmdate字段是smalldatetime,这里是date1和date2的代码:

            Dim data1, data2 As DateTime
            data1 = DateTime.Parse(txtDate1.Text)
            data2 = DateTime.Parse(txtDate2.Text)

它说参数异常并没有执行select命令

提前谢谢 的 __________________________ / ____________________________________ / _______________

感谢Steve,我使用了你的代码,现在我可以选择将sql数据库组成另一个数据库,现在它可以正常工作

            Dim x As Integer = 0
            Dim temp1, temp2, temp3, temp4 As String


            Dim Sql As String = "Select strCodSeccao,strAbrevTpDoc,strCodExercicio,strNumero " & _
                 "From Mov_Venda_Cab where dtmdata between @d1 and @d2"


            Dim data1, data2 As DateTime
            data1 = DateTime.Parse(txtData1.Text)
            data2 = DateTime.Parse(txtData2.Text)

            data2 = data2.AddMinutes(0)
            data2 = data2.AddHours(0)
            data2 = data2.AddSeconds(0)

            data1 = data1.AddMinutes(0)
            data1 = data1.AddHours(0)
            data1 = data1.AddSeconds(0)


            Using con = New SqlConnection("Data Source=" & txtserv.Text & ";" & "Initial Catalog=" & txtBD.Text & ";" & "User ID=" & txtuser.Text & ";" & "Password=" & txtPass.Text & "")
                Using cmd = New SqlCommand(Sql, con)
                    con.Open()
                    cmd.Parameters.AddWithValue("@d1", data1)
                    cmd.Parameters.AddWithValue("@d2", data2)
                    Using reader = cmd.ExecuteReader()
                        While reader.Read()
                            Dim strCodSeccao = reader("strCodSeccao").ToString()
                            temp1 = reader.Item(x)
                            temp2 = reader.Item(x + 1)
                            temp3 = reader.Item(x + 2)
                            temp4 = reader.Item(x + 3)


                            Dim Con2 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Utilizador.Utilizador-PC\Documents\Visual Studio 2013\Projects\WindowsApplication1\WindowsApplication1\Doc_Vendas_Cab.mdb;Persist Security Info=True")
                            Con2.Open()
                            Dim Ole2 As String = "Insert into Mov_Venda_Cab values('" & temp1 & "','" & temp2 & "','" & temp3 & "','" & temp4 & "');"
                            Dim OledbCom2 As New OleDb.OleDbCommand(Ole2, Con2)

                            Try
                                OledbCom2.ExecuteNonQuery()
                            Catch Ex As Exception
                                MsgBox(Ex)
                            End Try
                            Con2.Close()
                        End While
                    End Using
                End Using
            End Using

1 个答案:

答案 0 :(得分:2)

您应该使用参数化查询并传递日期时间值

 Dim Sql As String = "Select strCodSeccao,strAbrevTpDoc,strCodExercicio,strNumero " & _
                     "From Mov_Venda_Cab where dtmdate between @d1 and @d2"


 Dim data1, data2 As DateTime
 data1 = DateTime.Parse(txtDate1.Text)
 data2 = DateTime.Parse(txtDate2.Text)
 data2 = data2.AddMinutes(1439)

 Using con = new SqlConnection("connection string here")
 Using cmd = new SqlCommand(Sql, con)
     con.Open()
     cmd.Parameters.AddWithValue("@d1", data1)
     cmd.Parameters.AddWithValue("@d2", data2)
     Using reader = cmd.ExecuteReader()
         While reader.Read() Then
             Dim strCodSeccao = reader("strCodSeccao").ToString()
             .....
         End While
     End Using
 End Using
 End Using

请注意,如果您的日期时间字段还包含设置小时/分钟/秒的值,我会在第二个日期添加1439分钟以包含最后一天。