我在SQL Server中有3个表:
CountriesTbl :
CountryID int output,
CountryName nvarchar (50),
Nots nvarchar (Max),
ModifiedBy nvarchar (30)
CitiesTbl :
CityID int output,
CityName nvarchar (50),
CountryID int,
Nots nvarchar (Max),
ModifiedBy nvarchar (30)
CustomersTbl :
CustomerID int output,
CustomerName nvarchar (50),
CustomerPhoto image,
CustomerEmail nvarchar(Max),
CustomerPhone1 nvarchar(12),
CustomerPhone2 nvarchar(12),
CustomerAddress nvarchar(Max),
CustomerFax nvarchar(12),
CustomerStatus bit,
CountryID int,
CityID int,
Notes nvarchar (Max),
ModifiedBy nvarchar (30)
更新存储过程如下所示:
ALTER procedure [dbo].[Update_Customer_WithOutPic]
@CustomerID int,
@CustomerName nvarchar (50),
@CustomerEmail nvarchar(Max),
@CustomerPhone1 nvarchar(12),
@CustomerPhone2 nvarchar(12),
@CustomerAddress nvarchar(Max),
@CustomerFax nvarchar(12),
@CustomerStatus bit,
@CountryID int,
@CityID int,
@Notes nvarchar (Max),
@ModifiedBy nvarchar (30)
AS
BEGIN
UPDATE CustomersTbl
SET CustomerName = @CustomerName,
CustomerEmail = @CustomerEmail,
CustomerPhone1 = @CustomerPhone1,
CustomerPhone2 = @CustomerPhone2,
CustomerAddress = @CustomerAddress,
CustomerFax = @CustomerFax,
CustomerStatus = @CustomerStatus,
CountryID = @CountryID,
CityID = @CityID,
Notes = @Notes,
ModifiedDate = GETDATE(),
ModifiedBy = @ModifiedBy
WHERE
CustomerID = @CustomerID
END
在VB中,我有两个类:数据层和业务层。
数据层 - 更新代码:
Friend Function Update_Customer_WithOutPic(ByVal CustomerID As String, ByVal CustomerName As String, ByVal CustomerEmail As String, ByVal CustomerPhone1 As String, ByVal CustomerPhone2 As String, ByVal CustomerAddress As String, ByVal CustomerFax As String, ByVal CustomerStatus As Boolean, ByVal CountryID As Integer, ByVal CityID As Integer, ByVal Notes As String, ByVal ModifiedBy As String) As String
Dim retval As String
Dim cmd As New SqlCommand("Update_Customer_WithOutPic")
cmd.Parameters.AddWithValue("@CustomerID", CustomerID)
cmd.Parameters.AddWithValue("@CustomerName", CustomerName)
cmd.Parameters.AddWithValue("@CustomerEmail", CustomerEmail)
cmd.Parameters.AddWithValue("@CustomerPhone1", CustomerPhone1)
cmd.Parameters.AddWithValue("@CustomerPhone2", CustomerPhone2)
cmd.Parameters.AddWithValue("@CustomerAddress", CustomerAddress)
cmd.Parameters.AddWithValue("@CustomerFax", CustomerFax)
cmd.Parameters.AddWithValue("@CustomerStatus", CustomerStatus)
cmd.Parameters.AddWithValue("@CountryID", CountryID)
cmd.Parameters.AddWithValue("@CityID", CityID)
cmd.Parameters.AddWithValue("@Notes", Notes)
cmd.Parameters.AddWithValue("@ModifiedBy", ModifiedBy)
retval = dm.executeNonQuery(cmd)
Return retval
End Function
业务层:
Public Function Update_Customer_WithOutPic(ByVal CustomerID As String, ByVal CustomerName As String, ByVal CustomerEmail As String, ByVal CustomerPhone1 As String, ByVal CustomerPhone2 As String, ByVal CustomerAddress As String, ByVal CustomerFax As String, ByVal CustomerStatus As Boolean, ByVal CountryID As Integer, ByVal CityID As Integer, ByVal Notes As String, ByVal ModifiedBy As String) As String
Dim retval As String
retval = p.Update_Customer_WithOutPic(CustomerID, CustomerName, CustomerEmail, CustomerPhone1, CustomerPhone2, CustomerAddress, CustomerFax, CustomerStatus, CountryID, CityID, Notes, ModifiedBy)
Return retval
End Function
现在更新按钮代码:
Dim retval As String = p.Update_Customer_WithOutPic(txtCustomerCode.Text, txtCustomerName.Text, txtCustomerEmail.Text, txtCustomerPhone1.Text, txtCustomerPhone2.Text, txtCustomerAddress.Text, txtCustomerFax.Text, CheckBox2.Checked, ComboCustomerCountry.SelectedValue, ComboCustomerCity.SelectedValue, txtCustomernote.Text, FrmMain.LblUserID.Text)
我收到此错误:
UPDATE语句与FOREIGN KEY约束“FK_CustomersTbl_CountriesTbl”冲突。冲突发生在数据库“AlwaleedSSSystem”,表“dbo.CountriesTbl”,列'CountryID'。
答案 0 :(得分:0)
您的客户表中的外键约束似乎指向您的国家/地区表,而您在国家/地区组合框中选择的countryId在国家/地区表中不存在。确保从客户表引用的同一个表中加载您的国家/地区ID。
您应该阅读有关约束和数据库参照完整性的一些内容。 https://technet.microsoft.com/en-us/library/ms175464(v=sql.105).aspx