Returning a single row

前端 未结 10 1449
轻奢々
轻奢々 2021-02-13 06:55

I\'m trying to return a single row from a database:

using (connection = new SqlConnection(ConfigurationManager.AppSettings[\"connection\"]))
{
    using (command         


        
10条回答
  •  你的背包
    2021-02-13 07:43

    Follow the following steps to select a single colume, and display them.

        //create a connection
        SqlConnection sqlConnection = new SqlConnection("Your Connection String");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = sqlConnection;
    
        //open the connection
        sqlConnection.Open();
    
        //Your command query string
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT TOP 1 col_name FROM Customers";
    
    
        //Execute the reader
        SqlDataReader result  = cmd.ExecuteReader();
        result.Read();
    
        //close the connection
        sqlConnection.Close();
    
        return result["coiumn_name"].ToString(); 
    

提交回复
热议问题