2016-03-25 8 views
0

ちょっと私はプログラミングに慣れていないし、別のPCで動作する必要がある実際のプログラムを作ったこともありません。プログラムはデータバンクに接続されています。私が別のPCにいるとき、私はデータバンクの正しい場所を適用することができるようにprogram.exe.configファイルを変更しますが、まだ動作しません。ここに私が持っているコードは、多分何かがここで間違っています。 はapp.config:標準コードで私のoledbプログラムを別のPCで動作させるにはどうすればいいですか

<connectionStrings> 
    <add name="Program.Properties.Settings.InventoryDBConnectionString" 
     connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\School\Stage\Program testen\Program\bin\Debug\InventoryDB.accdb" 
     providerName="System.Data.OleDb" /> 
</connectionStrings> 

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\School\Stage\Program testen\Program\bin\Debug\InventoryDB.accdb"); 
+0

は、あなたがこのパス 'Dを持っていることを確認しています\ Program testen \ Program \ bin \ Debug'を実行しますか? –

+0

それは私のPCで動作しますが、別のものに転送すると、私はprogram.exe.configを開いて、そのPC上の場所を変更することができます。実行するとエラーが表示されます – Viktor

+0

エラーは何ですか?ターゲットマシンにMicrosoft AccessランタイムのMicrosoft Accessがインストールされていますか(Accessデータベースをバックエンドとして使用しているようです) –

答えて

0

最初の事はあなたがあなたのアプリケーション構成ファイルに接続文字列を指定しているということです。

<connectionStrings> 
    <add name="Program.Properties.Settings.InventoryDBConnectionString" 
     connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\School\Stage\Program testen\Program\bin\Debug\InventoryDB.accdb" 
     providerName="System.Data.OleDb" /> 
</connectionStrings> 

あなたは」上がりませんProgram.exe.configの接続文字列を使用する代わりに、接続文字列をコピー&ペーストしています。

あなたのコードを少し変更した場合は、おそらくあなたは私が何を意味するか見ることができます:

//Get the connection string to use 
String connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\School\Stage\Program testen\Program\bin\Debug\InventoryDB.accdb"; 

//Open a connection to the database 
OleDbConnection con = new OleDbConnection(connectionString); 

あなたがやるべきことは何があなたのアプリケーションの設定から接続文字列を引っ張っている。

//Get our connection string setting, and the connectionString it contains 
ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["Program.Properties.Settings.InventoryDBConnectionString"]; 
String connectionString = cs.ConnectionString; 

//Open a connection to the database 
OleDbConnection con = new OleDbConnection(connectionString); 

ありあなたが作ることができるもう一つの変化。あなたの接続文字列のエントリ自体は、使用したいプロバイダを指定:

providerName="System.Data.OleDb" 

しかし、その後、あなたは先に行くと、そのプロバイダを自分で使用して:あなたは別のプロバイダを使用するように接続文字列を変更した場合

OleDbConnection con = new OleDbConnection(...); 

を:

providerName="System.Data.SqlClient" 

あなたのコードはまだOleDbConnectionオブジェクトはなく、applica指定されたプロバイダを使用することだろう。

幸い、.NETフレームワークは、それがの正しいプロバイダを作成することができる便利な小さなヘルパー関数を持っています:\学校\ステージ:

public static DbConnection GetConnection() 
{ 
    //Get the connection string info from our application config 
    ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["Program.Properties.Settings.InventoryDBConnectionString"]; 

    if (cs == null) 
     throw new Exception("Could not find connection string settings"); 

    //Get the factory for the given provider (e.g. "System.Data.OleDbClient") 
    DbProviderFactory factory = DbProviderFactories.GetFactory(cs.ProviderName); 

    if (factory == null) 
     throw new Exception("Could not obtain factory for provider \"" + cs.ProviderName + "\""); 

    //Have the factory give us the right connection object 
    DbConnection conn = factory.CreateConnection(); 

    if (conn == null) 
     throw new Exception("Could not obtain connection from factory"); 

    //Knowing the connection string, open the connection 
    conn.ConnectionString = cs.ConnectionString; 
    conn.Open(); 

    return conn; 
} 
関連する問題