Вставка даты в Oracle с помощью Vb.net - PullRequest
0 голосов
/ 10 декабря 2011

Я получаю ошибку

Строка не была распознана как действительный DateTime.

Я хочу знать точные параметры для вставки в Oracle. Пожалуйста, помогите мне сделать это

Мой код - ниже, чтобы вставить

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 голосов
/ 11 декабря 2011

Если вы используете параметры, вам не нужно ничего делать, кроме как присвоить параметру значение даты. Драйвер 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...