Warm tip: This article is reproduced from serverfault.com, please click

Simple Sql Role query issue

发布于 2020-11-27 23:23:28

I am trying to get a user role from the database.

This works, its just a debug

public String GetRole()
        {
            string role = "";
            string querySQL = "SELECT * FROM Utilizadores WHERE Role LIKE 'admin' ";
            List<Users> lista = new List<Users>();
            try
            {
                //Cria uma instância de ligação à base de dados SQL Server Express
                conn = new SqlConnection(ligacaobd);
                //Abre a ligação à base de dados
                conn.Open();
                //Executa a instrução SQL
                SqlCommand cmd = new SqlCommand(querySQL, conn);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    lista.Add(new Users(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3)));
                    role = reader.GetString(3);
                }
            }
            catch (SqlException)
            {
                return role;
            }
            finally
            {
                conn.Close();
            }
            return role;
        }

I put this in a button and when I click it, it changes the label to "admin" as it should.

What i wanted

public String GetRole(string username)
        {
            string role = "";
            string querySQL = "SELECT" +@username+ "FROM Utilizadores";
            List<Users> lista = new List<Users>();
            try
            {
                //Cria uma instância de ligação à base de dados SQL Server Express
                conn = new SqlConnection(ligacaobd);
                //Abre a ligação à base de dados
                conn.Open();
                //Executa a instrução SQL
                SqlCommand cmd = new SqlCommand(querySQL, conn);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    lista.Add(new Users(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3)));
                    role = reader.GetString(3);
                }
            }
            catch (SqlException)
            {
                return role;
            }
            finally
            {
                conn.Close();
            }
            return role;
        }

I wanted to get the incoming username role but i can't really figure it out.

Create table script

CREATE TABLE [dbo].[Utilizadores] (
[Id_Users] INT          IDENTITY (1, 1) NOT NULL,
[Username] VARCHAR (50) NOT NULL,
[Password] VARCHAR (50) NOT NULL,
[Role]     VARCHAR (50) DEFAULT (user_name()) NOT NULL,
PRIMARY KEY CLUSTERED ([Id_Users] ASC)

);

Questioner
Jinteste
Viewed
0
Göksel ÖZER 2020-11-28 08:50:11

You can try this if you want to get all columns

string querySQL = "SELECT * FROM Utilizadores WHERE Username=@username";
SqlCommand cmd = new SqlCommand(querySQL, conn);
cmd.Parameters.AddWithValue("@username",username);

If you want to get only Role column then try this

string querySQL = "SELECT Role FROM Utilizadores WHERE Username=@username";
SqlCommand cmd = new SqlCommand(querySQL, conn);
cmd.Parameters.AddWithValue("@username",username);