无法将日期转换为字符串并传递给SQL

时间:2017-11-13 17:01:49

标签: vb.net

我收到以下错误消息:

  

System.InvalidCastException:从字符串&#34转换;从[tbl_FBNK_Limit_Hi"中选择*输入'整数'无效。

我的设置非常简单。以下是所有代码:

Imports System.Data.OleDb

Public Class Form1

    'Change Path Here:
    Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=path_here.accdb"
    Dim MyConn As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim ds As DataSet
    Dim tables As DataTableCollection
    Dim source1 As New BindingSource
    Dim SqlStr As String
    Dim startDate As String
    Dim endDate As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MyConn = New OleDbConnection
        MyConn.ConnectionString = connString
        ds = New DataSet
        tables = ds.Tables

        Dim startDate As DateTime = DateTimePicker1.Value.ToString("MM/dd/yyyy")
        Dim endDate As DateTime = DateTimePicker2.Value.ToString("MM/dd/yyyy")

        SqlStr = "select * from [tbl_Hist_Current] where AsOfDate >= #" & startDate & "# and AsOfDate <=#" & endDate & "#"

        da = New OleDbDataAdapter(SqlStr, MyConn)
        da.Fill(SqlStr, "tbl_Limit_Hist_Current")
        Dim view As New DataView(tables(0))
        source1.DataSource = view
        DataGridView1.DataSource = view
    End Sub    
End Class

当我将SqlStr复制/粘贴到Access中时,它可以正常工作,因此它必须与处理日期或处理日期的方式相关。这里有人可以给我一个正确方向的推动吗?

Private Sub LoadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim connectionString As String = "Data Source=server_name;Initial Catalog=database_name;Integrated Security=True"
    Dim SqlStr As String
    Dim startDate As DateTime = DateTimePicker1.Value.ToString("MM/dd/yyyy")
    Dim endDate As DateTime = DateTimePicker2.Value.ToString("MM/dd/yyyy")

    SqlStr = "Select Field1, Field2, Field3, AsOfDate FROM [TBL_DATA_HIST] where AsOfDate >= '" & startDate & "' and AsOfDate <='" & endDate & "'"

    Dim connection As New SqlConnection(connectionString)
    Dim dataadapter As New SqlDataAdapter(SqlStr, connection)
    Dim ds As New DataSet()
    connection.Open()
    dataadapter.Fill(ds, "TBL_DATA_HIST")
    connection.Close()
    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "TBL_DATA_HIST"

    ' Count rows in DG
    Dim int As Integer
    int = DataGridView1.Rows.Count()
    TextBox1.Text = int

End Sub

1 个答案:

答案 0 :(得分:3)

您可以使用parameter placeholders

使用以下解决方案
'Change Path Here:
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=path_here.accdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
Dim SqlStr As String
Dim startDate As String
Dim endDate As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MyConn = New OleDbConnection
    MyConn.ConnectionString = connString

    Dim startDate As DateTime = DateTimePicker1.Value
    Dim endDate As DateTime = DateTimePicker2.Value

    SqlStr = "select * from [tbl_Limit_Hist_Current] where AsOfDate >= ? and AsOfDate <= ?"

    da = New OleDbDataAdapter(SqlStr, MyConn)
    Dim selectCMD As New OleDbCommand(SqlStr, MyConn)
    da.SelectCommand = selectCMD

    selectCMD.Parameters.Add("@AsOfDate1", OleDbType.Date).Value = startDate
    selectCMD.Parameters.Add("@AsOfDate2", OleDbType.Date).Value = endDate

    ds = New DataSet
    da.Fill(ds, "tbl_Limit_Hist_Current")
    tables = ds.Tables
    Dim view As New DataView(tables(0))
    source1.DataSource = view
    DataGridView1.DataSource = view
End Sub

您还应该删除ToString("MM/dd/yyyy")值上的DateTimePicker。在我的系统上(默认为德语设置 - dd.mm.yyyy日期格式)我收到如下错误消息:

  

将字符串11.13.2017无效转换为类型日期 - 从德语翻译

Joel Coehoorn也mentioned in the comments,这是一种性能浪费,在某些情况下是一个安全问题。