使用Vb.net将日期插入Oracle

时间:2011-12-10 11:52:14

标签: vb.net oracle

我收到错误

  

字符串未被识别为有效的DateTime。

我想知道要插入Oracle的确切参数。请帮帮我这样做

我的代码是Bellow to Insert

Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        For Each gvRow As GridViewRow In GridView1.Rows
            'Create Controls
            Dim txtCardNo As TextBox = CType(gvRow.FindControl("txtCardNo"), TextBox)
            Dim ddlZone As DropDownList = CType(gvRow.FindControl("ddlZone"), DropDownList)
            Dim ddlVeh As DropDownList = CType(gvRow.FindControl("ddlVeh"), DropDownList)
            'Start declarations
            Dim ddlStartDate As DropDownList = CType(gvRow.FindControl("ddlStartDate"), DropDownList)
            Dim ddlStartHH As DropDownList = CType(gvRow.FindControl("ddlStartHH"), DropDownList)
            Dim ddlStartMM As DropDownList = CType(gvRow.FindControl("ddlStartMM"), DropDownList)
            'Entry Declaration
            Dim ddlEntryDate As DropDownList = CType(gvRow.FindControl("ddlEntryDate"), DropDownList)
            Dim ddlEntryHH As DropDownList = CType(gvRow.FindControl("ddlEntryHH"), DropDownList)
            Dim ddlEntryMM As DropDownList = CType(gvRow.FindControl("ddlEntryMM"), DropDownList)
            'Gross Declarations
            Dim ddlGrossdate As DropDownList = CType(gvRow.FindControl("ddlGrossdate"), DropDownList)
            Dim ddlGrossHH As DropDownList = CType(gvRow.FindControl("ddlGrossHH"), DropDownList)
            Dim ddlGrossMM As DropDownList = CType(gvRow.FindControl("ddlGrossMM"), DropDownList)
            'gross and Tare Wt
            Dim txtGrossWt As TextBox = CType(gvRow.FindControl("txtGrossWt"), TextBox)
            Dim txtTareWt As TextBox = CType(gvRow.FindControl("txtTareWt"), TextBox)
            'Tare Date declareations
            Dim ddlTareDate As DropDownList = CType(gvRow.FindControl("ddlTareDate"), DropDownList)
            Dim ddlTareHH As DropDownList = CType(gvRow.FindControl("ddlTareHH"), DropDownList)
            Dim ddlTareMM As DropDownList = CType(gvRow.FindControl("ddlTareMM"), DropDownList)
            'NetWt
            Dim lblNetWt As Label = CType(gvRow.FindControl("lblNetWt"), Label)
            'Quality Declarearions
            Dim txtFat As TextBox = CType(gvRow.FindControl("txtFat"), TextBox)
            Dim txtLR As TextBox = CType(gvRow.FindControl("txtLR"), TextBox)
            Dim txtSNF As TextBox = CType(gvRow.FindControl("txtSNF"), TextBox)
            Dim txtCheese As TextBox = CType(gvRow.FindControl("txtCheese"), TextBox)
            Dim txtDistance As TextBox = CType(gvRow.FindControl("txtDistance"), TextBox)
            'Create Connection
            Dim con = New OleDbConnection("Data Source=sml2; User ID=sfpl; Password=a; provider=OraOLEDB.Oracle")
            'Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseConnectionString2").ConnectionString)
            Dim cmd As OleDbCommand = New OleDbCommand("Insert into Temp (Card_No,Zone_No,VEh_cod,START_DTE,ARRIVAL_DTE,GROSS_WEIGHT,GROSS_WEIGHT_DATE,TARE_WEIGHT,TARE_WEIGHT_DATE,Q_FAT,Q_LR,Q_SNF,Q_CHES,DISTANCE) VALUES (:Card_No,:ZONE_NO,:VEH_COD,:START_DTE,:ARRIVAL_DTE,:GROSS_WEIGHT,:GROSS_WEIGHT_DATE,:TARE_WEIGHT,:TARE_WEIGHT_DATE,:Q_FAT,:Q_LR,:Q_SNF,:Q_CHES,:DISTANCE)", con)
            cmd.Parameters.Clear()
            cmd.Parameters.AddWithValue(":Card_No", txtCardNo.Text)
            cmd.Parameters.AddWithValue(":Zone_No", ddlZone.SelectedValue)
            cmd.Parameters.AddWithValue(":VEh_cod", ddlVeh.SelectedValue)
            ' Starting Date
            cmd.Parameters.Add(":START_DTE", (OleDb.OleDbType.[Date]))
            cmd.Parameters(":START_DTE").Value = DateTime.Now()
            cmd.Parameters(":START_DTE").Value = ddlStartDate.SelectedValue '& " " & ddlStartHH.SelectedValue & ":" & ddlStartMM.SelectedValue & ":00"
            'cmd.Parameters.AddWithValue(":START_DTE", ddlStartDate.SelectedValue)
            'Entry Date
            cmd.Parameters.Add(":ENTRY_DATE", (OleDb.OleDbType.[Date]))
            cmd.Parameters(":ENTRY_DATE").Value = DateTime.Now()
            cmd.Parameters(":ENTRY_DATE").Value = ddlEntryDate.SelectedValue '& " " & ddlEntryHH.SelectedValue & ":" & ddlEntryMM.SelectedValue & ":00"
            'cmd.Parameters.AddWithValue(":ARRIVAL_DTE", ddlEntryDate.SelectedValue)
            'Gross Wt
            cmd.Parameters.Add(":GROSS_WEIGHT_DATE", (OleDb.OleDbType.[Date]))
            cmd.Parameters(":GROSS_WEIGHT_DATE").Value = DateTime.Now()
            cmd.Parameters(":GROSS_WEIGHT_DATE").Value = ddlGrossdate.SelectedValue ' & " " & ddlGrossHH.SelectedValue & ":" & ddlGrossMM.SelectedValue & ":00"
            'cmd.Parameters.AddWithValue(":GROSS_WEIGHT_DATE", ddlGrossdate.SelectedValue)
            'Gross and Tare Wt
            cmd.Parameters.AddWithValue("GROSS_WEIGHT", txtGrossWt.Text)
            cmd.Parameters.AddWithValue("TARE_WEIGHT", txtTareWt.Text)

            'Tare Wt
            cmd.Parameters.Add(":TARE_WEIGHT_DATE", (OleDb.OleDbType.[Date]))
            cmd.Parameters(":TARE_WEIGHT_DATE").Value = DateTime.Now()
            cmd.Parameters(":TARE_WEIGHT_DATE").Value = ddlTareDate.SelectedValue '& " " & ddlTareHH.SelectedValue & ":" & ddlTareMM.SelectedValue & ":00"
            'cmd.Parameters.AddWithValue(":TARE_WEIGHT_DATE", ddlTareDate.SelectedValue)
            'Net Wt

            'Quality
            cmd.Parameters.AddWithValue("Q_FAT", txtFat.Text)
            cmd.Parameters.AddWithValue("Q_LR", txtLR.Text)
            cmd.Parameters.AddWithValue("Q_SNF", txtSNF.Text)
            cmd.Parameters.AddWithValue("Q_CHES", txtCheese.Text)
            'Distance
            cmd.Parameters.AddWithValue("DISTANCE", txtDistance.Text)

            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            Label1.Text = "All Records are Saved Successfully"
            btnSubmit.Text = "Clear Form for New Entry"
            Button1.Visible = True
            btnSubmit.Focus()
            btnInsert.Visible = True
            btnInsert.Enabled = False

        Next

    End Sub

1 个答案:

答案 0 :(得分:0)

如果使用参数,则除了将参数的日期值赋值之外,您不需要执行任何操作。 Oracle / OleDB驱动程序应该处理其余的事情。

但是,如果要生成直接SQL以执行,这是我们使用的方法:

''' <summary>
''' This method converts the specified date time into an ODBC formatted string, 
''' in the format yyyy-MM-dd HH:mm:ss.000
''' </summary>
''' <param name="theDateTime"></param>
''' <param name="fNullIfNotDate"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function DateToODBCString(ByVal theDateTime As Object, Optional ByVal fNullIfNotDate As Boolean = False) As String

    If IsDate(theDateTime) AndAlso Not DateIsMinValue(CDate(theDateTime)) Then
        Return "'" & CDate(theDateTime).ToString("yyyy-MM-dd HH:mm:ss.000") & "'"

    ElseIf fNullIfNotDate Then
        Return "Null"

    Else
        Return "''"

    End If

End Function

DateIsMinValue的定义如下:

Public Function DateIsMinValue(ByVal oDate As DateTime) As Boolean
    Return ((oDate = Date.MinValue) OrElse (oDate < #1/1/1753#))
End Function