简体   繁体   English

与数据库价值进行比较

[英]Comparing against database value

What I am trying to do is grab the current logged in users username and compare that against a database which contains users, and also includes an Active flag and an Admin flag. 我正在尝试获取当前登录的用户名,并将其与包含用户的数据库进行比较,该数据库还包含一个Active标志和一个Admin标志。 I want to compare the current logged in user in the tbl_Person table and their respective user in the table to see if they are marked as Active and Admin. 我想比较tbl_Person表中的当前登录用户和表中的各自用户,以查看它们是否被标记为Active和Admin。 If both are true, they get access to an Admin page. 如果两者都正确,则他们可以访问“管理”页面。 I have the below so far which isn't working. 到目前为止,我的以下内容无法正常工作。 Some of which I know why, some I don't. 其中有些我知道为什么,有些我不知道。 I think I am on the right track, that being said I am sure I am not doing it correctly. 我认为我走在正确的轨道上,话虽这么说,但我确信我做得不好。 I know you use ExecuteScalar() to return something along with OUTPUT in the query string but couldn't get that to work. 我知道您使用ExecuteScalar()返回查询字符串中的某些内容以及OUTPUT,但无法正常工作。 The other glaring issue is that I am trying to return integers when the username is a string and the active and admin flags are Bools. 另一个明显的问题是,当用户名是字符串并且active和admin标志是Bools时,我试图返回整数。 I know that I only have Active in there are the moment. 我知道那一刻我只有Active。 I was trying to get that to work before adding in something else. 在添加其他内容之前,我试图使其正常工作。

I read that with the ExecuteScalar, you could Parse and convert ToString, but that didn't work and I found evidence that this might not be the correct thing to do, but I'm really not sure. 我读到,使用ExecuteScalar,可以解析和转换ToString,但这没有用,我发现有证据表明这可能不是正确的做法,但是我不确定。

I have got a few different errors. 我有几个不同的错误。 Type errors, invalid column when I've tried to do the OUTPUT. 尝试执行OUTPUT时键入错误,列无效。 With OUTPUT I tried as just OUTPUT and because I know when returning after inserting, you do inserted.name. 使用OUTPUT时,我尝试的只是OUTPUT,因为我知道插入后返回时,您确实插入了.name。 I tried selected.name as a hunch, but that didn't work. 我尝试将selected.name作为预感,但是那没有用。

I was thinking that if I pulled the info, concatenated them and then did a comparison, that this would do what I want, but I am open to other suggestions. 我当时在想,如果我提取信息,将它们连接起来,然后进行比较,则可以做到我想要的,但是我愿意接受其他建议。 Thanks. 谢谢。

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HSEProjRegConnectionString1"].ConnectionString);
conn.Open();
SqlCommand sqlUserName = new SqlCommand("SELECT [username] FROM [tbl_Person]", conn);
SqlCommand sqlActive = new SqlCommand("SELECT [active] FROM [tbl_Person]", conn);
int result1 = ((int)sqlUserName.ExecuteScalar());
int result2 = ((int)sqlActive.ExecuteScalar());

string userInfo = result1 + "." +result2;
string userName = userName + "." +result2;

if (userInfo == userName)
{
    Woo, you have access.
}
else
{
    Sorry, but no.
}

The Query isn't final either. 查询也不是最终的。 Once it is working, I'll change it to a parameterised query. 一旦运行,我将其更改为参数化查询。

Okay, consider the following code: 好的,请考虑以下代码:

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HSEProjRegConnectionString1"].ConnectionString))
{
    conn.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT [active] FROM [tbl_Person] WHERE username = @username", conn))
    {
        // since we can literally filter the results, if something comes back
        // we know they are registered
        cmd.Parameters.AddWithValue("@username", userName);

        var res = cmd.ExecuteScalar();
        bool registeredAndActive = (bool)res;

        // unless of course `[active]` is an INT -then do this
        bool registeredAndActive = (int)res == 1 ? true : false;

        // but really -set [active] up as a BIT if it's not **and**
        // please make it non-nullable :D
    }
}

I'm pretty sure it does what you want. 我很确定它能满足您的要求。 But it also shows you some best practices like: 但这也向您展示了一些最佳实践,例如:

  1. Leverage the using statement for all IDisposable objects. 利用所有IDisposable对象的using语句。
  2. Filter the query as much as you can and make only one round trip. 尽可能过滤查询,并且只进行一次往返。

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

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