locked
Run SQL Query & Return results to DataSet RRS feed

  • Question

  • Well I know how to run the query -- how do I return the results to a dataset?

    string Query = "Select * from tblMaster;"
    SqlConnection conn = new SqlConnection(//SQL Connection Here");
    {
    SqlCommand cmd = new SqlCommand(Query, conn);
    connection.Open();
    List<string> returnRes = new List<string>();
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
    returnRes.Add(reader[0].ToString());
    }
    reader.Close();

    How to put this into a dataset?

    Tuesday, January 7, 2014 2:46 PM

Answers

  • Is this what you are looking for ?

    		DataSet objTemp = new DataSet();
                    using (SqlConnection connection = new SqlConnection("your conn"))
                    {
                        using (SqlCommand command = new SqlCommand("your sql stmt", connection))
                        {
                            command.CommandType = CommandType.Text;
                       	connection.Open();
                            SqlDataAdapter adp = new SqlDataAdapter(command);
                            adp.Fill(objTemp);
                            connection.Close();
                        }
                    }
    


    Tuesday, January 7, 2014 3:35 PM