Getting Result From Select Command SQL Server

I am trying to get the result from a select command:

 string strName = dtTable.Rows[i][myName].ToString();
 string selectBrand = "SELECT [brand] FROM [myTable] WHERE [myName] = '" + strName + "'";

 SqlCommand sqlCmdSelectBrand = new SqlCommand(selectBrand , sqlConn);
 sqlCmdSelectBrand .Connection.Open();
 sqlCmdSelectBrand .ExecuteNonQuery();                         

 string newBrand = Convert.ToString(sqlCmdSelectBrand .ExecuteScalar());                          
 sqlCmdSelectBrand .Connection.Close(); 

The select works, I have executed it in SQL Studio, but it does not assign to my variable on the second to last line. Nothing gets assigned to that variable when I debug it…

  • Filtering by an ID that is part of a JOIN query
  • Using the result of an Stored procedure in a Select statement
  • Setting Identity to on or off in SQL server
  • How to create a Foreign Key with “ON UPDATE CASCADE” on Oracle?
  • “INSERT INTO .. ON DUPLICATE KEY UPDATE” Only inserts new entries rather than replace?
  • Suppress some warnings in SQL Server SSDT
  • Any advice?

  • SQL Join and concatenate rows
  • What is the most efficient / best practise to Upsert 5000+ rows without Merge in SQL Server?
  • Trim all database fields
  • How do I execute a .SQL file from Excel VBA code?
  • Syntax check all stored procedures?
  • Parse XML using T-SQL and XQUERY - Searching for specific values
  • One Solution collect form web for “Getting Result From Select Command SQL Server”

    Your approach to read data returned from a SELECT query is (in this particular context) a bit wrong. Usually you call ExecuteReader of the SqlCommand instance to get back your data.

    string strName = dtTable.Rows[i][myName].ToString();
    string selectBrand = "SELECT [brand] FROM [myTable] WHERE [myName] = @name";
    
    using(SqlCommand sqlCmdSelectBrand = new SqlCommand(selectBrand , sqlConn))
    {
        sqlCmdSelectBrand.Parameters.Add(
                 new SqlParameter("@name", SqlDbType.NVarChar)).Value = strName;
        sqlCmdSelectBrand .Connection.Open();
        using(SqlDataReader reader = sqlCmdSelectBrand.ExecuteReader())
        {
            if(reader.HasRows)
            {
               reader.Read();
               string newBrand = reader.GetString(reader.GetOrdinal("Brand"));
               ..... work with the string newBrand....
            }
            else
                // Message for data not found...
    
            sqlCmdSelectBrand .Connection.Close();
        }
    }
    

    In your context, the call to ExecuteNonQuery is not required because it doesn’t return anything from a SELECT query. The call to ExecuteScalar should work if you have at least one record that match to the WHERE condition

    Notice also that you should always use a parameterized query when building an sql command text. Also if you think to have full control of the inputs, concatenating string is the open door to Sql Injection

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.