如何在VB.Net中的两个不同表中进行两次插入

时间:2019-02-10 18:01:51

标签: sql vb.net ms-access

我试图通过单击一个按钮来执行两个不同的INSERT语句。

但是,当我尝试运行代码时,只有INSERT条语句中的一条在起作用。

解决此问题的最佳方法是什么?

pro = "Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\Users\XXXX\Desktop\XXXX\XXXXX.mdb"
connstring = pro
myconnection.ConnectionString = connstring
myconnection.Open()

commmand = ("insert into ApplicationData ([lastname], [firstname],[studentbirthday],[gender], [email], [phonenumber], [address], [city], [state], [zip], [dadlastname], [dadfirstname], [momlastname],[momfirstname]) values ('" & NewLastNameText.Text & "', '" & NewFirstNameText.Text & "','" & NewDateTimePicker.Text & "','" & NewGenderText.Text & "','" & NewEmailText.Text & "','" & phone.Text & "','" & NewAddressText.Text & "','" & city.Text & "','" & state.Text & "','" & zip.Text & "','" & NewDadLNtext.Text & "','" & NewDadFNtext.Text & "','" & NewMomLNtext.Text & "','" & NewMomFNtext.Text & "')")
commmand = ("insert into StudentLogin ([username], [password]) values('" & username.Text & "','" & password.Text & "')")
Dim cmd As OleDbCommand = New OleDbCommand(commmand, myconnection)

cmd.Parameters.Add(New OleDbParameter("lastname", CType(NewLastNameText.Text, String)))
cmd.Parameters.Add(New OleDbParameter("firstname", CType(NewFirstNameText.Text, String)))
cmd.Parameters.Add(New OleDbParameter("studentbirthday", CType(NewDateTimePicker.Text, String)))
cmd.Parameters.Add(New OleDbParameter("gender", CType(NewDateTimePicker.Text, String)))
cmd.Parameters.Add(New OleDbParameter("email", CType(NewEmailText.Text, String)))
cmd.Parameters.Add(New OleDbParameter("phonenumber", CType(phone.Text, String)))
cmd.Parameters.Add(New OleDbParameter("address", CType(NewAddressText.Text, String)))
cmd.Parameters.Add(New OleDbParameter("city", CType(city.Text, String)))
cmd.Parameters.Add(New OleDbParameter("state", CType(state.Text, String)))
cmd.Parameters.Add(New OleDbParameter("zip", CType(zip.Text, String)))
cmd.Parameters.Add(New OleDbParameter("dadlastname", CType(NewDadLNtext.Text, String)))
cmd.Parameters.Add(New OleDbParameter("dadfirstname", CType(NewDadFNtext.Text, String)))
cmd.Parameters.Add(New OleDbParameter("momfirstname", CType(NewMomLNtext.Text, String)))
cmd.Parameters.Add(New OleDbParameter("momlastname", CType(NewMomFNtext.Text, String)))

cmd.Parameters.Add(New OleDbParameter("username", CType(username.Text, String)))
cmd.Parameters.Add(New OleDbParameter("password", CType(password.Text, String)))

Try
    cmd.ExecuteNonQuery()
    cmd.Dispose()
    myconnection.Close()
    MsgBox("Student Added")
    NewLastNameText.Clear()
    NewFirstNameText.Clear()
    NewEmailText.Clear()
    NewAddressText.Clear()
    NewDadLNtext.Clear()
    NewDadFNtext.Clear()
    NewMomLNtext.Clear()
    NewMomFNtext.Clear()
Catch ex As Exception

End Try

2 个答案:

答案 0 :(得分:1)

将两个命令放入同一字符串

Dim command1 = "insert into ApplicationData ([lastname], ... values (?, ?, ...)"
Dim command2 = "insert into StudentLogin ([username], ... values (?, ?, ...)"
commmand = command1 & "; " & command2

顺便说一句:您正在添加参数(这很好),但是没有用参数替换命令的字符串连接。对于OLEDB,您必须使用位置参数。也就是说,在SQL文本中,您必须为每个参数使用?。然后,您必须以相同顺序将参数添加到参数集合中! (您在此处使用的名称将被忽略,所以没关系。)


在创建连接字符串时将其传递给连接,以后不要更改。始终在Using Statement中声明连接。它会自动关闭并结束连接。请注意,每次使用一个连接对象时都不会产生问题。由于存在连接池,因此将重新使用“真实”连接。

