如何在vb.net中更新数据库日期/时间字段?

时间:2015-11-25 00:16:55

标签: vb.net

我有一个更新按钮,将更新我的数据库表,似乎我的datepicker是问题,我尝试解析它但没有工作我最近的尝试是这....(下面)....但不是解析到日期...它说错误将字符串转换为类型整数。嗯,问题很明显,但我应用的解析方法不起作用。感谢

Try

        Dim RowsAffected As Integer = 0

        con = New OleDbConnection(cs)

        con.Open()


        Dim cq As String = "UPDATE StockItem SET StockedDate=@val1, ItemName=@val2, ItemSize=@val3, Quantity=@val4 WHERE StockedDate=@find1 AND ItemName=@find2 AND ItemSize=@find3 AND Quantity=@find4;"


        cmd = New OleDbCommand(cq)

        cmd.Connection = con

        ''SQL Where
        'type
        cmd.Parameters.Add(New OleDbParameter("@find1", System.Data.OleDb.OleDbType.Date, "StockedDate"))
        cmd.Parameters.Add(New OleDbParameter("@find2", System.Data.OleDb.OleDbType.VarChar, 50, "ItemName"))
        cmd.Parameters.Add(New OleDbParameter("@find3", System.Data.OleDb.OleDbType.VarChar, 50, "ItemSize"))
        cmd.Parameters.Add(New OleDbParameter("@find4", System.Data.OleDb.OleDbType.Integer, 10, "Quantity"))
        'value

        ''Dim iString As String = "2005-05-05 22:12 PM"
        ''Dim oDate As DateTime = DateTime.ParseExact(lbltemInDate.Text, "MM-dd-yyyy", Nothing)
        ''MsgBox(oDate.ToString())
        cmd.Parameters("@find1").Value = lbltemInDate.Value.Date
        'CDate(Format(lbltemInDate.Text, "MM/dd/yyyy"))
        cmd.Parameters("@find2").Value = Trim(lbltemInItem.Text)
        cmd.Parameters("@find3").Value = Trim(lbltemInItemSize.Text)
        cmd.Parameters("@find4").Value = Val(lbltemInQuantity.Text)



        ''SQL Set
        'type
        cmd.Parameters.Add(New OleDbParameter("@val1", System.Data.OleDb.OleDbType.Date, "StockedDate"))
        cmd.Parameters.Add(New OleDbParameter("@val2", System.Data.OleDb.OleDbType.VarChar, 50, "ItemName"))
        cmd.Parameters.Add(New OleDbParameter("@val3", System.Data.OleDb.OleDbType.VarChar, 50, "ItemSize"))
        cmd.Parameters.Add(New OleDbParameter("@val4", System.Data.OleDb.OleDbType.Integer, 10, "Quantity"))
        'value
        'cmd.Parameters("@val1").Value = CDate(Format(dtptemInDate.Value, "MM/dd/yyyy"))
        cmd.Parameters("@val1").Value = dtptemInDate.Value.Date
        cmd.Parameters("@val2").Value = Trim(cbxtemInNewItem.Text)
        cmd.Parameters("@val3").Value = Trim(cbxtemInNewItemSize.Text)
        cmd.Parameters("@val4").Value = Val(txttemInNewQuantity.Text)

        'cmd.Parameters.AddWithValue("val1", dtptemInDate.Value.Date)
        'cmd.Parameters.AddWithValue("val2", cbxtemInNewItem.Text)
        'cmd.Parameters.AddWithValue("val3", cbxtemInNewItemSize.Text)
        'cmd.Parameters.AddWithValue("val3", Val(txttemInNewQuantity.Text))

        RowsAffected = cmd.ExecuteNonQuery()
        If RowsAffected > 0 Then

            MessageBox.Show("Successfully Updated!", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
            iteminClearDetails()

        Else
            MessageBox.Show("No record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)

            If con.State = ConnectionState.Open Then

                con.Close()
            End If

            con.Close()
        End If

    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

这是重新排序的部分

''SQL Set
        'type
        cmd.Parameters.Add(New OleDbParameter("@val1", System.Data.OleDb.OleDbType.DBDate, "StockedDate"))
        cmd.Parameters.Add(New OleDbParameter("@val2", System.Data.OleDb.OleDbType.VarChar, 50, "ItemName"))
        cmd.Parameters.Add(New OleDbParameter("@val3", System.Data.OleDb.OleDbType.VarChar, 50, "ItemSize"))
        cmd.Parameters.Add(New OleDbParameter("@val4", System.Data.OleDb.OleDbType.Integer, 10, "Quantity"))

        cmd.Parameters.Add(New OleDbParameter("@find1", System.Data.OleDb.OleDbType.DBDate, "StockedDate"))
        cmd.Parameters.Add(New OleDbParameter("@find2", System.Data.OleDb.OleDbType.VarChar, 50, "ItemName"))
        cmd.Parameters.Add(New OleDbParameter("@find3", System.Data.OleDb.OleDbType.VarChar, 50, "ItemSize"))
        cmd.Parameters.Add(New OleDbParameter("@find4", System.Data.OleDb.OleDbType.Integer, 10, "Quantity"))

        'value
        'cmd.Parameters("@val1").Value = CDate(Format(dtptemInDate.Value, "MM/dd/yyyy"))
        cmd.Parameters("@val1").Value = dtptemInDate.Value.Date
        cmd.Parameters("@val2").Value = Trim(cbxtemInNewItem.Text)
        cmd.Parameters("@val3").Value = Trim(cbxtemInNewItemSize.Text)
        cmd.Parameters("@val4").Value = Val(txttemInNewQuantity.Text)


        ''SQL Where
        'type
                   'value

        ''Dim iString As String = "2005-05-05 22:12 PM"
        ''Dim oDate As DateTime = DateTime.ParseExact(lbltemInDate.Text, "MM-dd-yyyy", Nothing)
        ''MsgBox(oDate.ToString())
        Dim date1 As Date
        date1 = Date.Parse(Format("MM/dd/yyyy", lbltemInDate.Value))
        cmd.Parameters("@find1").Value = lbltemInDate.Value
        'CDate(Format(lbltemInDate.Text, "MM/dd/yyyy"))
        cmd.Parameters("@find2").Value = Trim(lbltemInItem.Text)
        cmd.Parameters("@find3").Value = Trim(lbltemInItemSize.Text)
        cmd.Parameters("@find4").Value = Val(lbltemInQuantity.Text)


        'cmd.Parameters.AddWithValue("val1", dtptemInDate.Value.Date)
        'cmd.Parameters.AddWithValue("val2", cbxtemInNewItem.Text)
        'cmd.Parameters.AddWithValue("val3", cbxtemInNewItemSize.Text)
        'cmd.Parameters.AddWithValue("val3", Val(txttemInNewQuantity.Text))

System.Data.OleDb.OleDbType.DBDate,“MM / dd / yyyy”,“StockedDate”......是否在.net上工作?

工作部分

''SQL Set
        'type
        cmd.Parameters.Add(New OleDbParameter("@val1", System.Data.OleDb.OleDbType.Date))
        cmd.Parameters.Add(New OleDbParameter("@val2", System.Data.OleDb.OleDbType.VarChar, 50, "ItemName"))
        cmd.Parameters.Add(New OleDbParameter("@val3", System.Data.OleDb.OleDbType.VarChar, 50, "ItemSize"))
        cmd.Parameters.Add(New OleDbParameter("@val4", System.Data.OleDb.OleDbType.Integer, 10, "Quantity"))

        cmd.Parameters.Add(New OleDbParameter("@find1", System.Data.OleDb.OleDbType.Date))
        cmd.Parameters.Add(New OleDbParameter("@find2", System.Data.OleDb.OleDbType.VarChar, 50, "ItemName"))
        cmd.Parameters.Add(New OleDbParameter("@find3", System.Data.OleDb.OleDbType.VarChar, 50, "ItemSize"))
        cmd.Parameters.Add(New OleDbParameter("@find4", System.Data.OleDb.OleDbType.Integer, 10, "Quantity"))

        'value
        'cmd.Parameters("@val1").Value = CDate(Format(dtptemInDate.Value, "MM/dd/yyyy"))
        cmd.Parameters("@val1").Value = dtptemInDate.Value.Date
        cmd.Parameters("@val2").Value = Trim(cbxtemInNewItem.Text)
        cmd.Parameters("@val3").Value = Trim(cbxtemInNewItemSize.Text)
        cmd.Parameters("@val4").Value = Val(txttemInNewQuantity.Text)


        ''SQL Where
        'type
                   'value

        ''Dim iString As String = "2005-05-05 22:12 PM"
        ''Dim oDate As DateTime = DateTime.ParseExact(lbltemInDate.Text, "MM-dd-yyyy", Nothing)
        ''MsgBox(oDate.ToString())
        Dim date1 As Date
        date1 = Date.Parse(Format("MM/dd/yyyy", lbltemInDate.Value))
        cmd.Parameters("@find1").Value = lbltemInDate.Value
        'CDate(Format(lbltemInDate.Text, "MM/dd/yyyy"))
        cmd.Parameters("@find2").Value = Trim(lbltemInItem.Text)
        cmd.Parameters("@find3").Value = Trim(lbltemInItemSize.Text)
        cmd.Parameters("@find4").Value = Val(lbltemInQuantity.Text)

感谢LarsTech和Plutonix的帮助

0 个答案:

没有答案