0

eu preciso de puchar somente um valor de uma tabela do sql, que eu estarei usando este valor para definir os usuários as roles, irá funcionar assim,

o usuario loga, então é criada uma conexção no banco de dados e nesta conexão ele verifica em qual role este usuario esta em então ele coloca o usuario na role,

o codigo que eu estou usando é este.

SqlConnection Conection = new SqlConnection(ConfigurationManager.ConnectionStrings["MarketingConnectionString"].ToString());
        SqlCommand consulta = new SqlCommand("SELECT REGRA FROM TblUsuariosExecutive WHERE Nome = 'victor'", Conection);
        SqlDataReader Dreader = null;
        try
        {
            Conection.Open();
            Dreader = consulta.ExecuteReader();

            while (Dreader.Read())
            {
                adm.Text = Dreader.ToString();
             
            }
        }
        catch
        {
            adm.Text = "ocorreu um erro";
        }
        finally
        {
            Dreader.Close();
            Conection.Close();
        }
5
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by hirenpatel53
0

if you want just one row one column in return use in the select statement do this
SELECT TOP 1 REGRA FROM TblUsuariosExecutive WHERE Nome = 'victor'

then in the SqlCommand execute the ExecuteScalar method that will return just one value as an Object just convert that to your data you are expecting.

0

An alternative without modifying the sql command would be to change how you run the query:

Change:
Dreader = consulta.ExecuteReader();

To:
Dreader = consulta.ExecuteScalar();

Execute scalar grabs only the first row, even if there are multiple results.

0

U can use any Top 1 Metode or ExecuteScalar As Explain Above As per i said Use Execute Sclar It will Speed Up Ur Application

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.