如何更新MS Access数据库(vb.net) [英] How to update MS Access Database (vb.net)

查看:148
本文介绍了如何更新MS Access数据库(vb.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为实习项目创建了日记程序,并且正在使用MS-Access数据库.我正在 VB.net 中编程.现在,我正在尝试使其能够更新"日记,这意味着他们单击日历日期,并且如果该日期有日历,则将其带入该日记.如果他们在该日期有一个,则显示该日期的标题和日记文本条目.我要这样做,以便当他们单击更新按钮时,他们对日志所做的任何更改(编辑文本框字段)也都在数据库中更改了.这就是我到目前为止所拥有的

I am created a journal program for an internship project and I am using a MS-Access Database. I am programming in VB.net. Now, I am trying to make it so that they can "Update" their journals, meaning that they click on their calendar date and it brings them to that journal if they have one for that date. If they have one for that date then it shows the title and journal text entry for that date. I want to make it so that any changes they have made to the journal (editing the textbox fields) are also changed in the database when they click the update button. Here's what i have so far

    Private Sub COE_JournalBtn_Click(sender As System.Object, e As System.EventArgs) Handles COE_JournalBtn.Click

    If DateTimePicker1.Value <> Nothing Then

        If TitleTxt.Text <> "" Then

            If JournalTextRtxt.Text <> "" Then

                myConnection.Open()

                Dim DatePicked As String = DateTimePicker1.Value
                Dim cmd As OleDbCommand
                Dim str As String

                Try

                    MyJournalTitle = TitleTxt.Text
                    MyJournalText = JournalTextRtxt.Text

                    str = "UPDATE Journals SET JournalTitle='" & MyJournalTitle & "', JournalText='" & MyJournalText & "' WHERE JournalDate=" & DatePicked

                    cmd = New OleDbCommand(str, myConnection)

                    cmd.ExecuteNonQuery()

                    myConnection.Close()

                Catch ex As Exception

                    MessageBox.Show("There was an error processing your request. Please try again." & vbCrLf & vbCrLf & _
                                    "Original Error:" & vbCrLf & vbCrLf & ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

                    myConnection.Close()

                End Try

                myConnection.Close()

            End If

        End If

    End If

End Sub

现在我自己的更新字符串是

Now my update string by itself is

"UPDATE Journals SET JournalTitle='" & MyJournalTitle & "', JournalText='" & MyJournalText & "' WHERE JournalDate=" & DatePicked

现在,发生的一切绝对没有.没有错误框出现.没有出现消息框.该程序不会冻结.并且数据库保持不变.我究竟做错了什么?我的代码中是否有错误或缺少内容?请帮助我,因为我真的很想弄清楚这一点,而且我一直在VB.net中寻找解决方案,却找不到适合我的解决方案,因为我正在使用MS-Access和NOT SQL.

Now, what happens, is absolutely nothing. No errorboxes come up. No messageboxes appear. The program doesn't freeze. And the database remains unchanged. What am I doing wrong? Is there an error in my code or something missing? Please help me because I really want to figure this out and i've been looking everywhere for a solution in VB.net and cannot find one that applies to me being that I am using MS-Access and NOT SQL.

预先感谢, 理查德·保利切利

Thanks in advance, Richard Paulicelli

推荐答案

您的UPDATE语句的这一部分可能有问题:

You may have a problem with this part of your UPDATE statement:

"' WHERE JournalDate=" & DatePicked

如果日记帐"字段JournalDate是日期/时间"数据类型,请在日期字符串值周围加上访问"日期定界符(#).

If the Journals field, JournalDate, is Date/Time data type, surround the date string value with Access date delimiter characters (#).

"' WHERE JournalDate=#" & DatePicked & "#"

您还可以将日期字符串转换为yyyy-mm-dd格式,以避免基于本地错误地解释日期文字.

You can also convert your date string to yyyy-mm-dd format to avoid misinterpreting the date literal based on local.

我同意为此使用参数查询的建议.我只是想帮助您了解为什么原始尝试可能会失败.

I agree with the suggestions to use a parameter query for this instead. I'm just trying to help you understand why the original attempt may have failed.

这篇关于如何更新MS Access数据库(vb.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