简化OleDbDataReader代码



我制作了一个程序,它读取数据库中的数据。我使用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……(有什么方法可以让我不重复这个代码吗?谢谢。:(

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

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.

编辑

您可能想要一些更具体的东西(例如,存储List<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);
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();
        }

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

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

只需传递一个表名作为参数:

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

相关内容

  • 没有找到相关文章

最新更新