我需要能够将文本框中的值插入到访问数据库中,但截至目前它还无法正常工作

时间:2013-12-28 11:05:00

标签: vb.net ms-access

我无法将文本框中的值插入到访问数据库中,这是代码,它显示错误“没有给出所需参数的值”

 firstname = fname.Text
        middlename = mname.Text
        lastname = lname.Text
        address = addr.Text
        accountplan = accplan.SelectedItem
        accounttype = acctype.SelectedItem
        accountname = firstname + " " + middlename + " " + lastname
        Dim accessconn As New  _
System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & "avalanche.mdb")
        Dim com As System.Data.OleDb.OleDbCommand
        Try
            accessconn.Open()
            MsgBox("Access succesfully connected")
            com = New System.Data.OleDb.OleDbCommand("INSERT INTO account(firstname,middlename,lastname) VALUES(firstname,middlename,lastname)", accessconn)
            com.ExecuteReader(CommandBehavior.CloseConnection)
            accessconn.Close()
            Return
        Catch ex As Exception
            accessconn.Close()
            MsgBox(ex.ToString)
            Return
        End Try

2 个答案:

答案 0 :(得分:2)

您需要设置值:

    firstname = fname.Text
    middlename = mname.Text
    lastname = lname.Text
    address = addr.Text
    accountplan = accplan.SelectedItem
    accounttype = acctype.SelectedItem
    accountname = firstname + " " + middlename + " " + lastname
    Dim accessconn As New  _
    System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & "avalanche.mdb")
    Dim com As System.Data.OleDb.OleDbCommand
    Try
        accessconn.Open()
        MsgBox("Access succesfully connected")
        com = New System.Data.OleDb.OleDbCommand("INSERT INTO account(firstname,middlename,lastname) VALUES('"& firstname &"','"& middlename &"','"& lastname &"')", accessconn)
        com.ExecuteReader(CommandBehavior.CloseConnection)
        accessconn.Close()
        Return
    Catch ex As Exception
        accessconn.Close()
        MsgBox(ex.ToString)
        Return
    End Try

答案 1 :(得分:1)

嗨将记录添加到访问表很简单就像那样

firstname = fname.Text
middlename = mname.Text
lastname = lname.Text
address = addr.Text
accountplan = accplan.SelectedItem
accounttype = acctype.SelectedItem
accountname = firstname + " " + middlename + " " + lastname

dim conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & "avalanche.mdb"

dim con as new oledbConnection

con.connectionstring = conString
con.open()

cmd.connection = con
cmd.commandtext = "INSERT INTO account(firstname,middlename,lastname) VALUES('"& firstname & "','" & middlename & "','" & lastname & "')"
cmd.executeNonQuery


msgbox "Record Saved"

con.close()

如果您收到任何错误,请回复我很乐意帮助。

问候。