简体   繁体   English

如何从sql server 2008中获取列值?

[英]how do i get column value from sql server 2008?

i have table where have 5 columns : 我有5列的表:

i wrote the code like this : 我写了这样的代码:

String SQLQuery = "SELECT count(*) FROM aspnet_Users where Username=@uname AND Password = @pwd";
    using(SqlConnection sqlConnection = new SqlConnection(strConnection))
    using(SqlCommand command = new SqlCommand(SQLQuery, sqlConnection))
    {
        sqlConnection.Open();
        command.Parameters.AddWithValue("@uname", Username);
        command.Parameters.AddWithValue("@pwd", Password);
        int result = Convert.ToInt32(command.ExecuteScalar());
        boolReturnValue = (result > 0);
    }

here few more extra information i needed,if above Username and password is correct, what i need is : userid, and role column data 这里需要一些其他信息,如果上面的用户名和密码正确,我需要的是: userid和角色列数据

Why you aren't doing that instead ? 为什么您不这样做呢?

string SQLQuery = "SELECT UserId FROM aspnet_Users where Username=@uname AND Password = @pwd";
[...]
object result = command.ExecuteScalar();
if (result == null)
{
    boolReturnValue = false;
}
else
{
    long userId = Convert.ToInt64(result);
    boolReturnValue = true;
}
String SQLQuery = "SELECT Top 1 UserId, role  FROM aspnet_Users where Username=@uname AND Password = @pwd";
    using(SqlConnection sqlConnection = new SqlConnection(strConnection))
    using(SqlCommand command = new SqlCommand(SQLQuery, sqlConnection))
    {
        sqlConnection.Open();
        command.Parameters.AddWithValue("@uname", Username);
        command.Parameters.AddWithValue("@pwd", Password);

         SqlDataReader Reader = null;
         if (sqlConnection.State == ConnectionState.Closed || sqlConnection.State == ConnectionState.Broken)
                    sqlConnection.Open();
            Reader = command.ExecuteReader();
           if (Reader.Read())
          {
            int UserId = Convert.ToInt32(Reader["UserId"]);
            string Role = Convert.ToString(Reader["role"]);

          }

   }

Why don't you just get the UserId instead of the Count(*) so your query should look like this : 为什么不直接获取UserId而不是Count(*),所以查询应如下所示:

SELECT UserId FROM aspnet_Users where Username=@uname AND Password = @pwd

Username should be unique so you shouldn't retrieve more than one row...you can add a Top 1 in case you have multiple same username with same password. 用户名应该是唯一的,因此您不应检索超过一行...如果您有多个相同的用户名和相同的密码,则可以添加前1名。

试试这个代码

SELECT count(*),userid,role FROM aspnet_Users where Username=@uname AND Password = @pwd Group by userid,role 

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

相关问题 结果如何在列中获取列标题值? SQL Server 2008 - How to get column Header values in column as a result ?? Sql Server 2008 如何创建存储过程以获取SQL Server 2008中上一列Entry的值? - How to create stored procedure to get value of previous column Entry in SQL Server 2008? 在构建过程中如何将项目引用从SQL Server 2005更改为SQL Server 2008? - How do I change project references from SQL Server 2005 to SQL Server 2008 during a build? 如何在Excel Server 2008的varbinary(max)列中插入/检索Excel文件? - How do I insert/retrieve Excel files to varbinary(max) column in SQL Server 2008? 如何从SQL Server 2008中的表中获取非英语列名? - How can I fetch non-english column names from a table in SQL Server 2008? 如何获得与SQL Server 2008中的SERIAL PG数据类型相似的内容? - How do I get something similar to the SERIAL PG datatype in SQL Server 2008? 我必须从sql server 2008数据库中检索一个值 - I have to retrieve a value from sql server 2008 database 使用VPN连接到SQL Server时如何从查询中获取SQL Server 2008的端口号 - How should I get the port number for SQL Server 2008 from query when I am connecting to SQL Server using VPN 设置 SQL Server 2008,如何使用 SqlConnection 从 C# 访问它? - Set up SQL Server 2008, how do I access it from C# using SqlConnection? 如何在SQL Server 2008中更改列类型 - How to change column type in sql server 2008
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM