无法更新Access数据库。如何使这成为可能? (VB.NET) [英] Unable to update Access database. How to make this possible? (VB.NET)

查看:91
本文介绍了无法更新Access数据库。如何使这成为可能? (VB.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只有一个月的VB.NET。我遇到了这个问题,它不允许我将条目保存到数据库中。以前,没有问题,但是当我在数据库中添加另一列时问题就开始了。它在运行时保存数据,但是当我们重新启动程序时,没有数据。我使用MS Access 2010作为数据库。撤消更改为时已晚。我该怎么办呢?



以下是代码的一部分:

I am only a month old to VB.NET. I'm stuck with this problem which doesn't let me save the entry to the database. Previously, there was no problem, but the problem started when I added another column in the database. It saves the data on run-time,but when we restart the program, there is no data.I use MS Access 2010 as database. It is too late to Undo changes. How can I sort this out?

Here is part of the code:

Imports System.Security.Cryptography
Public NotInheritable Class Form1


Private TripleDes As New TripleDESCryptoServiceProvider
    Private Function TruncateHash(
    ByVal key As String,
    ByVal length As Integer) As Byte()

        Dim sha1 As New SHA1CryptoServiceProvider

        ' Hash the key.
        Dim keyBytes() As Byte =
            System.Text.Encoding.Unicode.GetBytes(key)
        Dim hash() As Byte = sha1.ComputeHash(keyBytes)

        ' Truncate or pad the hash.
        ReDim Preserve hash(length - 1)
        Return hash
    End Function
    Public Sub New(ByVal key As String)
        ' Initialize the crypto provider.
        InitializeComponent()
        TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
        TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
    End Sub
    Public Function EncryptData(
    ByVal plaintext As String) As String

        ' Convert the plaintext string to a byte array.
        Dim plaintextBytes() As Byte =
            System.Text.Encoding.Unicode.GetBytes(plaintext)

        ' Create the stream.
        Dim ms As New System.IO.MemoryStream
        ' Create the encoder to write to the stream.
        Dim encStream As New CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)

        ' Use the crypto stream to write the byte array to the stream.
        encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
        encStream.FlushFinalBlock()

        ' Convert the encrypted stream to a printable string.
        Return Convert.ToBase64String(ms.ToArray)
    End Function
    Sub New()
        ' TODO: Complete member initialization 
        InitializeComponent()
    End Sub


    Private Sub Add_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'PassMgrDataSet.Table1' table. You can move, or remove it, as needed.
        Me.Table1TableAdapter.Fill(Me.PassMgrDataSet.Table1)
        'TODO: This line of code loads data into the 'PassMgrDataSet1.Table1' table. You can move, or remove it, as needed.
        Me.Table1TableAdapter.Fill(Me.PassMgrDataSet.Table1)
        'TODO: This line of code loads data into the 'PassMgrDataSet.Table1' table. You can move, or remove it, as needed.
        Me.Table1TableAdapter.Fill(Me.PassMgrDataSet.Table1)

    End Sub

    Private Sub BtnAddEnc_Click(sender As Object, e As EventArgs) Handles BtnAddEnc.Click
        TestEncoding()
    End Sub
    Sub TestEncoding()
        Dim plainText As String = lblPassword.Text
        Dim wrapper As New Form1()
        Dim cipherText As String = wrapper.EncryptData(plainText)
        lblPassword.Text = cipherText
        My.Computer.FileSystem.WriteAllText(
            My.Computer.FileSystem.SpecialDirectories.MyDocuments &
            "\cipherText.txt", cipherText, False)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Table1TableAdapter.Fill(Me.PassMgrDataSet.Table1)
        Me.Table1TableAdapter.Fill(Me.PassMgrDataSet.Table1)
    End Sub

    Private Sub EncBtnEdit_Click(sender As Object, e As EventArgs) Handles EncBtnEdit.Click
        Edit_Window.Show()
    End Sub

    Private Sub btnaddnew_Click(sender As Object, e As EventArgs) Handles btnaddnew.Click
        Table1BindingSource.AddNew()
        AddAccName.Text = ""
        AddEID.Text = ""
        AddPass.Text = ""
        lblPassword.Text = ""
    End Sub

    Private Sub EncBtnDel_Click(sender As Object, e As EventArgs) Handles EncBtnDel.Click
        If Table1BindingSource.Count = vbEmpty Then
            MsgBox("No more fields to delete!", +vbInformation)
        Else
            Table1BindingSource.RemoveCurrent()
            Table1TableAdapter.Update(PassMgrDataSet.Table1)
        End If
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        EnDe.Show()
        Me.Hide()
    End Sub

    Private Sub btnsave_Click(sender As Object, e As EventArgs) Handles btnsave.Click
        If lblPassword.Text = "" Then
            MsgBox("Your password requires Security! Please Re-Enter Data, Click Encrypt and then proceed.", +vbCritical)
            Table1BindingSource.RemoveCurrent()
            btnback.PerformClick()
        Else
            Table1BindingSource.EndEdit()
            Table1TableAdapter.Update(PassMgrDataSet.Table1)
            DataGridView1.Refresh()
        End If
    End Sub

End Class





表格预览图片

http://stackoverflow.com/questions/23180354/unable-to-update-access-database-vb-net [ ^ ]

推荐答案

通常,这种问题不在代码中:重建时,访问文件被覆盖了你的项目。您没有显示连接到数据库的位置,因此很难说,但您可能会将数据库放在应用程序的bin文件夹中。



因此,请检查您的Access数据库是否包含在项目文件中,如果是,请将其删除。

将数据库移动到更合理的文件夹(应用程序二进制文件夹不是一个好地方:它是通常只在发布版本中阅读,仅适用于开发)。



请参阅此处:我应该在哪里存储我的数据? [ ^ ] - 代码是C#,但它很简单,你应该马上得到这个想法。
Normally, this kind of problem is not in the code: it's that the access file is being overwritten when you rebuild your project. You don't show where you connect to your DB, so it's difficult to tell, but the chances are you have your DB in the "bin" folder with your application.

So check if your Access database in included in your project files, and if so, remove it.
Move the DB to a more sensible folder (the application binary folder is not a good place: it's generally read only in release versions and only works in development).

See here: Where should I store my data?[^] - the code is C#, but it's pretty simple and you should get the idea straight away.


这篇关于无法更新Access数据库。如何使这成为可能? (VB.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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