使用VB在Access中添加数据

时间:2016-03-09 06:01:23

标签: mysql vb.net

我试图在VB中创建一个接受用户信息的程序,然后将其保存在MS访问中。我已连接MS访问和Visual Basic ...代码工作,但它不会添加用户在MS访问表中输入的值..

Public Sub AddNewStudent()
    Dim firstname, middlename As String
    con.Open()
    Dim myconnect As New SqlClient.SqlConnection
    myconnect.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\user.mdf;Integrated Security=True;user Instance=True"

    Dim mycommand As SqlClient.SqlCommand = New SqlClient.SqlCommand()

    mycommand.Connection = myconnect
    mycommand.CommandText = "INSERT INTO Students(FirstName, MiddleName, LastName, Address, Cellphone Number, Course)" & _
                            "VALUES(" & Me.firstnameTB.Text & ",'" & Me.midnameTB.Text & "')"
    firstname = firstnameTB.Text
    middlename = midnameTB.Text

    Try
        mycommand.Parameters.AddWithValue("@firstname", SqlDbType.NVarChar).Value = Me.firstnameTB.Text()
        mycommand.Parameters.Add("@middlename", SqlDbType.NVarChar).Value = Me.midnameTB.Text
        mycommand.Parameters.Add("@lastnameTB", SqlDbType.NVarChar).Value = Me.lastnameTB.Text
        mycommand.Parameters.Add("@addressTB", SqlDbType.NVarChar).Value = Me.addressTB.Text
        mycommand.Parameters.Add("@cpnumTB", SqlDbType.NVarChar).Value = Me.cpnumTB.Text
        mycommand.Parameters.Add("@courseCB", SqlDbType.NVarChar).Value = Me.courseCB.Text
        MsgBox("Successfully added new student")
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try
    myconnect.Close()

2 个答案:

答案 0 :(得分:1)

首先,您指定要插入六列,但您只提供两个值。其次,您向命令添加参数,但SQL代码中没有参数。您需要提供与列数相同的值,并且这些值需要是参数。还有一些其他问题,例如列名称中包含空格并且不正确地调用AddWithValue。

Public Sub AddNewStudent()
    Dim firstname, middlename As String
    con.Open()
    Dim myconnect As New SqlClient.SqlConnection
    myconnect.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\user.mdf;Integrated Security=True;user Instance=True"

    Dim mycommand As SqlClient.SqlCommand = New SqlClient.SqlCommand()

    mycommand.Connection = myconnect
    mycommand.CommandText = "INSERT INTO Students(FirstName, MiddleName, LastName, Address, [Cellphone Number], Course)" & _
                            "VALUES(@firstname, middlename, @lastnameTB, @addressTB, @cpnumTB, @courseCB)"
    firstname = firstnameTB.Text
    middlename = midnameTB.Text

    Try
        mycommand.Parameters.Add("@firstname", SqlDbType.NVarChar).Value = Me.firstnameTB.Text
        mycommand.Parameters.Add("@middlename", SqlDbType.NVarChar).Value = Me.midnameTB.Text
        mycommand.Parameters.Add("@lastnameTB", SqlDbType.NVarChar).Value = Me.lastnameTB.Text
        mycommand.Parameters.Add("@addressTB", SqlDbType.NVarChar).Value = Me.addressTB.Text
        mycommand.Parameters.Add("@cpnumTB", SqlDbType.NVarChar).Value = Me.cpnumTB.Text
        mycommand.Parameters.Add("@courseCB", SqlDbType.NVarChar).Value = Me.courseCB.Text
        mycommand.ExecuteNonQuery
        MsgBox("Successfully added new student")
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try
    myconnect.Close()

答案 1 :(得分:-1)

像这样更改命令文本

 mycommand.CommandText = "INSERT INTO Students(FirstName, MiddleName, LastName, Address, [Cellphone Number], Course)" & _
                            "VALUES(@firstname, @middlename, @lastnameTB, @addressTB, @cpnumTB, @courseCB)"

更改所有行设置数据(更方便)

mycommand.Parameters.AddWithValue("@courseCB", courseCB.Text)

最后编写此代码   MsgBox(“成功添加新学生”)

mycommand.ExecuteNonQuery