简体   繁体   English

SqlDataReader放入List <>-最快的方法

[英]SqlDataReader into List<> - Quickest way

When I am reading data from a sql db and want to add all the items into a list (ie. eonumlist) below. 当我从sql数据库读取数据并想将所有项目添加到下面的列表(即eonumlist)中时。 Do I need to specifically assign each field or can I mass assign this data? 我是否需要专门分配每个字段,还是可以大量分配此数据? I'm doing this for a report and want to get the data quickly. 我正在做此报告,并希望快速获取数据。 Maybe I should use a dataset instead. 也许我应该改用数据集。 I have 40+ fields to bring into the report and want to do this quickly. 我有40多个字段要纳入报告,并希望尽快完成。 Looking for suggestions. 寻找建议。

public static List<EngOrd> GetDistinctEONum()
{
    List<EngOrd> eonumlist = new List<EngOrd>();
    SqlConnection cnn = SqlDB.GetConnection();
    string strsql = "select distinct eonum " +
        "from engord " +
        "union " +
        "select 'zALL' as eonum " +
        "order by eonum desc";
    SqlCommand cmd = new SqlCommand(strsql, cnn);
    try
    {
        cnn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            EngOrd engord = new EngOrd();
            engord.EONum = reader["eonum"].ToString();
            engord.Name = reader["name"].ToString();
            engord.Address = reader["address"].ToString();
            eonumlist.Add(engord);
        }
        reader.Close();
    }
    catch (SqlException ex)
    {
        throw ex;
    }
    finally
    {
        cnn.Close();
    }
    return eonumlist;                
}

I do something similar storing data from a db into a combo box. 我做了类似的事情,将数据从数据库存储到组合框中。

To do this i use the following code. 为此,我使用以下代码。

 public static void FillDropDownList(System.Windows.Forms.ComboBox cboMethodName, String myDSN, String myServer)
        {
            SqlDataReader myReader;
            String ConnectionString = "Server="+myServer+"\\sql2008r2;Database="+myDSN+";Trusted_Connection=True;";

            using (SqlConnection cn = new SqlConnection(ConnectionString))
            {
                cn.Open();
                try
                {
                    SqlCommand cmd = new SqlCommand("select * from tablename", cn);
                    using (myReader = cmd.ExecuteReader())
                    {
                        while (myReader.Read())
                        {
                            cboMethodName.Items.Add(myReader.GetValue(0).ToString());
                        }
                    }
                }
                catch (SqlException e)
                {
                    MessageBox.Show(e.ToString());
                    return;
                }
            }
        }

This connects to the database and reads each record of the table adding the value in column 0 (Name) to a combo box. 这将连接到数据库,并读取表的每个记录,并将列0(名称)中的值添加到组合框中。

I would think you can do something similar with a list making sure the index values are correct. 我认为您可以对列表执行类似操作,以确保索引值正确。

将数据存储为xml,然后将xml反序列化到列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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