0

我有一个关于将数据从一种方法调用到另一种方法的问题。

基本上,我的任务是压缩和简化我的代码。在这种情况下,我连接到一个 .MDB 文件并从中读取 1 位数据。最初我在所有需要它的地方都有连接字符串,但是在学习了越来越多的类和东西之后,我被告知复制和粘贴是不好的。所以我想把我的连接字符串做成......它是自己的 void 然后用来自所需位置的变量调用它。问题是如何返回从数据库中检索到的数据列表?这是我现在拥有的代码,它工作正常。

string directory = @"C:\";
            string[] folders = Directory.GetDirectories(directory, "Rameses-*");
            List<string> dataList = new List<string>();
            foreach (string foldername in folders)
            {
                using (var conection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" + "data source=" + foldername + "\\Program\\random.mdb;Jet OLEDB:Database Password=******"))
                {
                    conection.Open();
                    var query = "Select u_company From t_user";
                    var command = new System.Data.OleDb.OleDbCommand(query, conection);
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        dataList.Add(reader[0].ToString());
                    }
                    reader.Close();
                    conection.Close();
                }
            }
            error_handling(" Alternate Profiles Loaded into Selector");
            profselect.DataSource = dataList;
            profselect.SelectedText = dataList.Last();

但我认为我可以将连接从当前位置断开并执行类似操作,但是现在我无法从初始连接中获取数据列表以进行下拉。有什么建议么?

        //Database Connection Base Function
    public static void database_link(string query, string foldername)
    {
        List<string> dataList = new List<string>();
        using (var conection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" + "data source=" + foldername + "\\Program\\Random.mdb;Jet OLEDB:Database Password=****"))
        {
            conection.Open();
            var command = new System.Data.OleDb.OleDbCommand(query, conection);
            var reader = command.ExecuteReader();
            while (reader.Read())
            {
                dataList.Add(reader[0].ToString());
            }
            reader.Close();
            conection.Close();
        }
    }

    // Ramdata check and load from files and database
    private void ramdata_check(object sender, EventArgs e)
    {
        try
        {
            string directory = @"C:\";
            string[] folders = Directory.GetDirectories(directory, "Myapp-*");
            List<string> dataList = new List<string>();
            foreach (string foldername in folders)
            {
                database_link("Select u_company From t_user", foldername);
            }
            error_handling(" Alternate Profiles Loaded into Selector");
            profselect.DataSource = dataList;
            profselect.SelectedText = dataList.Last();
        }
        catch
{}
4

1 回答 1

0

不要让共享连接调用无效:让它创建连接,打开它并返回它。using 块将在完成时为您关闭连接,因此您无需手动关闭它:

public OleDbConnection OpenConnection()
{
      var result = new OleDbConnection("My special connection string here"))
     result.Open()
     return result;
 }

public List<string> void DoSomethingWithConnection()
 {
    using (var connection = OpenConnection());
    {
        List<string> dataList = new List<string>();

        OleDbCommand command = new OleDbCommand("DoSomethingHere", connection);
        var reader = command.ExecuteReader();
        while (reader.Read())
        {
            dataList.Add(reader[0].ToString());
        }

        return dataList;
    // The connection is automatically closed when the 
    // code exits the using block.
    }
}
于 2013-08-16T03:23:58.420 回答