最初の事はあなたがあなたのアプリケーション構成ファイルに接続文字列を指定しているということです。
<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;
}
は、あなたがこのパス 'Dを持っていることを確認しています\ Program testen \ Program \ bin \ Debug'を実行しますか? –
それは私のPCで動作しますが、別のものに転送すると、私はprogram.exe.configを開いて、そのPC上の場所を変更することができます。実行するとエラーが表示されます – Viktor
エラーは何ですか?ターゲットマシンにMicrosoft AccessランタイムのMicrosoft Accessがインストールされていますか(Accessデータベースをバックエンドとして使用しているようです) –