Initial commit
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
namespace Service.Core
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using System.Reflection;
|
||||
|
||||
[assembly: AssemblyTitle("Service.Core")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.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>{F7E3DF42-4D35-4A0C-9182-90438F9956E1}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Xylem.Common.Logic.ServiceCore</RootNamespace>
|
||||
<AssemblyName>Xylem.Common.Logic.ServiceCore</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</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>
|
||||
<LangVersion>7.0</LangVersion>
|
||||
</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>
|
||||
<LangVersion>7.0</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == '_MANUAL_CONTROL|AnyCPU'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\_MANUAL_CONTROL\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<LangVersion>7.0</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\..\.shared\SharedAssemblyInfo.cs">
|
||||
<Link>Properties\SharedAssemblyInfo.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="DatabaseApiController.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SqlDataAccess.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<WCFMetadata Include="Service References\" />
|
||||
</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>
|
||||
@@ -0,0 +1,305 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Service.Core
|
||||
{
|
||||
public class SqlDataAccess : IDisposable
|
||||
{
|
||||
private string _connectionString = "";
|
||||
public SqlDataAccess(string ConnectionString)
|
||||
{
|
||||
_connectionString = ConnectionString;
|
||||
}
|
||||
|
||||
public string GenerateParam<T>(string name, string typeName,T value)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine($"Declare @{name} as {typeName}" );
|
||||
|
||||
if (value is string)
|
||||
{
|
||||
sb.AppendLine($"set @{name} = '%{value.ToString()}%' ");
|
||||
return sb.ToString();
|
||||
}
|
||||
sb.AppendLine($"set @{name} = ");
|
||||
if (value == null)
|
||||
{
|
||||
sb.Append(" null ");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
if (value is int)
|
||||
{
|
||||
var tmp = Convert.ToInt32(value);
|
||||
sb.Append(tmp.ToString());
|
||||
}
|
||||
if (value is bool)
|
||||
{
|
||||
var tmp = Convert.ToBoolean(value);
|
||||
|
||||
sb.Append(tmp ? "1" : "0");
|
||||
}
|
||||
|
||||
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
private SqlConnection _con;
|
||||
|
||||
/// <summary>
|
||||
/// Creating a database connection if there is none.
|
||||
/// </summary>
|
||||
/// <remarks>Working with a global connection variable to make sure the connection would be closed at the end of working with it.</remarks>
|
||||
private void CreateConnection()
|
||||
{
|
||||
if (_con == null)
|
||||
{
|
||||
_con = new SqlConnection(_connectionString);
|
||||
|
||||
}
|
||||
if (_con != null && _con.State == ConnectionState.Closed)
|
||||
{
|
||||
_con.Open();
|
||||
}
|
||||
}
|
||||
|
||||
public DataTable ExecuteQuery(string sqlStatement, List<SqlParameter> parameter = null)
|
||||
{
|
||||
DataTable ret;
|
||||
SqlConnection connection = null;
|
||||
SqlDataReader reader = null;
|
||||
|
||||
try
|
||||
{
|
||||
connection = new SqlConnection(_connectionString);
|
||||
|
||||
if (connection.State != ConnectionState.Open)
|
||||
{
|
||||
connection.Open();
|
||||
}
|
||||
var cmd = connection.CreateCommand();
|
||||
cmd.CommandTimeout = 120;
|
||||
cmd.CommandText = sqlStatement;
|
||||
if (parameter != null)
|
||||
{
|
||||
foreach (var item in parameter)
|
||||
{
|
||||
cmd.Parameters.Add(item);
|
||||
}
|
||||
}
|
||||
reader = cmd.ExecuteReader();
|
||||
var entries = new object[reader.FieldCount];
|
||||
ret = new DataTable();
|
||||
|
||||
var jcount = 0;
|
||||
try
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
if (jcount == 0)
|
||||
{
|
||||
for (var i = 0; i < reader.FieldCount; i++)
|
||||
{
|
||||
var headline = reader.GetName(i);
|
||||
if (!ret.Columns.Contains(headline))
|
||||
{
|
||||
ret.Columns.Add(headline, Type.GetType("System.Object"));
|
||||
}
|
||||
else
|
||||
{
|
||||
ret.Columns.Add(headline + Guid.NewGuid().ToString(), Type.GetType("System.Object"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
for (var i = 0; i < reader.FieldCount; i++)
|
||||
{
|
||||
entries[i] = reader[i];
|
||||
}
|
||||
jcount++;
|
||||
ret.Rows.Add(entries);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw (new Exception(ex.Message, ex));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw (new Exception(ex.Message, ex));
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
if (reader != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
reader.Dispose();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
if (connection != null && connection.State == ConnectionState.Open)
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { throw (new Exception("Unhandled exception within finally: " + ex.Message)); }
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
/// <summary>
|
||||
/// Executing Stored Procedure
|
||||
/// </summary>
|
||||
/// <param name="storedProcedure"> The Name of Stored Procedure</param>
|
||||
/// <param name="values">Parameter names and values</param>
|
||||
/// <returns>Result Set</returns>
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Closing and disposing the connection.
|
||||
/// </summary>
|
||||
private void CloseConnection()
|
||||
{
|
||||
if (_con != null)
|
||||
{
|
||||
if (_con.State != ConnectionState.Closed)
|
||||
{
|
||||
_con.Close();
|
||||
}
|
||||
_con.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generating a list of comma seperated parameters by a given string array.
|
||||
/// </summary>
|
||||
/// <param name="parameter">array to work on</param>
|
||||
/// <returns>comma seperated string with parameters.</returns>
|
||||
private string GenerateParameter(string[] parameter)
|
||||
{
|
||||
var list = new StringBuilder();
|
||||
var ret = "";
|
||||
var first = true;
|
||||
|
||||
foreach (var param in parameter)
|
||||
{
|
||||
if (list.ToString().Length > 0)
|
||||
{
|
||||
list.Append(",");
|
||||
}
|
||||
|
||||
var result = param;
|
||||
var content = param;
|
||||
const string mitpat = @"(?:^@\w[\w_\s]*=[\s]*(?:'(.*)'$|(.*)$)|^(?:'(.*)'$|(.*)$))";
|
||||
const string numbpat = @"^[+-]?(?:\d+\.?|,?\d*|\d*(\.?|,?)\d+)[\r\n]*$";
|
||||
|
||||
if (!first || !result.Contains(","))
|
||||
{
|
||||
first = false;
|
||||
|
||||
var mtch = Regex.Match(content, mitpat);
|
||||
if (mtch.Groups[1].Success)
|
||||
{
|
||||
var orig = mtch.Groups[1].Value;
|
||||
var mch = Regex.Replace(orig, "[']+", "'");
|
||||
mch = Regex.Replace(mch, "[']+", "''");
|
||||
|
||||
result = !String.IsNullOrEmpty(mch) ? content.Replace(orig, mch) : content;
|
||||
}
|
||||
else if (mtch.Groups[2].Success)
|
||||
{
|
||||
var orig = mtch.Groups[2].Value;
|
||||
var mch = Regex.Replace(orig, "[']+", "'");
|
||||
mch = Regex.Replace(mch, "[']+", "''");
|
||||
|
||||
if (!String.IsNullOrEmpty(mch))
|
||||
{
|
||||
if (Regex.IsMatch(mch, numbpat) || mch.ToLower() == "null")
|
||||
{
|
||||
result = content;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = content.Replace(orig, "'" + mch + "'");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = content;
|
||||
}
|
||||
}
|
||||
else if (mtch.Groups[3].Success)
|
||||
{
|
||||
var orig = mtch.Groups[3].Value;
|
||||
var mch = Regex.Replace(orig, "[']+", "'");
|
||||
mch = Regex.Replace(mch, "[']+", "''");
|
||||
|
||||
result = !String.IsNullOrEmpty(mch) ? content.Replace(orig, mch) : content;
|
||||
}
|
||||
else if (mtch.Groups[4].Success)
|
||||
{
|
||||
var orig = mtch.Groups[4].Value;
|
||||
var mch = Regex.Replace(orig, "[']+", "'");
|
||||
mch = Regex.Replace(mch, "[']+", "''");
|
||||
|
||||
if (!String.IsNullOrEmpty(mch))
|
||||
{
|
||||
if (Regex.IsMatch(mch, numbpat) || mch.ToLower() == "null")
|
||||
{
|
||||
result = content;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = content.Replace(orig, "'" + mch + "'");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = content;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
list.Append(result);
|
||||
}
|
||||
if (list.ToString().Length > 0)
|
||||
{
|
||||
ret = " " + list;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public string GetConnectionString()
|
||||
{
|
||||
//if (_conifg.SupportConnectionString)
|
||||
//{
|
||||
return _connectionString;
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
//StringBuilder ret = new StringBuilder();
|
||||
//ret.Append("Data Source=").Append(_conifg.ReadFromConfig("Data Source", "Connection"));
|
||||
//ret.Append(";Initial Catalog=").Append(_conifg.ReadFromConfig("Initial Catalog", "Connection"));
|
||||
//ret.Append(";User id=").Append(_conifg.ReadFromConfig("User id", "Connection"));
|
||||
//ret.Append(";Password=").Append(_conifg.ReadFromConfig("Password", "Connection"));
|
||||
//return ret.ToString();
|
||||
//}
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
CloseConnection();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user