pro = "Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\Users\XXXX\Desktop\XXXX\XXXXX.mdb"
Using myconnection As New OleDbConnection(pro)
    myconnection.Open()
    Dim command1 = "insert into ApplicationData ([lastname], ... values (?, ?, ...)"
    Dim command2 = "insert into StudentLogin ([username], ... values (?, ?, ...)"
    commmand = command1 & "; " & command2

    ...
End Using ' Automatically closes connection here.

答案 1 :(得分:0)

OleDb不在乎名称或我们的参数。它只关心它们在Sql语句中出现的顺序与它们添加到参数集合中的顺序是否匹配。

在Sql语句中连接字符串是一个不好的主意,原因有几个,在使用参数时肯定不需要。参数集合的.Add方法非常聪明,它返回一个OleDb参数对象,而无需我们明确声明。包括OleDb数据类型总是一个好主意。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'Pass the connection string directly to the constructor of the connection
    Using cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\Users\XXXX\Desktop\XXXX\XXXXX.mdb")
        'Pass the Sql statement and the connection directly to the constructor of the command.
        'Note: this should NOT be an open connection.
        Using StudentCommand As New OleDbCommand("insert into ApplicationData ([lastname], [firstname],[studentbirthday],[gender], [email], [phonenumber], [address], [city], [state], [zip], [dadlastname], [dadfirstname], [momlastname],[momfirstname]) values (lastname, firstname,studentbirthday,gender,email,phonenumber,address,city,state,zip,dadlastname,dadfirstname,momlastname,momfirstname);", cn)
            StudentCommand.Parameters.Add("lastname", OleDbType.VarChar).Value = NewLastNameText.Text
            StudentCommand.Parameters.Add("firstname", OleDbType.VarChar).Value = NewFirstNameText.Text
            StudentCommand.Parameters.Add("studentbirthday", OleDbType.VarChar).Value = NewDateTimePicker.Text
            StudentCommand.Parameters.Add("gender", OleDbType.VarChar).Value = NewDateTimePicker.Text
            StudentCommand.Parameters.Add("email", OleDbType.VarChar).Value = NewEmailText.Text
            StudentCommand.Parameters.Add("phonenumber", OleDbType.VarChar).Value = phone.Text
            StudentCommand.Parameters.Add("address", OleDbType.VarChar).Value = NewAddressText.Text
            StudentCommand.Parameters.Add("city", OleDbType.VarChar).Value = city.Text
            StudentCommand.Parameters.Add("state", OleDbType.VarChar).Value = state.Text
            StudentCommand.Parameters.Add("zip", OleDbType.VarChar).Value = zip.Text
            StudentCommand.Parameters.Add("dadlastname", OleDbType.VarChar).Value = NewDadLNtext.Text
            StudentCommand.Parameters.Add("dadfirstname", OleDbType.VarChar).Value = NewDadFNtext.Text
            StudentCommand.Parameters.Add("momfirstname", OleDbType.VarChar).Value = NewMomLNtext.Text
            StudentCommand.Parameters.Add("momlastname", OleDbType.VarChar).Value = NewMomFNtext.Text
            'Open the connection at the last minute
            cn.Open()
            StudentCommand.ExecuteNonQuery()
            cn.Close()
        End Using 'Disposes StudentCommand
        Using LoginCommand As New OleDbCommand("insert into StudentLogin ([username], [password]) values(@username, @password;", cn)
            LoginCommand.Parameters.Add("@username", OleDbType.VarChar).Value = username.Text
            LoginCommand.Parameters.Add("@password", OleDbType.VarChar).Value = password.Text
            cn.Open()
            LoginCommand.ExecuteNonQuery()
            'We don't need to .Close the connection
            'The second End Using will close and dispose the connection
        End Using 'Disposes LoginCommand
    End Using
    MessageBox.Show("Student Added")
    NewLastNameText.Clear()
    NewFirstNameText.Clear()
    NewEmailText.Clear()
    NewAddressText.Clear()
    NewDadLNtext.Clear()
    NewDadFNtext.Clear()
    NewMomLNtext.Clear()
    NewMomFNtext.Clear()
End Sub

一键单击,执行2条命令。

当然,在真实的应用程序中,您永远不会将密码另存为纯文本。