Skip to content

Commit

Permalink
generate MSAccess .accdb file programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Ste1io committed Oct 7, 2023
1 parent 3bf3209 commit 77acb79
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.IO;
using System.Linq;
using System.Configuration;
using System.Runtime.InteropServices;

namespace PetaPoco.Tests.Integration.Databases.MSAccess
{
Expand All @@ -9,6 +12,25 @@ public class MSAccessDbProviderFactory : BaseDbProviderFactory

protected override string ScriptResourceName => "PetaPoco.Tests.Integration.Scripts.MSAccessBuildDatabase.sql";

public override IDatabase Execute()
{
if (!File.Exists(Path.Combine(Environment.CurrentDirectory, "PetaPoco.accdb")))
{
var catalog = new ADOX.Catalog();

try
{
catalog.Create(ConfigurationManager.ConnectionStrings["MSAccess"].ConnectionString);
}
finally
{
Marshal.ReleaseComObject(catalog);
}
}

return base.Execute();
}

public override void ExecuteBuildScript(IDatabase database, string script)
{
script.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToList().ForEach(s =>
Expand Down
21 changes: 13 additions & 8 deletions PetaPoco.Tests.Integration/PetaPoco.Tests.Integration.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;netcoreapp2.1</TargetFrameworks>
Expand All @@ -17,7 +17,6 @@
<ItemGroup>
<Compile Remove="Databases\SqlServerCeTests\**" />
<Compile Remove="Databases\MSAccessTests\**" Condition="'$(TargetFramework)' == 'netcoreapp2.1'" />
<Compile Remove="DataSources\**" Condition="'$(TargetFramework)' == 'netcoreapp2.1'" />
</ItemGroup>

<ItemGroup>
Expand All @@ -42,6 +41,18 @@
<EmbeddedResource Include="Scripts\SqlServerCeBuildDatabase.sql" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'netcoreapp2.1'">
<COMReference Include="ADOX">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>6</VersionMajor>
<Guid>00000600-0000-0010-8000-00aa006d2ea4</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.0" Condition="'$(TargetFramework)' == 'netcoreapp2.1'" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.1.0" Condition="'$(TargetFramework)' == 'netcoreapp2.1'" />
Expand Down Expand Up @@ -88,10 +99,4 @@
</None>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'netcoreapp2.1'">
<None Update="DataSources\petapoco.accdb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion PetaPoco.Tests.Integration/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<add name="SqlServerMSData" connectionString="Data Source=localhost,5007;Initial Catalog=PetaPocoMsData;User ID=sa;Password=pAtAp0c8" providerName="Microsoft.Data.SqlClient" />
<!-- Env: Local development environment -->
<add name="SQLite" connectionString="Data Source=PetaPoco.sqlite;Version=3;Pooling=True;Max Pool Size=100" providerName="System.Data.SQLite" />
<add name="MSAccess" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|DataSources/petapoco.accdb" providerName="OleDb" />
<add name="MSAccess" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=PetaPoco.accdb" providerName="OleDb" />
<!--TODO: OracleDb-->
<!--TODO: Teradata-->
</connectionStrings>
Expand Down
2 changes: 1 addition & 1 deletion PetaPoco.Tests.Integration/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
},
{
"Name": "MSAccess",
"ConnectionString": "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|DataSources/petapoco.accdb",
"ConnectionString": "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=PetaPoco.accdb",
"ProviderName": "OleDb"
}
]
Expand Down

0 comments on commit 77acb79

Please sign in to comment.