2016-08-03 5 views
0

私は多少の.mdbファイルがあり、接続が必要なプログラムを作成しています各ファイルに検索し、それらを検索し、ユーザーデータとデータベースの値とを比較します。データベースにファイアウォール情報と特定の場所のルールが含まれていて、ネットワーク管理者がフォルダにアクセスファイルを追加する傾向があり、プログラムのソースを変更して変更する必要があるため、各接続をハードコードしたくないこれが起こるたびにコードを作成します。特にプロジェクトがもう手に入らなくなり、別の開発者が作業しなければならなくなります。マイクロソフトアクセスデータベースファイル(.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

ループを1分間忘れてしまいます。 .mdbファイルを開く方法を知っていますか? – elyashiv

+1

これはよりダイナミックにするのは難しいはずがありません。ただ接続文字列を変更し、失敗した場合はオープンしてください。成功すればスキップしてください。 – BugFinder

+0

@Elyashivはい - 関連するコードを追加しました。ファイアウォールのコメントを削除すると、firewallConn.ConnectionStringが誤って削除されました。私はそれを修正した。単一のデータベースに正常に接続します。 – Ryan

答えて

0

:私はちょうどDBの名前に応じて接続文字列を作成し、関連するメソッドにそれを渡し

// 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) 

+0

ありがとうございました!なぜ私には起こらなかったのか分かりません。この答えを見ている他の誰かに注意してください。メソッドEstablishConnection()にString.Format(connParamTemplate、name)があります。変数 'name'は間違った変数です。パラメータのdbPath変数を使用します。 'name'は前の関数から来ました。 – Ryan

+0

oops ...が修正されました。 – elyashiv

関連する問題