0

Is there a quick way to query a database table in c# and push all the results into an arrayList?

Thanks

1
  • possibly. why do u ask? Commented Mar 30, 2011 at 22:25

3 Answers 3

1

If you're using SQL Server you can use LINQ. http://msdn.microsoft.com/en-us/vcsharp/aa336746

Sign up to request clarification or add additional context in comments.

Comments

0

I like to use Microsoft Enterprise Library for database access. Once you have the libraries included in your project and a connection string defined in app.config it's very easy to run simple queries and map them to lists;

app.config

<connectionStrings>
    <add name="Default" connectionString="server=LOCALHOST;database=MyDb; integrated security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>

C#

class MyTable
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
}

var db = DatabaseFactory.CreateDatabase("Default");
var genericList = db.ExecuteSqlStringAccessor<MyTable>("select * from mytable").ToList();

2 Comments

you don't end up with an ArrayList there, you end up with a List
sorry, my mistake. If you really want an ArrayList you can do var arrayList = new ArrayList(genericList);
0

Found the solution by guess and check. Can anyone verify that this code is relatively sound?

 SqlConnection con = new SqlConnection(constr);
        con.Open();

        SqlCommand com = new SqlCommand(@"SELECT * FROM compsTickers", con);

        SqlDataReader reader = com.ExecuteReader();

        while (reader.Read())
        {
            tickerList.Add(reader.GetString(0));

        }
        reader.Close();
        con.Close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.