简体   繁体   English

从sql查询中排除特定结果并将其余结果显示在组合框中-Visual Studio 2013

[英]excluding specific results from sql query and displaying the rest in a combo box - visual studio 2013

This has probably been asked somewhere before, but I have been searching for a while and cant find anything. 可能以前有人问过这个问题,但是我已经搜索了一段时间,却找不到任何东西。 I'm basically trying to create a sort-of internal messaging system in VB and am having trouble with a function that I'm working on. 我基本上是在尝试在VB中创建某种内部消息传递系统,而我正在使用的功能却遇到了麻烦。 I already have a user database and secure login system and I'm now working on a form to send a message from user to user. 我已经有一个用户数据库和安全的登录系统,现在正在处理一个表单,用于在用户之间发送消息。

What I want to do is to run this query on form load: 我想做的是在表单加载时运行此查询:

SELECT usr_id, usrname FROM dbo.users 
WHERE usrname NOT IN 
    (
     SELECT ALL usrname 
     FROM dbo.users 
     WHERE usrname = '" & //textbox containing username that's logged in// & "'
    )

I want to output the items to a Combobox. 我想将项目输出到组合框。 The purpose of this is so that (since it's an internal system for, say, employees to communicate) a user wouldn't necessarily have to know the username of the receiver in order to send them a message. 这样的目的是(因为它是一个内部系统,例如,员工可以进行通信),用户不必知道接收方的用户名即可向他们发送消息。 I will be changing the function eventually to display the actual name of the user rather than the username, but I can add that in later and as of right now it's not important. 我最终将更改该功能以显示用户的实际名称而不是用户名,但是我可以稍后再添加,到目前为止,这并不重要。 Here is my code so far: 到目前为止,这是我的代码:

Private Sub NewMsg_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim con As New SqlConnection
    con.ConnectionString = //connection string for database
    Dim query As String = //query mentioned above
    Try
        con.Open()
        Using sqlcmd As New SqlCommand(query, con)
            Dim sqldr As SqlDataReader = sqlcmd.ExecuteReader
            Dim dt As DataTable = New DataTable
            dt.Load(sqldr)
            sendtoBox.ValueMember = "usr_id"
            sendtoBox.DisplayMember = "usrname"
            sendtoBox.DataSource = dt
            con.Close()
        End Using
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Information)
        con.Close()
    End Try
End Sub

The query itself works perfectly when I run it on the SQL Server, and sure enough when the form loads users are displayed in the Combobox just like I want them to. 当我在SQL Server上运行查询时,查询本身可以完美地工作,并且可以肯定的是,当窗体加载用户时,就像我希望他们一样,用户会显示在组合框中。 The only problem is it's still including the username that I am trying to exclude. 唯一的问题是它仍然包含我要排除的用户名。 So I have reluctantly decided to ask for a little help because I can't figure out why it's not excluding the specified username :( 所以我勉强决定寻求一点帮助,因为我不知道为什么它不排除指定的用户名:(

Any help will be appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

Problem was my own stupidity lol. 问题是我自己的愚蠢。 Nothing wrong with the code itself, may be useful for someone so I will leave it up here :) 代码本身没有错,对某人可能有用,所以我将其留在这里:)

For the person who thinks this does not answer the question, I would like to initially state that I was the one who asked the question. 对于认为这不能回答问题的人,我首先要声明我是提出问题的人。

This was being used for a "Send Message" form, and the desired function was as follows: 它被用于“发送消息”表单,所需的功能如下:

GET CURRENTLY LOGGED IN USERS USERNAME FROM A TEXTBOX ON A DIFFERENT FORM! >
Connect to database >
Run a query against database to gather all users EXCEPT the logged in user >
Display results in a ComboBox.

The query was fine, the code was fine, everything was fine. 查询很好,代码很好,一切都很好。 The PROBLEM here was that when I was debugging the form I was NOT LOGGING IN and hence the TextBox that contained the username that I wanted to exclude was EMPTY, so instead of excluding say "admin" it was excluding "" because nobody was logged in. 这里的问题是,当我调试表单时,我没有登录,因此包含我要排除的用户名的TextBox是EMPTY,因此不是排除说“ admin”,而是排除了“”,因为没有人登录。

So tell me, how is this not an answer? 所以告诉我,这不是答案吗? It was me being dumb that caused the problem in the first place, but the VB code and SQL query may help someone else, that is why I have left it up and answered by explaining that there is nothing wrong with the code. 最初是由我愚蠢引起问题的,但VB代码和SQL查询可能会对其他人有所帮助,这就是为什么我将其保留下来并通过解释代码没有问题来回答的原因。 Get off your high-horse man... 下车你的高个子男人...

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

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