vb.net无法将查询写入MSAccess文件

时间:2014-03-08 10:40:33

标签: sql database vb.net

我可以访问本地Access数据库,我可以使用Query添加一些数据,但它必须在程序处于活动状态时显示其数据。当我重新执行这个程序时,我看不到任何以前添加的数据,原始数据库文件(SourceDB.mdb)也是如此。

如何保存数据?这是我的代码。

Imports System.Data.OleDb

Public Class UserForm
    Private myConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=.\SourceDB.mdb"
    Private mDbConn As OleDbConnection

    Private Sub Button_Search_Click(sender As Object, e As EventArgs) Handles Button_Search.Click

        Dim myAdapter As New OleDbDataAdapter("SELECT * FROM user WHERE u_name = '" +    tBoxName.Text + "'", myConn)
        Dim myDataTable As New DataTable
        If tBoxName.Text = "" Then
            MsgBox("Input name")
        Else
            myAdapter.Fill(myDataTable)
            If myDataTable.Rows.Count > 0 Then
                tBoxPhone.Text = myDataTable.Rows(0).Item("u_phone")
                tBoxAddr.Text = myDataTable.Rows(0).Item("u_addr")
                tBoxBName.Text = myDataTable.Rows(0).Item("u_bname")
                tBoxBAccount.Text = myDataTable.Rows(0).Item("u_baccount")
                tBoxEtc.Text = myDataTable.Rows(0).Item("u_comment")
            Else
                MsgBox("No Name in Table")
            End If
        End If
    End Sub

    Private Sub Button_OK_Click(sender As Object, e As EventArgs) Handles Button_OK.Click
        Try
            If lblAOE.Text = "Add" Then
                Dim myAdapter As New OleDbDataAdapter("INSERT INTO user (u_name, u_phone, u_addr, u_bname, u_baccount, u_comment) VALUES ('" + _
                                                                       tBoxName.Text + "', '" + tBoxPhone.Text + "', '" + tBoxAddr.Text + "', '" + tBoxBName.Text + _
                                                                       "', '" + tBoxBAccount.Text + "', '" + tBoxEtc.Text + "')", myConn)
                Dim myDataTable As New DataTable
                myAdapter.Fill(myDataTable)
                Button_Add.Visible = True
                Button_Modify.Visible = True
                Button_OK.Visible = False
                ClearTextBoxes()
                Button_Clear.Text = "비우기"
            End If        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button_Delete_Click(sender As Object, e As EventArgs) Handles Button_Delete.Click
        If MessageBox.Show("Sure Wanna delete data?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
            MsgBox("Cancelled")
            Exit Sub
        Else
            Dim myAdapter As New OleDbDataAdapter("DELETE FROM user WHERE u_name = '" + tBoxName.Text + "'", myConn)
            Dim myDataTable As New DataTable
            myAdapter.Fill(myDataTable)
            MsgBox("DELETED")
            ClearTextBoxes()
        End If
    End Sub

    Private Sub ClientForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.UserTableAdapter.Fill(Me.SourceDBDataSet.user)
        Me.Text = "User Form"
    End Sub
End Class

2 个答案:

答案 0 :(得分:1)

您正在使用OleDbDataAdapter尝试执行INSERT操作(或DELETE操作),但这不是OleDbDataAdapter的工作方式。

OleDbDataAdapter使用SELECT命令检索记录,如果已定义,则使用其InsertCommand属性通过调用OleDbDataAdapter.Update方法更新数据集的记录。您不是处于这种情况,而是直接在文本框中显示您的值。您可以简单地将OleDbCommand与适当的sql语句一起使用(这对于您的DELETE方法也是如此)

尝试将代码更改为

Dim cmdText = "INSERT INTO [user] (u_name, u_phone, u_addr, u_bname, " & _ 
              "u_baccount, u_comment) VALUES (?, ?, ?, ?,?,?)"
Dim cmd = New OleDbCommand(cmdText, myConn)
cmd.Parameters.AddWithValue("@p1",tBoxName.Text)
cmd.Parameters.AddWithValue("@p2",tBoxPhone.Text )
cmd.Parameters.AddWithValue("@p3",tBoxAddr.Text)
cmd.Parameters.AddWithValue("@p4",tBoxBName.Text)
cmd.Parameters.AddWithValue("@p5",tBoxBAccount.Text)
cmd.Parameters.AddWithValue("@p6",tBoxEtc.Text )
cms.ExecuteNonQuery()

我已将您的字符串连接更改为更正确的参数化查询,以避免在您的一个文本框值包含单引号时出现Sql Injection和解析问题。

答案 1 :(得分:0)

你使用OleDbCommand类。你创建类对象并使用ExecuteNonQuery插入数据 像

    Dim o As OleDbCommand
    o.Connection = con
    o.CommandType = CommandType.Text
    o.CommandText = "your Query"
    o.ExecuteNonQuery()

你应该使用try block

o.ExecuteNonQuery()

捕获

结束尝试