2013-05-17 164 views
0

我正在努力編寫可以在工作中使用的東西,並且正在自行學習。我遇到了一個我找不到解決方案的問題,雖然我很可能只是不熟悉正確的術語。Visual Studio 2012中的SQL查詢錯誤

我已將Visual Studio項目附加到SQL數據庫,並試圖執行搜索,我可以在三個文本框中的任何一箇中輸入信息,並將相應地過濾返回的結果。這裏是用戶搜索查詢:

SELECT  First_Name AS [First Name], Middle_Name AS [Middle Name], Last_Name AS [Last Name], Users_ID AS [Unique ID] 
FROM   Users 
WHERE  (First_Name LIKE '%' + @firstName + '%') AND (Last_Name LIKE '%' + @lastName + '%') AND (Users_ID LIKE '%' + @uniqueID + '%') 
ORDER BY [Last Name] 

這是我用來調用搜索的SUB。我唯一遇到的問題是用戶搜索。

Private Sub searchButton_Click(sender As Object, e As EventArgs) Handles searchButton.Click 
    'Clear all variables when the search button is clicked. This allows Search to start with a clean' 
    'slate and gather new or changed variables again.' 
    addRemoveValue = Nothing 
    jobTitle = Nothing 
    jtLocation = Nothing 
    jtDepartment = Nothing 
    loc = Nothing 
    lComments = Nothing 
    department = Nothing 
    fname = Nothing 
    lname = Nothing 
    uID = Nothing 

    'Gather variables.' 
    addRemoveValue = TryCast(addRemoveComboBox.SelectedItem, String) 
    jobTitle = TryCast(jobTitleTextBox.Text, String) 
    jtLocation = TryCast(jtLocationComboBox.Text, String) 
    jtDepartment = TryCast(jtDepartmentComboBox.Text, String) 
    loc = TryCast(locationTextBox.Text, String) 
    lComments = TryCast(lCommentsTextBox.Text, String) 
    department = TryCast(departmentTextBox.Text, String) 
    fname = TryCast(firstNameTextBox.Text, String) 
    lname = TryCast(lastNameTextBox.Text, String) 
    uID = TryCast(uniqueIDTextBox.Text, String) 

    'I added this in order to troubleshoot the search feature for the Users search.' 
    'If uID = Nothing Then' 
    ' uID = "%"' 
    'End If' 

    'Change the visible buttons in the event of a Search.' 
    disableButton.Visible = True 
    addButton.Visible = False 

    'Populate the Data Grid based on the item the user selects.' 
    If addRemoveValue = "Job Title" Then 
     With Me.DataGridView1 
      .AutoGenerateColumns = True 
      .DataSource = RoleTableAdapter.GetDataByTitle(jobTitle, jtLocation, jtDepartment) 
      .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders 
      .AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.DisplayedCells 
      .BorderStyle = BorderStyle.Fixed3D 
      .EditMode = DataGridViewEditMode.EditOnEnter 
     End With 
    End If 

    If addRemoveValue = "Location" Then 
     With Me.DataGridView1 
      .AutoGenerateColumns = True 
      .DataSource = LocationTableAdapter.GetDataByLoc(loc) 
      .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders 
      .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells 
      .BorderStyle = BorderStyle.Fixed3D 
      .EditMode = DataGridViewEditMode.EditOnEnter 
     End With 
    End If 

    If addRemoveValue = "Department" Then 
     With Me.DataGridView1 
      .AutoGenerateColumns = True 
      .DataSource = DepartmentTableAdapter.GetDataByDept(department) 
      .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders 
      .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells 
      .BorderStyle = BorderStyle.Fixed3D 
      .EditMode = DataGridViewEditMode.EditOnEnter 
     End With 
    End If 

    If addRemoveValue = "User" Then 
     With Me.DataGridView1 
      .AutoGenerateColumns = True 
      .DataSource = UsersTableAdapter.GetDataByName(uID, fname, lname) 
      .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders 
      .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells 
      .BorderStyle = BorderStyle.Fixed3D 
      .EditMode = DataGridViewEditMode.EditOnEnter 
      .Columns("Unique ID").DisplayIndex = 0 
      .Columns("Last Name").DisplayIndex = 1 
      .Columns("First Name").DisplayIndex = 2 
      .Columns("Middle Name").DisplayIndex = 3 
     End With 
    End If 
End Sub 

每當我測試,我收到以下錯誤:

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

我看過的所有列的屬性,它們都可以包含空值。我查看了並確保我的桌子上沒有外鍵約束。爲了解決這個問題,我很困惑,爲了解決這個問題,我在這段代碼中進行的其他三項搜索工作得很好,並且他們有非常相似的查詢。

任何幫助你可以提供將是很棒!謝謝!!

回答

0

是否存在firstname,lastname和/或userid可能爲空的情況?

有時,如果更改數據庫中的列長度但不更新數據集,則有時會彈出此錯誤消息。

+0

是的,有。我的目標是用戶將能夠使用一個或多個字段進行搜索,但不必使用所有字段。 – Jerrbert

+0

@uniqueID是通過什麼數據類型?我知道它是我可以看到的部分中的字符串,但是如果它在應用之前轉換爲int,那麼這可能會有問題,但我會期望得到一個不同的錯誤。否則,確保任何綁定不會導致uniqueID重複或爲空,如果它實際上被約束爲唯一的(因爲afaik,沒有null的唯一約束將允許null,但只有一個null) – ZhiHeather

+0

@uniqueID的數據類型是varchar(12)。它是這樣設置的,因爲偶爾ID會寫一封信。 它恰好是我正在使用的表的主鍵,但我只是選擇行而不是添加行,所以獨特屬性不會發揮作用,因爲該列的所有內容在數據庫中已經是唯一的。 我離開那個基地嗎? – Jerrbert