简体   繁体   English

Visual Studio 2012中的SQL查询错误

[英]Error in SQL query in Visual Studio 2012

I am trying my hand at writing something that can be used at work and am learning on my own. 我正在尝试写一些可以在工作中使用并且自己学习的东西。 I've run into an issue that I cannot find a solution to, although it's very possible that I am just unfamiliar with the right terminology. 我遇到了一个找不到解决方案的问题,尽管我很可能不熟悉正确的术语。

I have attached a Visual Studio project to a SQL database and am trying to perform a search where I can enter information in any of the three text boxes and it will filter the returned results accordingly. 我已将Visual Studio项目附加到SQL数据库,并尝试执行搜索,在这里可以在三个文本框中的任何一个中输入信息,并且它将相应地过滤返回的结果。 Here is the User search query: 这是用户搜索查询:

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]

Here is the SUB I am using to call the search. 这是我用来调用搜索的SUB。 The only one I am having an issue with is the User search. 我遇到的唯一一个问题是用户搜索。

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

Whenever I test this, I receive the following error: 每当我对此进行测试时,都会出现以下错误:

Failed to enable constraints. 无法启用约束。 One or more rows contain values violating non-null, unique, or foreign-key constraints. 一或多个行包含违反非null,唯一或外键约束的值。

I have looked at the properties of all of the columns and all of them can contain NULL values. 我已经查看了所有列的属性,并且所有列都可以包含NULL值。 I have looked and made sure there are no foreign-key contraints on any of my tables. 我已经查看并确保我的任何桌子上都没有外键约束。 I am stumped as to what to look for next in order to resolve this as the other three searches I have in this code work just fine and they have very similar queries. 我很困惑接下来要寻找什么以解决此问题,因为我在此代码中进行的其他三个搜索工作正常,并且它们具有非常相似的查询。

Any help you can provide would be awesome! 您可以提供的任何帮助都很棒! Thanks!! 谢谢!!

Are there scenarios where firstname, lastname, and/or userid might be blank? 在某些情况下,名字,姓氏和/或用户ID可能为空?

Also sometimes this error pops up if you change a column length in the DB but don't update the dataset. 另外,如果您更改数据库中的列长度但不更新数据集,有时会弹出此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM