将数据类型nvarchar转换为日期

时间:2015-12-05 03:42:35

标签: sql-server vb.net

我正在尝试执行一个存储过程,该过程将从我的DateTimePicker获取开始和结束时间,并检查车辆是否可用并返回包含这些数据集的数据集。当我在SQL Managment Studio中运行代码时,代码确实有效,因此我尝试在Visual Studio中使用VBA执行。

我计划稍后发布有关显示数据集的帮助,因为我不知道如何在代码中执行此操作并且很难找到好的示例但是现在我试图通过此错误消息。我已经返回并将格式更改为自定义格式,因此它与我的SQL数据库中的日期格式相匹配。我在datetimepickers上使用的自定义格式是yyyy-MM-dd。我已经在各个网站上尝试了几个建议但到目前为止没有运气通过这条消息。下面是我的存储过程中的代码以及可视化基本代码。

 CREATE PROC spVehicleAvailable
@RequestedStartDate Date,--DateTime selectedDate = dateTimePicker1.Value.Date
@RequestedEndDate Date--DateTime selectedDate = dateTimePicker2.Value.Date
AS
BEGIN
Select Vehicle.*
FROM Vehicle
        Where Vehicle.VehicleID NOT IN (
        SELECT VehicleID FROM LoanRequest
        WHERE @RequestedEndDate >= LoanRequest.StartDate 
        AND @RequestedStartDate <= LoanRequest.EndDate)  
        AND Available = 1   AND Scrap=0
    END;



Private Sub btnAvailable_Click(sender As Object, e As EventArgs) Handles btnAvailable.Click
        Try
            Dim sqlConnection1 As New SqlConnection("Data Source=GALE-PC1\SQLEXPRESS2012;Initial Catalog=VehicleCheckout;Integrated Security=True")
            Dim cmd As New SqlCommand
            cmd.CommandText = "spVehicleAvailable"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = sqlConnection1
            cmd.Parameters.AddWithValue("@RequestedStartDate", Date.ParseExact(dtpStartDate.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture))
            cmd.Parameters.AddWithValue("@RequestedEndDate", Date.ParseExact(dtpEndDate.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture))
            'new code to try adding info into datagridview
            Dim da As New SqlDataAdapter(cmd)
            Dim ds As New DataSet("Vehicles")  ' Not sure of this code
            da.SelectCommand.Parameters(0).Value = "Not-Sure" 'Not sure of this code
            da.Fill(ds)
            If Not ds Is Nothing Then
                MessageBox.Show("This Vehicle" & ds.Tables(0).Rows(0)("SomethngElse").ToString) 'Not sure of this code
            End If
            'end new code
            sqlConnection1.Open()
            cmd.ExecuteNonQuery()
            sqlConnection1.Close()
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

1 个答案:

答案 0 :(得分:0)

在中间窗口中执行此操作:Date.ParseExact(dtpStartDate.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture)时,结果是什么?