简体   繁体   中英

SQl cmd.ExecuteReader() what does it return

I'm studying ASP.NET. I wondered if cmd.ExecuteReader() 's output could be temporally stored into something, like a temp variable, to later re-use it or alter it. I often use temp variables to store stuff.

How can I let a dropbox and a gridview both work with the result of cmd.exectuteReader . I don't want to create a new SQL connection for it.

A variable t might keep the content, but obviously I'm wrong here since it doesn't work. It executes the reader twice, and on the second run there is no data to fill the dropdown box.

How should i do that ?

 protected void Page_Load(object sender, EventArgs e)
 {
    string cs = System.Configuration.ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; // reading by name DBCS out of the web.config file
    using (SqlConnection con = new SqlConnection(cs))
    {

      SqlCommand cmd = new SqlCommand("Select * from tblEmployees", con);
      con.Open();

       var t = cmd.ExecuteReader();

       GridView1.DataSource = t;// cmd.ExecuteReader();
       GridView1.DataBind();

        // DropDownList2.DataSource = cmd.ExecuteReader();
        DropDownList2.DataSource = t;//cmd.ExecuteReader();

       DropDownList2.DataTextField = "Name";
       DropDownList2.DataValueField = "EmployeeId";
       DropDownList2.DataBind();
    }
}

SqlDataReader is a forward-only stream of rows from a SQL Server database.

You can bind a SqlDataReader to a GridView in the following ways:

Simple Examples:

    connection.Open();
    command.Connection = connection;
    SqlDataReader reader = command.ExecuteReader();
    GridView1.DataSource = reader;
    GridView1.DataBind();

Or:

 DataTable dt = new DataTable();
 dt.Load(cmd.ExecuteReader());
 GridView1.DataSource = dt;

Don't forget to configure the columns inside the Gridview control.

To answer your question, it returns SqlDataReader as stated in Msdn documentation - SqlCommand.ExecuteReader Method ()

You can check documentation example for better understanding of ExecuteReader()

You have kinda turned this into an XY problem but for binding data to a GridView you should use a DataTable and a SqlDataAdapter . A generic example would be:

void FillData()
{
    // 1
    // Open connection
    using (SqlConnection c = new SqlConnection(
    Properties.Settings.Default.DataConnectionString))
    {
    c.Open();
    // 2
    // Create new DataAdapter
    using (SqlDataAdapter a = new SqlDataAdapter(
        "SELECT * FROM EmployeeIDs", c))
    {
        // 3
        // Use DataAdapter to fill DataTable
        DataTable t = new DataTable();
        a.Fill(t);

        // 4
        // Render data onto the screen
        // dataGridView1.DataSource = t; // <-- From your designer
    }
}

I won't use your code for the time being, so that you can experiment on this example. If you need further help, please edit your original post, after changing your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM