Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加了对Access数据库的支持; #4

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/DbUtility.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ivony.Data.Logs", "Ivony.Da
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PerformanceTest", "PerformanceTest\PerformanceTest.csproj", "{EF894E7A-779D-483C-89F1-61541833DD34}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ivony.Data.Access", "Ivony.Data.Access\Ivony.Data.Access.csproj", "{B812CDE1-193C-4741-AEDC-D791A6848ABC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,6 +53,10 @@ Global
{EF894E7A-779D-483C-89F1-61541833DD34}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF894E7A-779D-483C-89F1-61541833DD34}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF894E7A-779D-483C-89F1-61541833DD34}.Release|Any CPU.Build.0 = Release|Any CPU
{B812CDE1-193C-4741-AEDC-D791A6848ABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B812CDE1-193C-4741-AEDC-D791A6848ABC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B812CDE1-193C-4741-AEDC-D791A6848ABC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B812CDE1-193C-4741-AEDC-D791A6848ABC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
16 changes: 16 additions & 0 deletions Sources/Ivony.Data.Access/AccessClient/AccessDbConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ivony.Data;

namespace Ivony.Data.Access.AccessClient
{
public class AccessDbConfiguration:DbConfiguration
{
public AccessDbConfiguration()
{
}
}
}
28 changes: 28 additions & 0 deletions Sources/Ivony.Data.Access/AccessClient/AccessDbExecuteContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ivony.Data.Common;
using System.Data.OleDb;

namespace Ivony.Data.Access.AccessClient
{
public class AccessDbExecuteContext:DbExecuteContextBase
{

public AccessDbExecuteContext(OleDbDataReader dataReader, IDbTracing tracing, object sync)
: base(dataReader, tracing, sync: sync)
{
AcccessDataReader = dataReader;
}


public OleDbDataReader AcccessDataReader
{
get;
private set;
}

}
}
73 changes: 73 additions & 0 deletions Sources/Ivony.Data.Access/AccessClient/AccessDbExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using Ivony.Data.Common;
using Ivony.Data.Queries;

namespace Ivony.Data.Access.AccessClient
{
public class AccessDbExecutor : DbExecutorBase, IDbExecutor<ParameterizedQuery>
{

public AccessDbExecutor(string connectionString, AccessDbConfiguration configuration)
: base(configuration)
{
if(string.IsNullOrEmpty(connectionString))
throw new ArgumentNullException("connectionString");
if(configuration == null)
throw new ArgumentNullException("configuration");

Configuration = configuration;
Connection = new OleDbConnection(connectionString);
}


public OleDbConnection Connection { get; private set; }


protected AccessDbConfiguration Configuration { get; private set; }

public object SyncRoot { get; private set; }

public IDbExecuteContext Execute(ParameterizedQuery query)
{
var tracing = TryCreateTracing(this, query);
var command = new AccessDbParameterizedQueryParser().Parse(query);
return Execue(command, tracing);
}

private IDbExecuteContext Execue(OleDbCommand command, IDbTracing tracing)
{
try
{
TryExecuteTracing(tracing, t => t.OnExecuting(command));

if (Connection.State == ConnectionState.Closed)
Connection.Open();
command.Connection = Connection;

if (Configuration.QueryExecutingTimeout.HasValue)
command.CommandTimeout = (int)Configuration.QueryExecutingTimeout.Value.TotalSeconds;

var context = new AccessDbExecuteContext(command.ExecuteReader(), tracing, SyncRoot);

TryExecuteTracing(tracing, t => t.OnLoadingData(context));
return context;

}
catch (DbException exception)
{
TryExecuteTracing(tracing, t => t.OnException(exception));
throw;
}
}



}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ivony.Data;
using Ivony.Data.Common;

namespace Ivony.Data.Access.AccessClient
{
public class AccessDbParameterizedQueryParser:ParameterizedQueryParser<OleDbCommand, OleDbParameter>
{
protected override string GetParameterPlaceholder(object value, int index, out OleDbParameter parameter)
{
var name = "?";
parameter = new OleDbParameter(name, value);

return name;
}

protected override OleDbCommand CreateCommand(string commandText, OleDbParameter[] parameters)
{
var command = new OleDbCommand(commandText);
command.Parameters.AddRange(parameters);

return command;
}
}
}
91 changes: 91 additions & 0 deletions Sources/Ivony.Data.Access/AccessDb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.OleDb;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ivony.Data.Access.AccessClient;
namespace Ivony.Data.Access
{
public static class AccessDb
{
static AccessDb()
{
DefaultConfiguration = new AccessDbConfiguration();
}

public static AccessDbConfiguration DefaultConfiguration { get; private set; }

public static AccessDbExecutor ConnectFile(string filePath, bool create = true, AccessDbConfiguration configuration = null)
{
if (string.IsNullOrEmpty(filePath))
return null;

if (!File.Exists(filePath))
{
if (create)
CreateAccessDb(filePath);
else
{
throw new FileNotFoundException("The database file can't exist.");
}
}
var builder = new OleDbConnectionStringBuilder
{
DataSource = filePath,
Provider = "Microsoft.JET.OLEDB.4.0",
PersistSecurityInfo = false,
};

return Connect(builder.ConnectionString, configuration ?? DefaultConfiguration);
}

public static AccessDbExecutor Connect(string connectionString, AccessDbConfiguration configuration = null)
{
return new AccessDbExecutor(connectionString, configuration);
}

public static bool CreateAccessDb(string filePath, bool overwrite = false)
{
if (string.IsNullOrEmpty(filePath))
throw new ArgumentNullException(filePath);

if (File.Exists(filePath))
{
if(overwrite)
File.Delete(filePath);
else
{
throw new InvalidOperationException("File Already existed.");
}
}

try
{

//Reference:http://www.cnblogs.com/DasonKwok/archive/2012/08/02/2620194.html
var assembly = System.Reflection.Assembly.GetExecutingAssembly();

var stream = assembly.GetManifestResourceStream("Ivony.Data.Access.EmbedResource.emptyDb.mdb");
if (stream != null)
{
var dataLength = stream.Length;
var buffer = new Byte[dataLength];
stream.Read(buffer, 0, (int)dataLength);
using (var writer = new FileStream(filePath, FileMode.Create))
{
writer.Write(buffer, 0, (int)dataLength);
}
}
return true;
}
catch
{
return false;
}
}
}
}
Binary file not shown.
66 changes: 66 additions & 0 deletions Sources/Ivony.Data.Access/Ivony.Data.Access.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{B812CDE1-193C-4741-AEDC-D791A6848ABC}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Ivony.Data.Access</RootNamespace>
<AssemblyName>Ivony.Data.Access</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AccessClient\AccessDbConfiguration.cs" />
<Compile Include="AccessClient\AccessDbExecuteContext.cs" />
<Compile Include="AccessClient\AccessDbExecutor.cs" />
<Compile Include="AccessClient\AccessDbParameterizedQueryParser.cs" />
<Compile Include="AccessDb.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Ivony.Data\Ivony.Data.csproj">
<Project>{27d46d02-f7bc-42b2-ae3e-33eb5b1fa3f9}</Project>
<Name>Ivony.Data</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="EmbedResource\emptyDb.mdb" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions Sources/Ivony.Data.Access/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Ivony.Data.Access")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Ivony.Data.Access")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("90dfc4c2-f50c-4d1a-aecd-b31c04397dbc")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading