早安堆栈溢出,
我是asp.net的新手,遇到问题我正在努力解决,也许你可以帮忙吗?
我要查找的结果是数据进入CANRADcontacts和CANRADcollreg,它们共享一个公共ID。
我通过网络表单填充数据库,并且无法正确获取SQL,请参阅此示例;
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Vars
Dim FlightDateArriveString As String = (FlightMonthArrive.SelectedValue.ToString & "/" & FlightDayArrive.SelectedValue.ToString & "/" & "2010")
Dim FlightDateDepartString As String = (FlightMonthDepart.SelectedValue.ToString & "/" & FlightDayDepart.SelectedValue.ToString & "/" & "2010")
Dim FlightDateArrive As DateTime
Dim FlightDateDepart As DateTime
FlightDateArrive = Convert.ToDateTime(FlightDateArriveString)
FlightDateDepart = Convert.ToDateTime(FlightDateDepartString)
'Setup db connection
Dim CollID As Integer = 1
Dim connstring As String = WebConfigurationManager.ConnectionStrings("ConnectionString").ToString()
Dim conn As SqlConnection = New SqlConnection(connstring)
Dim sql As String = ("INSERT INTO [CANRADcontacts]") & _
(" ([Title], [Initials], [FirstName], [LastName], [Organisation], [Department], [Address], [Code],") & _
(" [Telephone], [WorkTelephone], [Fax], [CellularTelephone],[Email], [ResearchFocus], [CollID], [ResearchFunders])") & _
(" VALUES (@Title, @Initials, @FirstName, @LastName, @Organisation, @Department, @Address, @Code, ") & _
("@Telephone, @WorkTelephone, @Fax, @CellularTelephone, @Email, @ResearchFocus, @CollID, @ResearchFunders) ") & _
("[CANRADcollreg]([CollID], [FlightDateArrive], [FlightNameArrive], [FlightTimeArrive], [FlightDateDepart], [FlightNameDepart], [FlightTimeDepart], [AttendingLaunch], [Accomodation], [DietaryRequirements], [SpecificDietaryRequirement]) ") & _
("VALUES (@CollID, @FlightDateArrive, @FlightNameArrive, @FlightTimeArrive, @FlightDateDepart, @FlightNameDepart, @FlightTimeDepart, @AttendingLaunch, @Accomodation, @DietaryRequirements, @SpecificDietaryRequirement)")
Dim comm As New SqlCommand(sql, conn)
's1
comm.Parameters.Add("@Title", SqlDbType.VarChar)
comm.Parameters.Add("@Initials", SqlDbType.VarChar)
comm.Parameters.Add("@FirstName", SqlDbType.VarChar)
comm.Parameters.Add("@LastName", SqlDbType.VarChar)
comm.Parameters.Add("@Organisation", SqlDbType.VarChar)
comm.Parameters.Add("@Department", SqlDbType.VarChar)
comm.Parameters.Add("@Address", SqlDbType.VarChar)
comm.Parameters.Add("@Code", SqlDbType.VarChar)
comm.Parameters.Add("@Telephone", SqlDbType.VarChar)
comm.Parameters.Add("@WorkTelephone", SqlDbType.VarChar)
comm.Parameters.Add("@Fax", SqlDbType.VarChar)
comm.Parameters.Add("@CellularTelephone", SqlDbType.VarChar)
comm.Parameters.Add("@Email", SqlDbType.VarChar)
comm.Parameters.Add("@ResearchFocus", SqlDbType.VarChar)
comm.Parameters.Add("@CollID", SqlDbType.VarChar)
comm.Parameters.Add("@ResearchFunders", SqlDbType.VarChar)
's2
comm.Parameters.Add("@FlightDateArrive", SqlDbType.DateTime)
comm.Parameters.Add("@FlightNameArrive", SqlDbType.VarChar)
comm.Parameters.Add("@FlightTimeArrive", SqlDbType.VarChar)
comm.Parameters.Add("@FlightDateDepart", SqlDbType.DateTime)
comm.Parameters.Add("@FlightNameDepart", SqlDbType.VarChar)
comm.Parameters.Add("@FlightTimeDepart", SqlDbType.VarChar)
comm.Parameters.Add("@AttendingLaunch", SqlDbType.VarChar)
comm.Parameters.Add("@Accomodation", SqlDbType.VarChar)
comm.Parameters.Add("@DietaryRequirements", SqlDbType.VarChar)
comm.Parameters.Add("@SpecificDietaryRequirement", SqlDbType.VarChar)
'bind1
comm.Parameters("@Title").Value = TitleDD.SelectedItem.ToString
comm.Parameters("@Initials").Value = Initials.Text
comm.Parameters("@FirstName").Value = FirstName.Text
comm.Parameters("@LastName").Value = LastName.Text
comm.Parameters("@Organisation").Value = Organisation.Text
comm.Parameters("@Department").Value = DeptDiv.Text
comm.Parameters("@Address").Value = PostalAddress.Text
comm.Parameters("@Code").Value = Code.Text
comm.Parameters("@Telephone").Value = Telephone.Text
comm.Parameters("@WorkTelephone").Value = WorkTelephone.Text
comm.Parameters("@Fax").Value = Fax.Text
comm.Parameters("@CellularTelephone").Value = CellularTelephone.Text
comm.Parameters("@Email").Value = EmailAddress.Text
comm.Parameters("@ResearchFocus").Value = ResearchFocus.Text
comm.Parameters("@CollID").Value = CollID
comm.Parameters("@ResearchFunders").Value = ResearchFunders.Text
'bind2
comm.Parameters("@FlightDateArrive").Value = FlightDateArrive
comm.Parameters("@FlightNameArrive").Value = FlightNameArrive.Text
comm.Parameters("@FlightTimeArrive").Value = FlightTimeArrive.Text
comm.Parameters("@FlightDateDepart").Value = FlightDateDepart
comm.Parameters("@FlightNameDepart").Value = FlightNameDepart.Text
comm.Parameters("@FlightTimeDepart").Value = FlightTimeDepart.Text
comm.Parameters("@AttendingLaunch").Value = AttendingMarch23.SelectedValue.ToString
comm.Parameters("@Accomodation").Value = Transport.SelectedValue.ToString
comm.Parameters("@DietaryRequirements").Value = Dietary.SelectedValue.ToString
comm.Parameters("@SpecificDietaryRequirement").Value = SpecificDietary.Text
conn.Open()
comm.ExecuteScalar()
conn.Close()
End Sub
在此先感谢,任何帮助都非常感谢!!
答案 0 :(得分:1)
问题出在你的SQL语句中 - 你需要有两个INSERT语句,每个表一个(注意分隔两个语句的分号)。
Dim sql As String = ("INSERT INTO [CANRADcontacts]") & _
(" ([Title], [Initials], [FirstName], [LastName], [Organisation], [Department], [Address], [Code],") & _
(" [Telephone], [WorkTelephone], [Fax], [CellularTelephone],[Email], [ResearchFocus], [CollID], [ResearchFunders])") & _
(" VALUES (@Title, @Initials, @FirstName, @LastName, @Organisation, @Department, @Address, @Code, ") & _
("@Telephone, @WorkTelephone, @Fax, @CellularTelephone, @Email, @ResearchFocus, @CollID, @ResearchFunders); ") & _
("INSERT INTO [CANRADcollreg] ([CollID], [FlightDateArrive], [FlightNameArrive], [FlightTimeArrive], [FlightDateDepart], [FlightNameDepart], [FlightTimeDepart], [AttendingLaunch], [Accomodation], [DietaryRequirements], [SpecificDietaryRequirement]) ") & _
("VALUES (@CollID, @FlightDateArrive, @FlightNameArrive, @FlightTimeArrive, @FlightDateDepart, @FlightNameDepart, @FlightTimeDepart, @AttendingLaunch, @Accomodation, @DietaryRequirements, @SpecificDietaryRequirement)")
答案 1 :(得分:0)
是的,你需要制作两个SQL命令...每个表一个,并为两个SQL命令添加参数(两个插入语句)。
我现在遇到的问题是CANRADCollreg中的ContactsID需要与CANRADcontacts表中的id列匹配
为什么必须与主键字段匹配?为什么ContactsID不能成为CANRAD表中的外键?对于你所描述的,这似乎是最好的方法。
如果这不起作用,那么请告诉我们有关此数据库的更多详细信息,或许我们可以提出更好的解决方案。