运行查询plz帮助时出现事务查询错误 [英] transaction query error when running the query plz help
问题描述
好的,所以我有一个网页表单,其中数据被添加到两个表中。一个表有一个FK(电子邮件),它引用另一个表主键(电子邮件)。
当我运行代码时将值插入到表单中以将其存储在数据库中...但我收到错误
what我在下面的代码中做错了,请帮助
INSERT语句与FOREIGN KEY约束FK_Tech_Admin冲突。冲突发生在数据库E:\ATOM \ 06012013 \APP_DATA \ ATOM.MDF中,
以下是我的代码:
connect.ConnectionString = Constr
Dim query1 As String =INSERT into Tech VALUES(@ first,@ last,@ add,@ phone,@ email,@ status,@ photo,@ registration,@ DOB)
Dim query2 As String =INSERT into Admin VALUES(@email,@ password,@ type)
Dim cmd作为新的SqlCommand
Dim cmd2作为新的SqlCommand
cmd.CommandText = query1
cmd2.CommandText = query2
cmd.Connection = connect
cmd2.Connection = connect
cmd.Parameters.AddWithValue(@第一个,txtFirst.Text
cmd.Parameters.AddWithValue(@ last,txtLast.Text)
cmd.Parameters.AddWithValue(@ add,txtAddress .Text)
cmd.Parameters.AddWithValue(@ phone,txtPhone.Text)
cmd.Parameters.AddWithValue(@ email,txtEmail.Text)
cmd.Parameters.AddWithValue(@ status,txtStatus.Text)
cmd.Parameters.AddWithValue(@ photo,lblPath.Text)
cmd.Parameters.AddWithValue(@ registration,txtRegistration.Text)
cmd .Parameters.AddWithValue(@ DOB,txtDOB.Text)
cmd2.Parameters.AddWithValue(@ email,txtEmail.Text)
cmd2.Parameters.AddWithValue(@ password,txtPassword.Text)
cmd2.Parameters.AddWithValue(@ type,txtType.Text)
Dim Ts作为SqlTransaction
尝试
connect.Open()
Ts = connect.BeginTransaction
cmd.Transaction = Ts
cmd2.Transaction = Ts
Dim i As Integer = cmd.ExecuteNonQuery
如果我> 0然后
MsgBox(tech)
结束如果
Dim j As Integer = cmd2.ExecuteNonQuery
如果j> 0然后
MsgBox(管理员表)
结束如果
Ts.Commit()
Catch ex As Exception
MsgBox(不成功)
抛出ex
Ts.Rollback()
最后
connect.Close()
结束尝试
请帮帮我...
ok so i have a web form in which data is being added into two tables. one table has a FK(email) which refers to the other table primary key(email).
when i run the code insert values in to the form to store it in the db...but i get an error
what im doing wrong in my code below, please help
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Tech_Admin". The conflict occurred in database "E:\ATOM\06012013\APP_DATA\ATOM.MDF",
Below is my code:
connect.ConnectionString = Constr
Dim query1 As String = "INSERT into Tech VALUES (@first, @last, @add, @phone, @email, @status, @photo, @registration, @DOB)"
Dim query2 As String = "INSERT into Admin VALUES (@email, @password, @type)"
Dim cmd As New SqlCommand
Dim cmd2 As New SqlCommand
cmd.CommandText = query1
cmd2.CommandText = query2
cmd.Connection = connect
cmd2.Connection = connect
cmd.Parameters.AddWithValue("@first", txtFirst.Text)
cmd.Parameters.AddWithValue("@last", txtLast.Text)
cmd.Parameters.AddWithValue("@add", txtAddress.Text)
cmd.Parameters.AddWithValue("@phone", txtPhone.Text)
cmd.Parameters.AddWithValue("@email", txtEmail.Text)
cmd.Parameters.AddWithValue("@status", txtStatus.Text)
cmd.Parameters.AddWithValue("@photo", lblPath.Text)
cmd.Parameters.AddWithValue("@registration", txtRegistration.Text)
cmd.Parameters.AddWithValue("@DOB", txtDOB.Text)
cmd2.Parameters.AddWithValue("@email", txtEmail.Text)
cmd2.Parameters.AddWithValue("@password", txtPassword.Text)
cmd2.Parameters.AddWithValue("@type", txtType.Text)
Dim Ts As SqlTransaction
Try
connect.Open()
Ts = connect.BeginTransaction
cmd.Transaction = Ts
cmd2.Transaction = Ts
Dim i As Integer = cmd.ExecuteNonQuery
If i > 0 Then
MsgBox("tech")
End If
Dim j As Integer = cmd2.ExecuteNonQuery
If j > 0 Then
MsgBox("admin table")
End If
Ts.Commit()
Catch ex As Exception
MsgBox("unsuccessfull")
Throw ex
Ts.Rollback()
Finally
connect.Close()
End Try
Please help me out...
推荐答案
不确定,但我确实注意到你在回滚之前抛出的捕获物。
另外,只需使用throw,而不是扔ex。
您是否尝试过以其他顺序执行命令?
Unsure, but I do notice in the catch you throw before before you rollback.
Also, just use throw, not throw ex.
Have you tried executing the commands in the other order?
这篇关于运行查询plz帮助时出现事务查询错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!