Returning a single row

前端 未结 10 2342
野的像风
野的像风 2021-02-13 06:58

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:18

    reader["col_1"] returns object.

    You want something like reader.GetString(reader.GetOrdinal("col_1")).

    Edit -> I just wanted to add a note here that, in addition to the concerns others have raised, a SELECT TOP without an ORDER BY can give you random results based on schema changes and/or merry-go-round scans.

    0 讨论(0)
  • 2021-02-13 07:22

    To me it seems, you don't want a single row, only a single value:

    SqlConnection sqlConnection = new SqlConnection("Your Connection String");
    SqlCommand cmd = new SqlCommand();
    Object returnValue;
    
    cmd.CommandText = "SELECT TOP 1 col_name FROM Customers";
    cmd.CommandType = CommandType.Text;
    cmd.Connection = sqlConnection1;
    
    sqlConnection.Open();
    
    returnValue = cmd.ExecuteScalar();
    
    sqlConnection.Close();
    
    return returnValue.ToString(); //Note you have to cast it to your desired data type
    
    0 讨论(0)
  • 2021-02-13 07:24

    You can use an if statement if your query only returns one value

    [...]
    string x = string.Empty;
    if(reader.Read()) {
        // make sure the value is not DBNull
        if(DBNull.Value != reader["col_1"]) {
           x = reader.GetString(0);
        }
    }
    [...]
    
    0 讨论(0)
  • 2021-02-13 07:28

    The problem is the return type. The method you are in is expecting you to return a string, but reader["col_1"] is an object. I suggest returning reader["col_1"].ToString() or Convert.ToString(reader["col_1"]).

    0 讨论(0)
  • 2021-02-13 07:29

    the reader returns object which you should cast it to what you need, in this case a string.

    you can use any of this codes :

    return reader.GetString(0);

    return reader["col_1"].ToString();

    return Convert.ToString(reader["col_1"]);

    return reader["col_1"] as string;

    but dont forget to close the connection and reader before leaving the function.

    string ret = reader.GetString(0);
    reader.Close();
    connection.Close();
    return ret;
    
    0 讨论(0)
  • 2021-02-13 07:31

    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(); 
    
    0 讨论(0)
提交回复
热议问题