数据类型不匹配

时间:2018-09-03 10:16:37

标签: vb.net ms-access

您能用这种语法帮助我吗? 我有一张桌子(datapenjualan) 我想用此代码更新数据,但始终在ExecuteQuery中出现错误“数据类型不匹配” 在这里,我的数据库处于访问状态

Table Description

这里是我的代码 在主键“ Archive”中使用datetime来更新数据,使用1个组合框来说明选择存储,使用2个groupbox来显示ordner和lemari,然后再次在groupbox和datetime中使用组合框

Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click

    If storage.SelectedIndex = 1 Then
        str = "Update datapenjualan set BulanOrdner = '" & bulanordner.Text & "', TahunOrdner = '" & tahunordner.Text & "' Where Archive = '" & tanggalarchive.Text & "'"
        proses.ExecuteNonQuery(str)
        MsgBox("Data Has Been Saved", MessageBoxButtons.OK)
        Call bersih()
    Else
        If storage.SelectedIndex = 2 Then
            str = "Update datapenjualan set Lemari = '" & lemari.Text & "', BulanLemari = '" & bulanlemari.Text & "', TahunLemari = '" & tahunlemari.Text & "'Where Archive = '" & tanggalarchive.Text & "'"
            proses.ExecuteNonQuery(str)
            MsgBox("Data Has Been Saved", MessageBoxButtons.OK)
            Call bersih()
        End If
    End If
End Sub

Private Sub storage_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles storage.SelectedIndexChanged
    If storage.SelectedIndex = 1 Then
        groupordner.Visible = "True"
        grouplemari.Visible = "False"
    Else
        If storage.SelectedIndex = 2 Then
            groupordner.Visible = "False"
            grouplemari.Visible = "True"
        End If
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

主要问题是您要将日期格式的字符串传递给类型为DateTime的列(请记住,日期/时间值必须始终以DateTimeTimeSpan的形式传递视情况而定)。您应该使用参数(与Parameters.AddParameters.AddWithValue一起使用,并使用DateTime将日期格式的任何字符串转换为Convert.ToDateTime(),或者如果有特定日期,最好使用DateTime.ParseExact()文本框中的格式:

Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click

    If storage.SelectedIndex = 1 Then
        str = "Update datapenjualan set BulanOrdner = @BulanOrder, TahunOrdner = @TahunOrder Where Archive = @TanggalArchive"
        proses.Parameters.Add("@BulanOrder", OleDbType.VarChar).Value = bulanordner.Text
        proses.Parameters.Add("@TahunOrder", OleDbType.Date).Value = Convert.ToDateTime(tahunordner.Text) 
        ' or DateTime.ParseExact(tahunordner.Text, "[date_format_here]", CultureInfo.InvariantCulture)

        proses.Parameters.Add("@TanggalArchive", OleDbType.Date).Value = Convert.ToDateTime(tanggalarchive.Text) 
        ' or DateTime.ParseExact(tanggalarchive.Text, "[date_format_here]", CultureInfo.InvariantCulture)

        proses.ExecuteNonQuery(str)
        MsgBox("Data Has Been Saved", MessageBoxButtons.OK)
        Call bersih()
    Else
        If storage.SelectedIndex = 2 Then
            str = "Update datapenjualan set Lemari = @Lemari, BulanLemari = @BulanLemari, TahunLemari = @TahunLemari Where Archive = @TanggalArchive"
            proses.Parameters.Add("@Lemari", OleDbType.VarChar).Value = lemari.Text
            proses.Parameters.Add("@BulanLemari", OleDbType.VarChar).Value = bulanlemari.Text
            proses.Parameters.Add("@TahunLemari", OleDbType.Date).Value = Convert.ToDateTime(tahunlemari.Text)
            ' or DateTime.ParseExact(tahunlemari.Text, "[date_format_here]", CultureInfo.InvariantCulture)

            proses.Parameters.Add("@TanggalArchive", OleDbType.Date).Value = Convert.ToDateTime(tanggalarchive.Text)
            ' or DateTime.ParseExact(tanggalarchive.Text, "[date_format_here]", CultureInfo.InvariantCulture)

            proses.ExecuteNonQuery(str)
            MsgBox("Data Has Been Saved", MessageBoxButtons.OK)
            Call bersih()
        End If
    End If
End Sub

请注意,为了使查询语句正常工作,参数顺序应正确排列(首先定义最左边的参数,从左到右)。