【发布时间】:2011-12-17 10:03:36
【问题描述】:
我被要求创建一个程序,将记录插入到一个父表和多个子表中。我的问题是,我怎么知道父表的 PK 是什么,以便我可以将它作为 FK 添加到子表中?父级的 PK 是一个自动编号。正如我在标题中所说,我通过 ODBC 连接使用 VB.net、mySQL。我必须通过代码做到这一点,不能使用存储过程。有什么建议吗?
谢谢
我的交易如下所示:
Dim cmdText As String = "INSERT INTO candidate(first_name, last_name, phone1, phone2, email1, city, " _
& " state, country, zip,primary_contact_id ) VALUES (?,?, ?, ?,?,?, ?,?,?,?)"
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim SqlStatus As Integer
Dim trans As Odbc.OdbcTransaction = conn.BeginTransaction(IsolationLevel.ReadCommitted)
Dim cmd As OdbcCommand = New OdbcCommand(cmdText, conn, trans)
Try
cmd.Parameters.Clear()
cmd.CommandType = CommandType.Text 'The default is CommandType.Text
With cmd.Parameters
.Add("@first_name", OdbcType.VarChar).Value = fName
.Add("@last_name", OdbcType.VarChar).Value = lName
.Add("@phone1", OdbcType.VarChar).Value = phone
.Add("@phone2", OdbcType.VarChar).Value = mobilePhone
.Add("@email1", OdbcType.VarChar).Value = email
.Add("@city", OdbcType.VarChar).Value = city
.Add("@state", OdbcType.VarChar).Value = state
.Add("@country", OdbcType.VarChar).Value = country
.Add("@zip", OdbcType.VarChar).Value = zip
.Add("@primary_contact_id", OdbcType.Int).Value = getContactFK
End With
SqlStatus = cmd.ExecuteNonQuery
If Not SqlStatus = 0 Then
trans.Commit()
Me.Close()
Else
MsgBox("Not Updated")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
trans.Dispose()
End Try
我仍在编写代码,所以不确定它是否有效 杰森
【问题讨论】:
标签: mysql vb.net odbc vb.net-2010