chore: initial commit
This commit is contained in:
134
Interface_WebAPI/DatabaseClient/01_DatabaseClient.csproj
Normal file
134
Interface_WebAPI/DatabaseClient/01_DatabaseClient.csproj
Normal file
@@ -0,0 +1,134 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" 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>{C7C5E929-BE10-4ED6-94DB-B8D65E05E52F}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>DatabaseClient</RootNamespace>
|
||||
<AssemblyName>DatabaseClient</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<Deterministic>true</Deterministic>
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<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' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<StartupObject />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<TargetZone>LocalIntranet</TargetZone>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<GenerateManifests>false</GenerateManifests>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup />
|
||||
<ItemGroup>
|
||||
<Reference Include="BouncyCastle.Crypto, Version=1.8.5.0, Culture=neutral, PublicKeyToken=0e99375e54769942">
|
||||
<HintPath>..\packages\BouncyCastle.1.8.5\lib\BouncyCastle.Crypto.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Google.Protobuf, Version=3.19.4.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Google.Protobuf.3.19.4\lib\net45\Google.Protobuf.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="K4os.Compression.LZ4, Version=1.2.6.0, Culture=neutral, PublicKeyToken=2186fa9121ef231d, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\K4os.Compression.LZ4.1.2.6\lib\net46\K4os.Compression.LZ4.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="K4os.Compression.LZ4.Streams, Version=1.2.6.0, Culture=neutral, PublicKeyToken=2186fa9121ef231d, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\K4os.Compression.LZ4.Streams.1.2.6\lib\net46\K4os.Compression.LZ4.Streams.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="K4os.Hash.xxHash, Version=1.0.6.0, Culture=neutral, PublicKeyToken=32cd54395057cec3, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\K4os.Hash.xxHash.1.0.6\lib\net46\K4os.Hash.xxHash.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="log4net, Version=2.0.14.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\log4net.2.0.14\lib\net45\log4net.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MySql.Data, Version=8.0.29.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MySql.Data.8.0.29\lib\net452\MySql.Data.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.ComponentModel" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Configuration.Install" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data.SQLite, Version=1.0.116.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Stub.System.Data.SQLite.Core.NetFramework.1.0.116.0\lib\net46\System.Data.SQLite.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Management" />
|
||||
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Numerics" />
|
||||
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Transactions" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="Ubiety.Dns.Core, Version=2.2.1.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MySql.Data.8.0.29\lib\net452\Ubiety.Dns.Core.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ZstdNet, Version=1.4.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MySql.Data.8.0.29\lib\net452\ZstdNet.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="DBClient.cs" />
|
||||
<Compile Include="DatabaseFactory.cs" />
|
||||
<Compile Include="DBClientFactory.cs" />
|
||||
<Compile Include="GlobalVar.cs" />
|
||||
<Compile Include="MssqlClient.cs" />
|
||||
<Compile Include="MysqlClient.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SqliteClient.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
<None Include="DBClientLog4net.config">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="..\packages\Stub.System.Data.SQLite.Core.NetFramework.1.0.116.0\build\net46\Stub.System.Data.SQLite.Core.NetFramework.targets" Condition="Exists('..\packages\Stub.System.Data.SQLite.Core.NetFramework.1.0.116.0\build\net46\Stub.System.Data.SQLite.Core.NetFramework.targets')" />
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
<PropertyGroup>
|
||||
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('..\packages\Stub.System.Data.SQLite.Core.NetFramework.1.0.116.0\build\net46\Stub.System.Data.SQLite.Core.NetFramework.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Stub.System.Data.SQLite.Core.NetFramework.1.0.116.0\build\net46\Stub.System.Data.SQLite.Core.NetFramework.targets'))" />
|
||||
</Target>
|
||||
</Project>
|
||||
14
Interface_WebAPI/DatabaseClient/App.config
Normal file
14
Interface_WebAPI/DatabaseClient/App.config
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
|
||||
</startup>
|
||||
<runtime>
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
|
||||
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
|
||||
</dependentAssembly>
|
||||
</assemblyBinding>
|
||||
</runtime>
|
||||
</configuration>
|
||||
62
Interface_WebAPI/DatabaseClient/DBClient.cs
Normal file
62
Interface_WebAPI/DatabaseClient/DBClient.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseClient
|
||||
{
|
||||
public abstract class DBClient
|
||||
{
|
||||
/// <summary>
|
||||
/// 根据连接字符串连接
|
||||
/// </summary>
|
||||
/// <param name="connString">连接字符串</param>
|
||||
public abstract void Connect(string connString);
|
||||
|
||||
/// <summary>
|
||||
/// 执行sql查询语句,返回DataTable
|
||||
/// </summary>
|
||||
/// <param name="sql">查询sql字符串</param>
|
||||
/// <returns></returns>
|
||||
public abstract DataTable ExecQuery(string sql);
|
||||
|
||||
/// <summary>
|
||||
/// 执行sql非查询语句,返回执行结果
|
||||
/// </summary>
|
||||
/// <param name="sql">非查询sql字符串</param>
|
||||
/// <returns></returns>
|
||||
public abstract int ExecNonQuery(string sql);
|
||||
|
||||
/// <summary>
|
||||
/// 分页,将原始表分成子表。约束为当前页数和每页记录数
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型类型</typeparam>
|
||||
/// <param name="originList">原始表</param>
|
||||
/// <param name="pageSize">每页记录数</param>
|
||||
/// <param name="currentPage">当前页数</param>
|
||||
/// <returns></returns>
|
||||
public static List<T> SplitePage<T>(List<T> originList,int pageSize,int currentPage)
|
||||
{
|
||||
List<T> resList = new List<T>();
|
||||
|
||||
var numAll = originList.Count;
|
||||
var numPage = numAll / pageSize + 1;
|
||||
|
||||
var start = (currentPage - 1) * pageSize;
|
||||
var end = start + pageSize;
|
||||
|
||||
for (int i = 0; i < originList.Count; i++)
|
||||
{
|
||||
var origin = originList[i];
|
||||
if (i >= start && i < end)
|
||||
{
|
||||
resList.Add(origin);
|
||||
}
|
||||
}
|
||||
|
||||
return resList;
|
||||
}
|
||||
}
|
||||
}
|
||||
45
Interface_WebAPI/DatabaseClient/DBClientFactory.cs
Normal file
45
Interface_WebAPI/DatabaseClient/DBClientFactory.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseClient
|
||||
{
|
||||
public class DBClientFactory
|
||||
{
|
||||
DatabaseFactory factory;
|
||||
|
||||
public DBClientFactory(string dbTypeStr)
|
||||
{
|
||||
switch (dbTypeStr.ToLower())
|
||||
{
|
||||
case "mysql":
|
||||
{
|
||||
factory = new MysqlDatabaseFactory();
|
||||
break;
|
||||
}
|
||||
case "sqlite":
|
||||
{
|
||||
factory = new SqliteDatabaseFactory();
|
||||
break;
|
||||
}
|
||||
case "mssql":
|
||||
{
|
||||
factory = new MssqlDatabaseFactory();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
factory = new MysqlDatabaseFactory();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DBClient Create()
|
||||
{
|
||||
return factory.Create();
|
||||
}
|
||||
}
|
||||
}
|
||||
34
Interface_WebAPI/DatabaseClient/DBClientLog4net.config
Normal file
34
Interface_WebAPI/DatabaseClient/DBClientLog4net.config
Normal file
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<log4net>
|
||||
<!-- 控制台日志配置 -->
|
||||
<appender name="Console" type="log4net.Appender.ConsoleAppender">
|
||||
<!-- 日志输出格式 -->
|
||||
<layout type="log4net.Layout.PatternLayout">
|
||||
<!--<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />-->
|
||||
<conversionPattern value="%date %-5level %thread %logger - %message%newline" />
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<!-- 文件存储日志配置 -->
|
||||
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
|
||||
<!-- 保存文件的名称 -->
|
||||
<file value="./log/dbclient.log" />
|
||||
<appendToFile value="true" />
|
||||
<!-- 文件的编码方式 -->
|
||||
<param name="Encoding" value="UTF-8"/>
|
||||
<!-- 每个文件的大小 -->
|
||||
<maximumFileSize value="100KB" />
|
||||
<!-- 保存文件数量 -->
|
||||
<maxSizeRollBackups value="2" />
|
||||
<!-- 日志输出格式 -->
|
||||
<layout type="log4net.Layout.PatternLayout">
|
||||
<conversionPattern value="%date %-5level %thread %logger - %message%newline" />
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<root>
|
||||
<level value="ALL" />
|
||||
<appender-ref ref="Console" />
|
||||
<appender-ref ref="RollingFile" />
|
||||
</root>
|
||||
</log4net>
|
||||
37
Interface_WebAPI/DatabaseClient/DatabaseFactory.cs
Normal file
37
Interface_WebAPI/DatabaseClient/DatabaseFactory.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseClient
|
||||
{
|
||||
public abstract class DatabaseFactory
|
||||
{
|
||||
public abstract DBClient Create();
|
||||
}
|
||||
|
||||
class MysqlDatabaseFactory : DatabaseFactory
|
||||
{
|
||||
public override DBClient Create()
|
||||
{
|
||||
return new MysqlClient();
|
||||
}
|
||||
}
|
||||
|
||||
class SqliteDatabaseFactory : DatabaseFactory
|
||||
{
|
||||
public override DBClient Create()
|
||||
{
|
||||
return new SqliteClient();
|
||||
}
|
||||
}
|
||||
|
||||
class MssqlDatabaseFactory : DatabaseFactory
|
||||
{
|
||||
public override DBClient Create()
|
||||
{
|
||||
return new MssqlClient();
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Interface_WebAPI/DatabaseClient/GlobalVar.cs
Normal file
9
Interface_WebAPI/DatabaseClient/GlobalVar.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using log4net;
|
||||
|
||||
namespace DatabaseClient
|
||||
{
|
||||
class GlobalVar
|
||||
{
|
||||
public static ILog log = LogManager.GetLogger("DBClient");
|
||||
}
|
||||
}
|
||||
75
Interface_WebAPI/DatabaseClient/MssqlClient.cs
Normal file
75
Interface_WebAPI/DatabaseClient/MssqlClient.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace DatabaseClient
|
||||
{
|
||||
class MssqlClient : DBClient
|
||||
{
|
||||
SqlConnection conn = null;
|
||||
|
||||
public MssqlClient(){}
|
||||
|
||||
public override void Connect(string connString)
|
||||
{
|
||||
try
|
||||
{
|
||||
conn = new SqlConnection(connString);
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
GlobalVar.log.Error(err.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public override DataTable ExecQuery(string sql)
|
||||
{
|
||||
DataTable dt = null;
|
||||
try
|
||||
{
|
||||
if (conn != null)
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
var cmd = new SqlCommand(sql, conn);
|
||||
SqlDataAdapter da = new SqlDataAdapter(cmd);
|
||||
DataSet ds = new DataSet();
|
||||
da.Fill(ds);
|
||||
|
||||
conn.Close();
|
||||
if (ds.Tables.Count > 0)
|
||||
{
|
||||
dt = ds.Tables[0];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
GlobalVar.log.Error(err.Message);
|
||||
}
|
||||
|
||||
return dt;
|
||||
}
|
||||
|
||||
public override int ExecNonQuery(string sql)
|
||||
{
|
||||
int res = 0;
|
||||
try
|
||||
{
|
||||
if (conn!=null)
|
||||
{
|
||||
var cmd = new SqlCommand(sql, conn);
|
||||
conn.Open();
|
||||
res = cmd.ExecuteNonQuery();
|
||||
conn.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
GlobalVar.log.Error(err.Message);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
126
Interface_WebAPI/DatabaseClient/MysqlClient.cs
Normal file
126
Interface_WebAPI/DatabaseClient/MysqlClient.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseClient
|
||||
{
|
||||
public class MysqlClient : DBClient
|
||||
{
|
||||
MySqlConnection conn;
|
||||
|
||||
public MysqlClient() { }
|
||||
|
||||
public override void Connect(string connString)
|
||||
{
|
||||
try
|
||||
{
|
||||
conn = new MySqlConnection(connString);
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
GlobalVar.log.Error(err.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public override DataTable ExecQuery(string sql)
|
||||
{
|
||||
DataTable dt = null;
|
||||
try
|
||||
{
|
||||
if (conn != null)
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
MySqlCommand command = new MySqlCommand(sql, conn);
|
||||
MySqlDataAdapter da = new MySqlDataAdapter(command);
|
||||
DataSet ds = new DataSet();
|
||||
da.Fill(ds);
|
||||
|
||||
conn.Close();
|
||||
|
||||
if (ds.Tables.Count > 0)
|
||||
{
|
||||
dt = ds.Tables[0];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
GlobalVar.log.Error(err.Message);
|
||||
}
|
||||
return dt;
|
||||
}
|
||||
|
||||
public override int ExecNonQuery(string sql)
|
||||
{
|
||||
int res = 0;
|
||||
|
||||
try
|
||||
{
|
||||
if (conn != null)
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
MySqlCommand command = new MySqlCommand(sql, conn);
|
||||
res = command.ExecuteNonQuery();
|
||||
|
||||
conn.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
GlobalVar.log.Error(err.Message);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public static bool ExecQueryDataTable(string sql,string ConnectStr,out DataTable dt, out string err)
|
||||
{
|
||||
bool success = false;
|
||||
err = "";
|
||||
dt = null;
|
||||
using (MySqlConnection conn = new MySqlConnection(ConnectStr))
|
||||
{
|
||||
try
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
MySqlCommand command = new MySqlCommand(sql, conn);
|
||||
MySqlDataAdapter da = new MySqlDataAdapter(command);
|
||||
DataSet ds = new DataSet();
|
||||
da.Fill(ds);
|
||||
|
||||
if (ds.Tables.Count > 0)
|
||||
{
|
||||
dt = ds.Tables[0];
|
||||
}
|
||||
success = true;
|
||||
}
|
||||
catch (Exception er)
|
||||
{
|
||||
err = er.Message;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (conn != null)
|
||||
{
|
||||
conn.Close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
40
Interface_WebAPI/DatabaseClient/Program.cs
Normal file
40
Interface_WebAPI/DatabaseClient/Program.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseClient
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
|
||||
// TestSqlite();
|
||||
TestMysql();
|
||||
//TestMssql();
|
||||
Console.ReadKey();
|
||||
}
|
||||
static void TestSqlite()
|
||||
{
|
||||
var sqlClient = new DBClientFactory("Sqlite").Create();
|
||||
sqlClient.Connect("Data Source=mytest.sqlite;Version=3;");
|
||||
var ds = sqlClient.ExecQuery("select * from Z_Save_Position");
|
||||
}
|
||||
static void TestMysql()
|
||||
{
|
||||
var sqlClient = new DBClientFactory("Mysql").Create();
|
||||
sqlClient.Connect("server=192.168.1.204;port=33060;database=zkeco;username=mesuser;password=Mes147258;");
|
||||
var ds = sqlClient.ExecQuery("select * from vw_mes_userinfo;");
|
||||
}
|
||||
|
||||
static void TestMssql()
|
||||
{
|
||||
var sqlClient = new DBClientFactory("Mssql").Create();
|
||||
|
||||
sqlClient.Connect(@"server=.\WINCC;database=MW_DataFactory_App_Add;uid=sa;pwd=126.com;Connection Reset=FALSE;Max Pool Size = 1000");
|
||||
var ds = sqlClient.ExecQuery("select * from ProjectList");
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Interface_WebAPI/DatabaseClient/Properties/AssemblyInfo.cs
Normal file
38
Interface_WebAPI/DatabaseClient/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
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("Database")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Database")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2022")]
|
||||
[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("c7c5e929-be10-4ed6-94db-b8d65e05e52f")]
|
||||
|
||||
// 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")]
|
||||
|
||||
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "DBClientLog4net.config", Watch = true)]
|
||||
83
Interface_WebAPI/DatabaseClient/SqliteClient.cs
Normal file
83
Interface_WebAPI/DatabaseClient/SqliteClient.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using System.Data.SQLite;
|
||||
|
||||
|
||||
|
||||
namespace DatabaseClient
|
||||
{
|
||||
class SqliteClient : DBClient
|
||||
{
|
||||
SQLiteConnection conn;
|
||||
|
||||
public SqliteClient() { }
|
||||
|
||||
public override void Connect(string connString)
|
||||
{
|
||||
try
|
||||
{
|
||||
conn = new SQLiteConnection(connString);
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
GlobalVar.log.Error(err.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public override int ExecNonQuery(string sql)
|
||||
{
|
||||
int res = 0;
|
||||
try
|
||||
{
|
||||
if (conn!=null)
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
var cmd = new SQLiteCommand(sql, conn);
|
||||
res = cmd.ExecuteNonQuery();
|
||||
|
||||
conn.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
GlobalVar.log.Error(err.Message);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public override DataTable ExecQuery(string sql)
|
||||
{
|
||||
DataTable dt = null;
|
||||
try
|
||||
{
|
||||
if (conn!=null)
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
var cmd = new SQLiteCommand(sql, conn);
|
||||
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
|
||||
DataSet ds = new DataSet();
|
||||
da.Fill(ds);
|
||||
conn.Close();
|
||||
if (ds.Tables.Count > 0)
|
||||
{
|
||||
dt = ds.Tables[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
GlobalVar.log.Error(err.Message);
|
||||
}
|
||||
|
||||
|
||||
return dt;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
Interface_WebAPI/DatabaseClient/packages.config
Normal file
16
Interface_WebAPI/DatabaseClient/packages.config
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="BouncyCastle" version="1.8.5" targetFramework="net461" />
|
||||
<package id="Google.Protobuf" version="3.19.4" targetFramework="net461" />
|
||||
<package id="K4os.Compression.LZ4" version="1.2.6" targetFramework="net461" />
|
||||
<package id="K4os.Compression.LZ4.Streams" version="1.2.6" targetFramework="net461" />
|
||||
<package id="K4os.Hash.xxHash" version="1.0.6" targetFramework="net461" />
|
||||
<package id="MySql.Data" version="8.0.29" targetFramework="net461" requireReinstallation="true" />
|
||||
<package id="Stub.System.Data.SQLite.Core.NetFramework" version="1.0.116.0" targetFramework="net461" />
|
||||
<package id="System.Buffers" version="4.5.1" targetFramework="net461" />
|
||||
<package id="System.Data.SQLite.Core" version="1.0.116.0" targetFramework="net461" />
|
||||
<package id="System.Memory" version="4.5.4" targetFramework="net461" />
|
||||
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net461" />
|
||||
<package id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" targetFramework="net461" />
|
||||
<package id="log4net" version="2.0.14" targetFramework="net461" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user