locoboy
locoboy

Reputation: 38940

get string data from sqlserver db table and pass to an arraylist

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

Thanks

Upvotes: 0

Views: 2661

Answers (3)

locoboy
locoboy

Reputation: 38940

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();

Upvotes: 0

cander
cander

Reputation: 852

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();

Upvotes: 0

System Down
System Down

Reputation: 6270

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

Upvotes: 1

Related Questions