我有2個存儲過程(InsertToCustomer)和(InsertToRepair),我需要在一個按鈕單擊事件下運行。然而,當我按一下按鈕只有InsertToRepair程序的工作原理,並將其存儲在「修復」表,但沒有對客戶信息的獲取存儲在「客戶」表在按鈕單擊事件上執行2個存儲過程vb.net
下面是本
我vb.Net代碼與我看到的代碼Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles btnBookRepair.Click
'Declare variables
Dim bookedInBy, deviceType, bookedInDate, fName, lName, address, contactNum, altContactNum, deviceName, accessories, devicePassword, repairType, technicalNotes, repairStatus, completedNotes As String
Dim trackingNum As Integer
'Setting Date
bookedInDate = txtBookedInDate.Text
'Assigning variables
bookedInBy = DDLBookedInBy.SelectedItem.Text
deviceType = DDLDeviceType.SelectedItem.Text
trackingNum = txtTrackingNumber.Text
fName = txtFname.Text
lName = txtLname.Text
address = txtAddress.Text
contactNum = txtContactNum.Text
altContactNum = txtAltContactNum.Text
deviceName = txtDeviceName.Text
accessories = DDLAccessories.SelectedItem.Text
devicePassword = txtDevicePassword.Text
repairType = DDLRepairType.SelectedItem.Text
technicalNotes = txtTechnical.Text
repairStatus = DDLRepairStatus.SelectedItem.Text
completedNotes = txtCompletedNotes.Text
Dim sqlCS As String
sqlCS = ConfigurationManager.ConnectionStrings("Database").ConnectionString
Try
Using sqlConn As New SqlConnection(sqlCS)
Dim sqlCmd As New SqlCommand
sqlCmd.Connection = sqlConn
sqlCmd.CommandText = "InsertToRepair"
sqlCmd.CommandType = Data.CommandType.StoredProcedure
sqlConn.Open()
sqlCmd.ExecuteNonQuery()
sqlCmd.Parameters.AddWithValue("@Tracking_Number", trackingNum)
sqlCmd.Parameters.AddWithValue("@Booked_in_by", bookedInBy)
sqlCmd.Parameters.AddWithValue("@Device_Type", deviceType)
sqlCmd.Parameters.AddWithValue("@Booked_In_Date", bookedInDate)
sqlCmd.Parameters.AddWithValue("@Device_Name", deviceName)
sqlCmd.Parameters.AddWithValue("@Accessories", accessories)
sqlCmd.Parameters.AddWithValue("@DevicePassword", devicePassword)
sqlCmd.Parameters.AddWithValue("@Repair_Type", repairType)
sqlCmd.Parameters.AddWithValue("@Technical_Notes", technicalNotes)
sqlCmd.Parameters.AddWithValue("@Completed_Notes", completedNotes)
sqlCmd.Parameters.AddWithValue("@RepairStatus", repairStatus)
sqlCmd.ExecuteNonQuery()
sqlCmd.CommandText = "InsertToCustomer"
sqlCmd.CommandType = Data.CommandType.StoredProcedure
sqlCmd.Parameters.AddWithValue("@First_Name", fName)
sqlCmd.Parameters.AddWithValue("@Last_Name", lName)
sqlCmd.Parameters.AddWithValue("@ContactNumber", contactNum)
sqlCmd.Parameters.AddWithValue("@Alternative_Contact_Number", altContactNum)
sqlCmd.Parameters.AddWithValue("@CustomerAddress", address)
sqlConn.Close()
End Using
Catch ex As Exception
StatusLabel.Text = ex.ToString
End Try
End Sub
您需要再次執行查詢。與您正在修理的內容相同。重新使用sqlCmd.ExecuteNonQuery()來執行SQL命令以將數據插入到客戶表中 – rach
某些事情不對。在設置參數之前執行InsertToRepair。如果SP需要參數,則應該得到一個異常。然後再次執行SP,並正確插入參數,最後爲第二個SP添加參數,但不要清除第一個調用的參數。它應該是另一個例外 – Steve
另一種選擇:創建一個SP,調用2個當前的SP。那麼你的.NET代碼只需要進行一次數據庫調用。 – Adam