Visual Studio 2010 で C# からMicrosoft AccessDatabase
オブジェクトのプロパティ(のような) にアクセスする最良の方法は何ですか?CurrentDb.Properties
(必須ではありません:実際、「オンデマンド」で数十のデータベースでレプリケーションを取り除きたいです。レプリケーションは数年間使用されていません。2013年より前のMS Accessでは問題ありませんでした。Access 2013はこれでデータベースを拒否します特徴。)
Access DAO オブジェクト ライブラリを使用して、Access データベースのプロパティを反復処理および変更できます。
次のコードは、データベース内のプロパティ、さまざまなコンテナーとそのプロパティ、および Databases コンテナー内の Documents とそのプロパティを反復処理します。出力は [デバッグ出力] ウィンドウに書き込まれます。
その後、Property
さまざまなプロパティ コレクションから を選択し、その値を変更します。プロパティが存在しない場合、コレクションでインデクサーを使用すると例外がスローされることに注意してください。
Microsoft Office 12.0 Access データベース エンジン オブジェクト ライブラリの Primary Interop Assembly への参照があることを確認してください(バージョンは異なる場合があります)。
using Microsoft.Office.Interop.Access.Dao;
あなたの方法は次のようになります:
// Open a database
var dbe = new DBEngine();
var db = dbe.OpenDatabase(@"C:\full\path\to\your\db\scratch.accdb");
// Show database properties
DumpProperties(db.Properties);
// show all containers
foreach (Container c in db.Containers)
{
Debug.WriteLine("{0}:{1}", c.Name, c.Owner);
DumpProperties(c.Properties);
}
//Show documents and properties for a specific container
foreach (Document d in db.Containers["Databases"].Documents)
{
Debug.WriteLine("--------- " + d.Name);
DumpProperties(d.Properties);
}
// set a property on the Database
Property prop = db.
Properties["NavPane Width"];
prop.Value = 300;
// set a property on the UserDefined document
Property userdefProp = db
.Containers["Databases"]
.Documents["UserDefined"]
.Properties["ReplicateProject"];
userdefProp.Value = true;
private static void DumpProperties(Properties props)
{
foreach (Property p in props)
{
object val = null;
try
{
val = (object)p.Value;
}
catch (Exception e)
{
val = e.Message;
}
Debug.WriteLine(
"{0} ({2}) = {1}",
p.Name,
val,
(DataTypeEnum) p.Type);
}
}
これを使用して、動的型でスローされる例外を克服しました (Value プロパティが判明したため)。