操作数类型冲突:日期与int

时间:2016-01-10 14:53:34

标签: sql-server vb.net

我对以下代码有困难。我认为这取决于我的sql语句。 我收到错误:

  

操作数类型冲突:日期与int

不兼容
    Dim strSql5 As String = "SELECT  * FROM dbo.ontledings where plaasblok = '" & plaasblokparsversoeke & "' and analisedatum  = " & laastedatum.Date
    MsgBox(strSql5)

    Dim dtb5 As New DataTable
    dtb5.Clear()

    Using cnn As New SqlConnection("Data Source=GIDEON-E-LAPTOP\SQLEXPRESS2014;Initial Catalog=SkeduleringDatabasis;Integrated Security=True")
        cnn.Open()
        Using dad5 As New SqlDataAdapter(strSql5, cnn)
            dad5.Fill(dtb5)
        End Using
        cnn.Close()
    End Using


    Dim laasteontleding As Decimal = dtb5.Rows(0)("suiker")

我从下面的代码中得到了laastedatum:

    Dim laastedatum As Date = dtb4.Rows(0)("last")

非常感谢任何帮助。

问候

1 个答案:

答案 0 :(得分:0)

不要使用SELECT *,请使用SELECT字段列表。然后使用Field(Of T)获取强类型的日期信息。始终使用Option Strict On。

从Microsoft NorthWind数据库订单表中读取订单数据的示例,请注意字段检索。

O(n k^2)

获取第一个日期

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim EmployeeId As Integer = 2
        Dim dt As New DataTable

        Using cn As New SqlClient.SqlConnection With {.ConnectionString = My.Settings.ConnectionString}
            Using cmd As New SqlClient.SqlCommand With {.Connection = cn}
                cmd.CommandText =
                    <SQL>
                    SELECT [OrderID]
                          ,[CustomerID]
                          ,[EmployeeID]
                          ,[OrderDate]
                          ,[ShipCountry]
                      FROM [NORTHWND.MDF].[dbo].[Orders]
                      WHERE [EmployeeID] = @EmployeeID
                      ORDER BY CustomerId  
                    </SQL>.Value

                cmd.Parameters.AddWithValue("@EmployeeID", EmployeeId)

                cn.Open()

                dt.Load(cmd.ExecuteReader)

                For Each row As DataRow In dt.Rows
                    Console.WriteLine("id: {1} date: {0}",
                                      row.Field(Of Date)("OrderDate").ToShortDateString,
                                      row.Field(Of String)("CustomerId"))
                Next
            End Using
        End Using
    End Sub
End Class