C# - 动态访问Microsft Access数据库文件(.mdb)列表并打开它们以搜索数据

时间:2016-08-03 13:37:19

标签: c# database dynamic connection access

我正在编写一个程序,其中我有大量的.mdb文件,我需要能够连接到每个文件并搜索它们并根据数据库中的值检查用户数据。我不想对每个连接进行硬编码,因为数据库包含防火墙信息和特定位置的规则,这里的网络管理员倾向于向文件夹添加更多访问文件,并且不得不去更改程序的源代码每次发生这种情况。特别是一旦项目不再掌握在我手中,另一位开发人员就不得不继续工作。

现在,我创建了一个函数,用于创建当前目录中以.mdb或.ldb扩展名结尾的文件(各种)文件列表。我还有一个文件名的字符串数组,并输出一个消息框,只是为了确认该方法背后的一般思路是有效的。该程序能够正确列出具有.mdb扩展名的每个文件的名称,所以我知道它至少可以用这个扩展名拉动每个文件。现在我面临循环遍历它并打开每个数据库来拉取我需要的信息的问题。这是我感到困惑的地方。这是迄今为止的功能:

private void SelectDstIPTable()
    {
        //Write SQL code to select table
        //Select the table and pull the info
        //Do something?
        //Sort?

        //Does this need to be moved out of the method scope to the whole class?
        string strQuery = "SELECT * FROM devices WHERE dst_IP like '% " + textBox1.Text + "%' OR src_ip Like '%" + textBox1.Text + "%'";

        //This will be the list of files that end in .mbd that the code will loop through looking for the IP
        string currentDirectory = Directory.GetCurrentDirectory();
        var ext = new List<string> { "mdb", "ldb" };
        var myFiles = Directory.GetFiles(currentDirectory, "*.*", SearchOption.AllDirectories).Where(s => ext.Contains(Path.GetExtension(s)));
        string[] fileArray = Directory.GetFiles(currentDirectory, "*.mdb", SearchOption.AllDirectories);

        string allNames = "";

        //Just confirmation that this actually works
        foreach (string name in fileArray)
        {
            allNames += name + "\n";
        }

        MessageBox.Show(allNames);

        //Now we actually need to do something
    }

我还有另一个函数为特定数据库建立连接,以确保我检查数据库信息的代码是有效的。不幸的是,情况并非如此,但无论如何都是相关代码:

private string connParam = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=PathToDatabaseFile.mdb";
private void OpenConnection()
    {
        firewallConn.ConnectionString = connParam;
        firewallConn.Open();
        successfulConnection = true;
        MessageBox.Show("Connection successful!");
    }

private void EstablishConnection()
    {
        //There's a slight delay in making the connection now that I wrapped it in exception handling
        //TODO: Try to speed that up and figure out why it was slowed down by a noticable ammount
        try
        {
            if (firewallConn != null && firewallConn.State == ConnectionState.Closed)
                OpenConnection();
            else
                MessageBox.Show("The connection has already been established.\nType ''Close me'' to close the connection.");
        }
        catch (Exception exception)
        {
            MessageBox.Show(exception.ToString());
        }
    }

如果需要更多细节,请告诉我,我会尽我所能。

1 个答案:

答案 0 :(得分:0)

这个怎么样:

// the loop
foreach (string name in fileArray)
{    
    EstablishConnection(name); // you might need a full path!
    //do search...
}

// a little change in the EstablishConnection method
private void EstablishConnection(String dbPath)
{
    string connParamTemplate = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}";
    String conn = String.Format(connParamTemplate, dbPath);

    //connect... notice the change in OpenConnection!

// a little change in the OpenConnection method
private void OpenConnection(String connParam)

我只是根据DB的名称创建连接字符串,并将其传递给相关方法。