Pomster
Pomster

Reputation: 15197

How can I populate a list with values from a SQL Server database?

The list will grow and shrink depending on how many items I have in my database.

I need to populate a list not a listbox. I understand I will need to open a connection.

using (var conn = new SqlConnection(Properties.Settings.Default.DBConnectionString))
{
    using (var cmd = conn.CreateCommand())
    {
        conn.Open(); 

        List<string> TagList = new List<string>();
        for (int i = 0; i < TagList.Count; i++)
            TagList[i].Add("Data from database");

        cmd.ExecuteNonQuery();
    }
}

I'm really not sure how to do this and I'm sure my method up here looks very wrong so I really need help.

Could someone show me what I'm doing wrong?

Upvotes: 4

Views: 24803

Answers (3)

JCO9
JCO9

Reputation: 990

I would like to share my solution, hope helps someone in the future:

public List<string> getFromDataBase() 
{
    List<string> result = new List<string>();
    using(SqlConnection con = new SqlConnection("connectionString"))
    {
        con.Open();
        DataTable tap = new DataTable();
        new SqlDataAdapter(query, con).Fill(tap);
        result = tap.Rows.OfType<DataRow>().Select(dr => dr.Field<string>("columnName")).ToList();
    }
    return result;
}

Upvotes: 8

Dummy01
Dummy01

Reputation: 1995

This would do as it is (if I didn't do any typos...)

private void LoadList()
    {
        List<string> tagsList = new List<string>();

        using (IDbConnection connection = new SqlConnection(Properties.Settings.Default.DBConnectionString))
        {
            connection.Open();    

            using (IDbCommand command = connection.CreateCommand())
            {
                command.CommandText = "SELECT TAGCOLUMN FROM TAGSTABLE";

                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        if (!reader.IsDBNull(0))
                            tagsList.Add(reader.GetString(0));
                    }

                    reader.Close();
                }
            }

            connection.Close();
        }
    }

EDIT:

Of course you have to change the select statement to the correct one from your database. I just used a pseudo one to show you what to put there.

Upvotes: 4

Damith
Damith

Reputation: 63065

public IEnumerable<string> GetTagList()
{
    using (var connection = new SqlConnection(Properties.Settings.Default.DBConnectionString))
    using (var cmd = connection.CreateCommand())
    {
        connection.Open();
        cmd.CommandText = "select Tag from TagsTable"; // update select command accordingly
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                yield return reader.GetString(reader.GetOrdinal("Tag"));
            }
        }
    }
}

then you can call it as below

List<string> tags = GetTagList().ToList();

Upvotes: 8

Related Questions