【问题标题】:How to do two inserts in two different tables in VB.Net如何在 VB.Net 的两个不同表中进行两次插入
【发布时间】:2019-02-10 18:01:51
【问题描述】:

我试图通过单击一个按钮来执行两个不同的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

【问题讨论】:

  • 对命令变量的第二个赋值会覆盖第一个。如果要运行多个插入语句,只需用分号分隔它们。此外,您真的应该考虑参数化查询,请参见此处:stackoverflow.com/questions/17867192/…
  • 您可能是指两个不同的表,而不是两个不同的数据库。

标签: sql vb.net ms-access


【解决方案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.

【讨论】:

  • 我的印象是Access只能执行单个语句,如果在分号后面遇到字符会出错。
  • @Mary,如果是这种情况,您将不得不使用两个命令执行两次ExecuteNonQuery,或者重复使用相同的命令对象,但请注意在添加参数之前清除参数集合第二次插入。
【解决方案2】:

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 个命令。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    相关资源
    最近更新 更多