1

我做了一个程序,它读取我使用的数据库中的数据,OleDbDataReader但问题是我有不同的表,这些代码运行良好,但我发现它有点“硬编码”或者recursive这是我的示例代码

        private void loadMilk()
        {
            cn.Open();
            OleDbDataReader reader = null;
            OleDbCommand cmd = new OleDbCommand("select* from Milk", cn);
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Milk.Add(reader["Product"].ToString());
            }
            cn.Close();
        }

我需要一次又一次地重复这个只是为了阅读另一张桌子上的内容(例如,"select* from Fruit然后"select* from Classics....)有什么办法可以让我不会一次又一次地重复这个代码?谢谢。:)

4

4 回答 4

4

您可以将该方法重构为如下内容:

private IList<string> Load(string tableName, string columnName)
{
    var result = new List<string>();
    cn.Open();
    OleDbDataReader reader = null;
    OleDbCommand cmd = new OleDbCommand(string.Format("select* from {0}", tableName), cn);
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        result.Add(reader[columnName].ToString());
    }
    cn.Close();

    return result;
}

您的代码示例将是:

var milkItems = Load("Milk", "Product");
var classicItems = Load("Classics", "..."); //Enter the column here.

编辑

您可能想要一些更具体的东西(例如,存储 aList<SomeObject>而不是只是List<string>)。假设您有时想要返回 的列表Person,并且还想读取 的列表Building。然后你可以写这样的东西(未编译和测试):

private IList<T> Load<T>(string tableName, Func<OleDbDataReader, T> selector)
{
    IList<T> result = new List<T>();
    cn.Open();
    OleDbDataReader reader = null;
    OleDbCommand cmd = new OleDbCommand(string.Format("select* from {0}", tableName), cn);
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        result.Add(selector(reader));
    }
    cn.Close();

    return result;
}

你可以这样称呼它:

Func<OleDbDataReader, Person> selector = x => new Person { Name = x["Person"].ToString() };
Load("People", selector);
于 2013-02-26T08:41:56.820 回答
3
private void loadMilk(string TableName, string itemValue)
        {
            string SQLString = String.Format("select * from {0}",TableName);

            cn.Open();
            OleDbDataReader reader = null;
            OleDbCommand cmd = new OleDbCommand(SQLString, cn);
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Milk.Add(reader[ItemValue].ToString());
            }
            cn.Close();
        }
于 2013-02-26T08:42:04.760 回答
1

不确定“牛奶”是什么类型。尝试:

private void loadObjectsFrom(string tableName, object obj, string column)
        {
            cn.Open();
            OleDbDataReader reader = null;
            OleDbCommand cmd = new OleDbCommand("select* from " + tableName, cn);
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                obj.Add(reader[column].ToString());
            }
            cn.Close();
        }
于 2013-02-26T08:43:48.347 回答
0

只需将表名作为参数传递:

    private void loadMilk(string tableName)
    {
        cn.Open();
        OleDbDataReader reader = null;
        OleDbCommand cmd = new OleDbCommand(string.Format("select* from {0}",tableName), cn);
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            Milk.Add(reader["Product"].ToString());
        }
        cn.Close();
    }
于 2013-02-26T08:43:17.640 回答