添加项目文件。
This commit is contained in:
14
LeadDataBaseUtility/App.config
Normal file
14
LeadDataBaseUtility/App.config
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<configSections>
|
||||
|
||||
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
|
||||
</configSections>
|
||||
<entityFramework>
|
||||
<providers>
|
||||
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
|
||||
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
|
||||
</providers>
|
||||
</entityFramework>
|
||||
|
||||
</configuration>
|
||||
26
LeadDataBaseUtility/LeadConfigureConnectionString.cs
Normal file
26
LeadDataBaseUtility/LeadConfigureConnectionString.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LeadDataBaseUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取配置文件中的连接字符串
|
||||
/// </summary>
|
||||
public class LeadConfigureConnectionString
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取配置文件中的连接字符串
|
||||
/// </summary>
|
||||
/// <param name="connStrType">数据库连接字符串类型</param>
|
||||
/// <returns></returns>
|
||||
public static string getConnectionString(string connStrType)
|
||||
{
|
||||
string _connectionString = ConfigurationManager.AppSettings[connStrType];
|
||||
return _connectionString;
|
||||
}
|
||||
}
|
||||
}
|
||||
123
LeadDataBaseUtility/LeadDataBaseUtility.csproj
Normal file
123
LeadDataBaseUtility/LeadDataBaseUtility.csproj
Normal file
@@ -0,0 +1,123 @@
|
||||
<?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>{824348A6-FFFF-4C1B-BBF5-8B507A8C8CE5}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>LeadDataBaseUtility</RootNamespace>
|
||||
<AssemblyName>LeadDataBaseUtility</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
</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>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
|
||||
</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>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\x64\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||
<OutputPath>bin\x64\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\x86\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
|
||||
<OutputPath>bin\x86\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="MySql.Data, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\public\MySql.Data.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Oracle.ManagedDataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\public\Oracle.ManagedDataAccess.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data.SQLite, Version=1.0.113.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=AMD64">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\public\System.Data.SQLite.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="LeadConfigureConnectionString.cs" />
|
||||
<Compile Include="LeadDbHelperMySQL.cs" />
|
||||
<Compile Include="LeadDbHelperOracle.cs" />
|
||||
<Compile Include="LeadDbHelperSQLite.cs" />
|
||||
<Compile Include="LeadDbHelperSQLServer.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\NX_LogUtility\NX_LogUtility.csproj">
|
||||
<Project>{925057D8-7EE8-42ED-8AA0-628AF82F10B2}</Project>
|
||||
<Name>NX_LogUtility</Name>
|
||||
</ProjectReference>
|
||||
</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>
|
||||
636
LeadDataBaseUtility/LeadDbHelperMySQL.cs
Normal file
636
LeadDataBaseUtility/LeadDbHelperMySQL.cs
Normal file
@@ -0,0 +1,636 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LeadDataBaseUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// Copyright (C) LEAD
|
||||
/// 数据访问抽象基础类(基于MySql)
|
||||
/// 允许修改、添加满足自己项目的需要。
|
||||
/// 添加、修改后请详细注释。违者会强制删除不予采用。
|
||||
/// </summary>
|
||||
public abstract class LeadDbHelperMySQL
|
||||
{
|
||||
public static string connectionString = LeadConfigureConnectionString.getConnectionString("LeadMySql");
|
||||
public LeadDbHelperMySQL()
|
||||
{
|
||||
}
|
||||
|
||||
#region 公用方法
|
||||
/// <summary>
|
||||
/// 得到最大值
|
||||
/// </summary>
|
||||
/// <param name="FieldName"></param>
|
||||
/// <param name="TableName"></param>
|
||||
/// <returns></returns>
|
||||
public static int GetMaxID(string FieldName, string TableName)
|
||||
{
|
||||
string strsql = "select max(" + FieldName + ")+1 from " + TableName;
|
||||
object obj = GetSingle(strsql);
|
||||
if (obj == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return int.Parse(obj.ToString());
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 是否存在
|
||||
/// </summary>
|
||||
/// <param name="strSql"></param>
|
||||
/// <returns></returns>
|
||||
public static bool Exists(string strSql)
|
||||
{
|
||||
object obj = GetSingle(strSql);
|
||||
int cmdresult;
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
cmdresult = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmdresult = int.Parse(obj.ToString());
|
||||
}
|
||||
if (cmdresult == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 是否存在(基于MySqlParameter)
|
||||
/// </summary>
|
||||
/// <param name="strSql"></param>
|
||||
/// <param name="cmdParms"></param>
|
||||
/// <returns></returns>
|
||||
public static bool Exists(string strSql, params MySqlParameter[] cmdParms)
|
||||
{
|
||||
object obj = GetSingle(strSql, cmdParms);
|
||||
int cmdresult;
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
cmdresult = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmdresult = int.Parse(obj.ToString());
|
||||
}
|
||||
if (cmdresult == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 执行简单SQL语句
|
||||
|
||||
/// <summary>
|
||||
/// 执行SQL语句,返回影响的记录数
|
||||
/// </summary>
|
||||
/// <param name="SQLString">SQL语句</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteSql(string SQLString)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
return rows;
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
connection.Close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static int ExecuteSqlByTime(string SQLString, int Times)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
cmd.CommandTimeout = Times;
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
return rows;
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
connection.Close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行多条SQL语句,实现数据库事务。
|
||||
/// </summary>
|
||||
/// <param name="SQLStringList">多条SQL语句</param>
|
||||
public static int ExecuteSqlTran(List<String> SQLStringList)
|
||||
{
|
||||
using (MySqlConnection conn = new MySqlConnection(connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
MySqlCommand cmd = new MySqlCommand();
|
||||
cmd.Connection = conn;
|
||||
MySqlTransaction tx = conn.BeginTransaction();
|
||||
cmd.Transaction = tx;
|
||||
try
|
||||
{
|
||||
int count = 0;
|
||||
for (int n = 0; n < SQLStringList.Count; n++)
|
||||
{
|
||||
string strsql = SQLStringList[n];
|
||||
if (strsql.Trim().Length > 1)
|
||||
{
|
||||
cmd.CommandText = strsql;
|
||||
count += cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
tx.Commit();
|
||||
return count;
|
||||
}
|
||||
catch
|
||||
{
|
||||
tx.Rollback();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行带一个存储过程参数的的SQL语句。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">SQL语句</param>
|
||||
/// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteSql(string SQLString, string content)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
MySqlCommand cmd = new MySqlCommand(SQLString, connection);
|
||||
MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@content", SqlDbType.NText);
|
||||
myParameter.Value = content;
|
||||
cmd.Parameters.Add(myParameter);
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
return rows;
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行带一个存储过程参数的的SQL语句。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">SQL语句</param>
|
||||
/// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static object ExecuteSqlGet(string SQLString, string content)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
MySqlCommand cmd = new MySqlCommand(SQLString, connection);
|
||||
MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@content", SqlDbType.NText);
|
||||
myParameter.Value = content;
|
||||
cmd.Parameters.Add(myParameter);
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
object obj = cmd.ExecuteScalar();
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
|
||||
/// </summary>
|
||||
/// <param name="strSQL">SQL语句</param>
|
||||
/// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
MySqlCommand cmd = new MySqlCommand(strSQL, connection);
|
||||
MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@fs", SqlDbType.Image);
|
||||
myParameter.Value = fs;
|
||||
cmd.Parameters.Add(myParameter);
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
return rows;
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行一条计算查询结果语句,返回查询结果(object)。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">计算查询结果语句</param>
|
||||
/// <returns>查询结果(object)</returns>
|
||||
public static object GetSingle(string SQLString)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
object obj = cmd.ExecuteScalar();
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
connection.Close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public static object GetSingle(string SQLString, int Times)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
cmd.CommandTimeout = Times;
|
||||
object obj = cmd.ExecuteScalar();
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
connection.Close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close )
|
||||
/// </summary>
|
||||
/// <param name="strSQL">查询语句</param>
|
||||
/// <returns>MySqlDataReader</returns>
|
||||
public static MySqlDataReader ExecuteReader(string strSQL)
|
||||
{
|
||||
MySqlConnection connection = new MySqlConnection(connectionString);
|
||||
MySqlCommand cmd = new MySqlCommand(strSQL, connection);
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
return myReader;
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回DataSet
|
||||
/// </summary>
|
||||
/// <param name="SQLString">查询语句</param>
|
||||
/// <returns>DataSet</returns>
|
||||
public static DataSet Query(string SQLString)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
|
||||
command.Fill(ds, "ds");
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
public static DataSet Query(string SQLString, int Times)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
|
||||
command.SelectCommand.CommandTimeout = Times;
|
||||
command.Fill(ds, "ds");
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region 执行带参数的SQL语句
|
||||
|
||||
/// <summary>
|
||||
/// 执行SQL语句,返回影响的记录数
|
||||
/// </summary>
|
||||
/// <param name="SQLString">SQL语句</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteSql(string SQLString, params MySqlParameter[] cmdParms)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
using (MySqlCommand cmd = new MySqlCommand())
|
||||
{
|
||||
try
|
||||
{
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
cmd.Parameters.Clear();
|
||||
return rows;
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 执行多条SQL语句,实现数据库事务。
|
||||
/// </summary>
|
||||
/// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[])</param>
|
||||
public static void ExecuteSqlTran(Hashtable SQLStringList)
|
||||
{
|
||||
using (MySqlConnection conn = new MySqlConnection(connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
using (MySqlTransaction trans = conn.BeginTransaction())
|
||||
{
|
||||
MySqlCommand cmd = new MySqlCommand();
|
||||
try
|
||||
{
|
||||
//循环
|
||||
foreach (DictionaryEntry myDE in SQLStringList)
|
||||
{
|
||||
string cmdText = myDE.Key.ToString();
|
||||
MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Value;
|
||||
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
|
||||
int val = cmd.ExecuteNonQuery();
|
||||
cmd.Parameters.Clear();
|
||||
}
|
||||
trans.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
trans.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行多条SQL语句,实现数据库事务。
|
||||
/// </summary>
|
||||
/// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[])</param>
|
||||
public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList)
|
||||
{
|
||||
using (MySqlConnection conn = new MySqlConnection(connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
using (MySqlTransaction trans = conn.BeginTransaction())
|
||||
{
|
||||
MySqlCommand cmd = new MySqlCommand();
|
||||
try
|
||||
{
|
||||
int indentity = 0;
|
||||
//循环
|
||||
foreach (DictionaryEntry myDE in SQLStringList)
|
||||
{
|
||||
string cmdText = myDE.Key.ToString();
|
||||
MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Value;
|
||||
foreach (MySqlParameter q in cmdParms)
|
||||
{
|
||||
if (q.Direction == ParameterDirection.InputOutput)
|
||||
{
|
||||
q.Value = indentity;
|
||||
}
|
||||
}
|
||||
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
|
||||
int val = cmd.ExecuteNonQuery();
|
||||
foreach (MySqlParameter q in cmdParms)
|
||||
{
|
||||
if (q.Direction == ParameterDirection.Output)
|
||||
{
|
||||
indentity = Convert.ToInt32(q.Value);
|
||||
}
|
||||
}
|
||||
cmd.Parameters.Clear();
|
||||
}
|
||||
trans.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
trans.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行一条计算查询结果语句,返回查询结果(object)。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">计算查询结果语句</param>
|
||||
/// <returns>查询结果(object)</returns>
|
||||
public static object GetSingle(string SQLString, params MySqlParameter[] cmdParms)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
using (MySqlCommand cmd = new MySqlCommand())
|
||||
{
|
||||
try
|
||||
{
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
object obj = cmd.ExecuteScalar();
|
||||
cmd.Parameters.Clear();
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close )
|
||||
/// </summary>
|
||||
/// <param name="strSQL">查询语句</param>
|
||||
/// <returns>MySqlDataReader</returns>
|
||||
public static MySqlDataReader ExecuteReader(string SQLString, params MySqlParameter[] cmdParms)
|
||||
{
|
||||
MySqlConnection connection = new MySqlConnection(connectionString);
|
||||
MySqlCommand cmd = new MySqlCommand();
|
||||
try
|
||||
{
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
cmd.Parameters.Clear();
|
||||
return myReader;
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回DataSet
|
||||
/// </summary>
|
||||
/// <param name="SQLString">查询语句</param>
|
||||
/// <returns>DataSet</returns>
|
||||
public static DataSet Query(string SQLString, params MySqlParameter[] cmdParms)
|
||||
{
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
MySqlCommand cmd = new MySqlCommand();
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
da.Fill(ds, "ds");
|
||||
cmd.Parameters.Clear();
|
||||
}
|
||||
catch (MySql.Data.MySqlClient.MySqlException ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, string cmdText, MySqlParameter[] cmdParms)
|
||||
{
|
||||
if (conn.State != ConnectionState.Open)
|
||||
conn.Open();
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = cmdText;
|
||||
if (trans != null)
|
||||
cmd.Transaction = trans;
|
||||
cmd.CommandType = CommandType.Text;//cmdType;
|
||||
if (cmdParms != null)
|
||||
{
|
||||
|
||||
|
||||
foreach (MySqlParameter parameter in cmdParms)
|
||||
{
|
||||
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
|
||||
(parameter.Value == null))
|
||||
{
|
||||
parameter.Value = DBNull.Value;
|
||||
}
|
||||
cmd.Parameters.Add(parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
581
LeadDataBaseUtility/LeadDbHelperOracle.cs
Normal file
581
LeadDataBaseUtility/LeadDbHelperOracle.cs
Normal file
@@ -0,0 +1,581 @@
|
||||
using Oracle.ManagedDataAccess.Client;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LeadDataBaseUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// Copyright20181102 (C) sunyalong
|
||||
/// 数据访问抽象基础类(基于Oracle.ManagedDataAccess)
|
||||
/// 允许修改、添加满足自己项目的需要。
|
||||
/// 添加、修改后请详细注释。违者会强制删除不予采用。
|
||||
/// </summary>
|
||||
public abstract class LeadDbHelperOracle
|
||||
{
|
||||
public static string connectionString = LeadConfigureConnectionString.getConnectionString("LeadOracle");
|
||||
public LeadDbHelperOracle()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#region 公用方法
|
||||
|
||||
public static int GetMaxID(string FieldName, string TableName)
|
||||
{
|
||||
string strsql = "select max(" + FieldName + ")+1 from " + TableName;
|
||||
object obj = GetSingle(strsql);
|
||||
if (obj == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return int.Parse(obj.ToString());
|
||||
}
|
||||
}
|
||||
public static bool Exists(string strSql)
|
||||
{
|
||||
object obj = GetSingle(strSql);
|
||||
int cmdresult;
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
cmdresult = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmdresult = int.Parse(obj.ToString());
|
||||
}
|
||||
if (cmdresult == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Exists(string strSql, params OracleParameter[] cmdParms)
|
||||
{
|
||||
object obj = GetSingle(strSql, cmdParms);
|
||||
int cmdresult;
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
cmdresult = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmdresult = int.Parse(obj.ToString());
|
||||
}
|
||||
if (cmdresult == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region 执行简单SQL语句
|
||||
|
||||
/// <summary>
|
||||
/// 执行SQL语句,返回影响的记录数
|
||||
/// </summary>
|
||||
/// <param name="SQLString">SQL语句</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteSql(string SQLString)
|
||||
{
|
||||
using (OracleConnection connection = new OracleConnection(connectionString))
|
||||
{
|
||||
using (OracleCommand cmd = new OracleCommand(SQLString, connection))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
return rows;
|
||||
}
|
||||
catch (Oracle.ManagedDataAccess.Client.OracleException E)
|
||||
{
|
||||
connection.Close();
|
||||
throw new Exception(E.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行多条SQL语句,实现数据库事务。
|
||||
/// </summary>
|
||||
/// <param name="SQLStringList">多条SQL语句</param>
|
||||
public static void ExecuteSqlTran(ArrayList SQLStringList)
|
||||
{
|
||||
using (OracleConnection conn = new OracleConnection(connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
OracleCommand cmd = new OracleCommand();
|
||||
cmd.Connection = conn;
|
||||
OracleTransaction tx = conn.BeginTransaction();
|
||||
cmd.Transaction = tx;
|
||||
try
|
||||
{
|
||||
for (int n = 0; n < SQLStringList.Count; n++)
|
||||
{
|
||||
string strsql = SQLStringList[n].ToString();
|
||||
if (strsql.Trim().Length > 1)
|
||||
{
|
||||
cmd.CommandText = strsql;
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
tx.Commit();
|
||||
}
|
||||
catch (Oracle.ManagedDataAccess.Client.OracleException E)
|
||||
{
|
||||
tx.Rollback();
|
||||
throw new Exception(E.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行带一个存储过程参数的的SQL语句。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">SQL语句</param>
|
||||
/// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteSql(string SQLString, string content)
|
||||
{
|
||||
using (OracleConnection connection = new OracleConnection(connectionString))
|
||||
{
|
||||
OracleCommand cmd = new OracleCommand(SQLString, connection);
|
||||
Oracle.ManagedDataAccess.Client.OracleParameter myParameter = new Oracle.ManagedDataAccess.Client.OracleParameter("@content", new Object());
|
||||
myParameter.Value = content;
|
||||
cmd.Parameters.Add(myParameter);
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
return rows;
|
||||
}
|
||||
catch (Oracle.ManagedDataAccess.Client.OracleException E)
|
||||
{
|
||||
throw new Exception(E.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
|
||||
/// </summary>
|
||||
/// <param name="strSQL">SQL语句</param>
|
||||
/// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
|
||||
{
|
||||
using (OracleConnection connection = new OracleConnection(connectionString))
|
||||
{
|
||||
OracleCommand cmd = new OracleCommand(strSQL, connection);
|
||||
Oracle.ManagedDataAccess.Client.OracleParameter myParameter = new Oracle.ManagedDataAccess.Client.OracleParameter("@fs", new Object());
|
||||
myParameter.Value = fs;
|
||||
cmd.Parameters.Add(myParameter);
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
return rows;
|
||||
}
|
||||
catch (Oracle.ManagedDataAccess.Client.OracleException E)
|
||||
{
|
||||
throw new Exception(E.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行一条计算查询结果语句,返回查询结果(object)。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">计算查询结果语句</param>
|
||||
/// <returns>查询结果(object)</returns>
|
||||
public static object GetSingle(string SQLString)
|
||||
{
|
||||
using (OracleConnection connection = new OracleConnection(connectionString))
|
||||
{
|
||||
using (OracleCommand cmd = new OracleCommand(SQLString, connection))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
object obj = cmd.ExecuteScalar();
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
catch (Oracle.ManagedDataAccess.Client.OracleException e)
|
||||
{
|
||||
connection.Close();
|
||||
throw new Exception(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回OracleDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
|
||||
/// </summary>
|
||||
/// <param name="strSQL">查询语句</param>
|
||||
/// <returns>OracleDataReader</returns>
|
||||
public static OracleDataReader ExecuteReader(string strSQL)
|
||||
{
|
||||
OracleConnection connection = new OracleConnection(connectionString);
|
||||
OracleCommand cmd = new OracleCommand(strSQL, connection);
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
OracleDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
return myReader;
|
||||
}
|
||||
catch (Oracle.ManagedDataAccess.Client.OracleException e)
|
||||
{
|
||||
throw new Exception(e.Message);
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回DataSet
|
||||
/// </summary>
|
||||
/// <param name="SQLString">查询语句</param>
|
||||
/// <returns>DataSet</returns>
|
||||
public static DataSet Query(string SQLString)
|
||||
{
|
||||
using (OracleConnection connection = new OracleConnection(connectionString))
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
OracleDataAdapter command = new OracleDataAdapter(SQLString, connection);
|
||||
command.Fill(ds, "ds");
|
||||
}
|
||||
catch (Oracle.ManagedDataAccess.Client.OracleException ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region 执行带参数的SQL语句
|
||||
|
||||
/// <summary>
|
||||
/// 执行SQL语句,返回影响的记录数
|
||||
/// </summary>
|
||||
/// <param name="SQLString">SQL语句</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteSql(string SQLString, params OracleParameter[] cmdParms)
|
||||
{
|
||||
using (OracleConnection connection = new OracleConnection(connectionString))
|
||||
{
|
||||
using (OracleCommand cmd = new OracleCommand())
|
||||
{
|
||||
try
|
||||
{
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
cmd.Parameters.Clear();
|
||||
return rows;
|
||||
}
|
||||
catch (Oracle.ManagedDataAccess.Client.OracleException E)
|
||||
{
|
||||
throw new Exception(E.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 执行多条SQL语句,实现数据库事务。
|
||||
/// </summary>
|
||||
/// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的OracleParameter[])</param>
|
||||
public static void ExecuteSqlTran(Hashtable SQLStringList)
|
||||
{
|
||||
using (OracleConnection conn = new OracleConnection(connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
using (OracleTransaction trans = conn.BeginTransaction())
|
||||
{
|
||||
OracleCommand cmd = new OracleCommand();
|
||||
try
|
||||
{
|
||||
//循环
|
||||
foreach (DictionaryEntry myDE in SQLStringList)
|
||||
{
|
||||
string cmdText = myDE.Key.ToString();
|
||||
OracleParameter[] cmdParms = (OracleParameter[])myDE.Value;
|
||||
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
|
||||
int val = cmd.ExecuteNonQuery();
|
||||
cmd.Parameters.Clear();
|
||||
|
||||
trans.Commit();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
trans.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 执行一条计算查询结果语句,返回查询结果(object)。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">计算查询结果语句</param>
|
||||
/// <returns>查询结果(object)</returns>
|
||||
public static object GetSingle(string SQLString, params OracleParameter[] cmdParms)
|
||||
{
|
||||
using (OracleConnection connection = new OracleConnection(connectionString))
|
||||
{
|
||||
using (OracleCommand cmd = new OracleCommand())
|
||||
{
|
||||
try
|
||||
{
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
object obj = cmd.ExecuteScalar();
|
||||
cmd.Parameters.Clear();
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
catch (Oracle.ManagedDataAccess.Client.OracleException e)
|
||||
{
|
||||
throw new Exception(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回OracleDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
|
||||
/// </summary>
|
||||
/// <param name="strSQL">查询语句</param>
|
||||
/// <returns>OracleDataReader</returns>
|
||||
public static OracleDataReader ExecuteReader(string SQLString, params OracleParameter[] cmdParms)
|
||||
{
|
||||
OracleConnection connection = new OracleConnection(connectionString);
|
||||
OracleCommand cmd = new OracleCommand();
|
||||
try
|
||||
{
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
OracleDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
cmd.Parameters.Clear();
|
||||
return myReader;
|
||||
}
|
||||
catch (Oracle.ManagedDataAccess.Client.OracleException e)
|
||||
{
|
||||
throw new Exception(e.Message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回DataSet
|
||||
/// </summary>
|
||||
/// <param name="SQLString">查询语句</param>
|
||||
/// <returns>DataSet</returns>
|
||||
public static DataSet Query(string SQLString, params OracleParameter[] cmdParms)
|
||||
{
|
||||
using (OracleConnection connection = new OracleConnection(connectionString))
|
||||
{
|
||||
OracleCommand cmd = new OracleCommand();
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
using (OracleDataAdapter da = new OracleDataAdapter(cmd))
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
da.Fill(ds, "ds");
|
||||
cmd.Parameters.Clear();
|
||||
}
|
||||
catch (Oracle.ManagedDataAccess.Client.OracleException ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, string cmdText, OracleParameter[] cmdParms)
|
||||
{
|
||||
if (conn.State != ConnectionState.Open)
|
||||
conn.Open();
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = cmdText;
|
||||
if (trans != null)
|
||||
cmd.Transaction = trans;
|
||||
cmd.CommandType = CommandType.Text;//cmdType;
|
||||
if (cmdParms != null)
|
||||
{
|
||||
foreach (OracleParameter parm in cmdParms)
|
||||
cmd.Parameters.Add(parm);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 存储过程操作
|
||||
|
||||
/// <summary>
|
||||
/// 执行存储过程 返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
|
||||
/// </summary>
|
||||
/// <param name="storedProcName">存储过程名</param>
|
||||
/// <param name="parameters">存储过程参数</param>
|
||||
/// <returns>OracleDataReader</returns>
|
||||
public static OracleDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
|
||||
{
|
||||
using (OracleConnection connection = new OracleConnection(connectionString))
|
||||
{
|
||||
OracleDataReader returnReader;
|
||||
connection.Open();
|
||||
OracleCommand command = BuildQueryCommand(connection, storedProcName, parameters);
|
||||
command.CommandType = CommandType.StoredProcedure;
|
||||
returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
return returnReader;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 执行存储过程
|
||||
/// </summary>
|
||||
/// <param name="storedProcName">存储过程名</param>
|
||||
/// <param name="parameters">存储过程参数</param>
|
||||
/// <param name="tableName">DataSet结果中的表名</param>
|
||||
/// <returns>DataSet</returns>
|
||||
public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
|
||||
{
|
||||
using (OracleConnection connection = new OracleConnection(connectionString))
|
||||
{
|
||||
DataSet dataSet = new DataSet();
|
||||
connection.Open();
|
||||
OracleDataAdapter sqlDA = new OracleDataAdapter();
|
||||
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
|
||||
sqlDA.Fill(dataSet, tableName);
|
||||
connection.Close();
|
||||
return dataSet;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行带参数的存储过程
|
||||
/// @auther SYL
|
||||
/// </summary>
|
||||
/// <param name="pro_name">存储过程名称</param>
|
||||
/// <param name="par_name">参数名称</param>
|
||||
public static void NewRunProcedure(string pro_name, string par_name)
|
||||
{
|
||||
using (OracleConnection connection = new OracleConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
OracleCommand dr = new OracleCommand(pro_name, connection); //连接存储过程
|
||||
dr.CommandType = CommandType.StoredProcedure; //定义连接的是存储过程
|
||||
OracleParameter op = dr.Parameters.Add("sql_str", OracleDbType.NVarchar2, 2000); //定义参数
|
||||
op.Direction = ParameterDirection.Input;
|
||||
op.Value = par_name;
|
||||
dr.ExecuteNonQuery();
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构建 OracleCommand 对象(用来返回一个结果集,而不是一个整数值)
|
||||
/// </summary>
|
||||
/// <param name="connection">数据库连接</param>
|
||||
/// <param name="storedProcName">存储过程名</param>
|
||||
/// <param name="parameters">存储过程参数</param>
|
||||
/// <returns>OracleCommand</returns>
|
||||
private static OracleCommand BuildQueryCommand(OracleConnection connection, string storedProcName, IDataParameter[] parameters)
|
||||
{
|
||||
OracleCommand command = new OracleCommand(storedProcName, connection);
|
||||
command.CommandType = CommandType.StoredProcedure;
|
||||
foreach (OracleParameter parameter in parameters)
|
||||
{
|
||||
command.Parameters.Add(parameter);
|
||||
}
|
||||
return command;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 执行存储过程,返回影响的行数
|
||||
/// </summary>
|
||||
/// <param name="storedProcName">存储过程名</param>
|
||||
/// <param name="parameters">存储过程参数</param>
|
||||
/// <param name="rowsAffected">影响的行数</param>
|
||||
/// <returns></returns>
|
||||
public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
|
||||
{
|
||||
using (OracleConnection connection = new OracleConnection(connectionString))
|
||||
{
|
||||
int result;
|
||||
connection.Open();
|
||||
OracleCommand command = BuildIntCommand(connection, storedProcName, parameters);
|
||||
rowsAffected = command.ExecuteNonQuery();
|
||||
result = (int)command.Parameters["ReturnValue"].Value;
|
||||
//Connection.Close();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建 OracleCommand 对象实例(用来返回一个整数值)
|
||||
/// </summary>
|
||||
/// <param name="storedProcName">存储过程名</param>
|
||||
/// <param name="parameters">存储过程参数</param>
|
||||
/// <returns>OracleCommand 对象实例</returns>
|
||||
private static OracleCommand BuildIntCommand(OracleConnection connection, string storedProcName, IDataParameter[] parameters)
|
||||
{
|
||||
OracleCommand command = BuildQueryCommand(connection, storedProcName, parameters);
|
||||
command.Parameters.Add(new OracleParameter("ReturnValue",
|
||||
OracleDbType.Int32, 4, ParameterDirection.ReturnValue,
|
||||
false, 0, 0, string.Empty, DataRowVersion.Default, null));
|
||||
return command;
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
857
LeadDataBaseUtility/LeadDbHelperSQLServer.cs
Normal file
857
LeadDataBaseUtility/LeadDbHelperSQLServer.cs
Normal file
@@ -0,0 +1,857 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LeadDataBaseUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// Copyright20181102 (C) sunyalong
|
||||
/// 数据访问抽象基础类(基于SQLServer)
|
||||
/// 允许修改、添加满足自己项目的需要。
|
||||
/// 添加、修改后请详细注释。违者会强制删除不予采用。
|
||||
/// </summary>
|
||||
public abstract class LeadDbHelperSQLServer
|
||||
{
|
||||
public static string connectionString = LeadConfigureConnectionString.getConnectionString("LeadSqlServer");
|
||||
|
||||
#region 公用方法
|
||||
/// <summary>
|
||||
/// 判断是否存在某表的某个字段
|
||||
/// </summary>
|
||||
/// <param name="tableName">表名称</param>
|
||||
/// <param name="columnName">列名称</param>
|
||||
/// <returns>是否存在</returns>
|
||||
public static bool ColumnExists(string tableName, string columnName)
|
||||
{
|
||||
string sql = "select count(1) from syscolumns where [id]=object_id('" + tableName + "') and [name]='" + columnName + "'";
|
||||
object res = GetSingle(sql);
|
||||
if (res == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Convert.ToInt32(res) > 0;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获得最大ID
|
||||
/// </summary>
|
||||
/// <param name="FieldName"></param>
|
||||
/// <param name="TableName"></param>
|
||||
/// <returns></returns>
|
||||
public static int GetMaxID(string FieldName, string TableName)
|
||||
{
|
||||
try
|
||||
{
|
||||
string strsql = "select max(" + FieldName + ")+1 from " + TableName;
|
||||
object obj = GetSingle(strsql);
|
||||
if (obj == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return int.Parse(obj.ToString());
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 判断表中数据是否存在
|
||||
/// </summary>
|
||||
/// <param name="tableName">表名</param>
|
||||
/// <param name="columnName">列名</param>
|
||||
/// <param name="value">数值</param>
|
||||
/// <returns>true表示存在 flase表示不存在</returns>
|
||||
public static bool ValueExists(string tableName, string columnName, string value)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql = "select count(1) from " + tableName + " where " + columnName + " = '" + value + "'";
|
||||
object c = GetSingle(sql);
|
||||
if (c != null && Convert.ToInt32(c) > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Exists(string strSql)
|
||||
{
|
||||
object obj = GetSingle(strSql);
|
||||
int cmdresult;
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
cmdresult = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmdresult = int.Parse(obj.ToString()); //也可能=0
|
||||
}
|
||||
if (cmdresult == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 表是否存在
|
||||
/// </summary>
|
||||
/// <param name="TableName"></param>
|
||||
/// <returns></returns>
|
||||
public static bool TabExists(string TableName)
|
||||
{
|
||||
string strsql = "select count(*) from sysobjects where id = object_id(N'[" + TableName + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1";
|
||||
//string strsql = "SELECT count(*) FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" + TableName + "]') AND type in (N'U')";
|
||||
object obj = GetSingle(strsql);
|
||||
int cmdresult;
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
cmdresult = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmdresult = int.Parse(obj.ToString());
|
||||
}
|
||||
if (cmdresult == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public static bool Exists(string strSql, params SqlParameter[] cmdParms)
|
||||
{
|
||||
object obj = GetSingle(strSql, cmdParms);
|
||||
int cmdresult;
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
cmdresult = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmdresult = int.Parse(obj.ToString());
|
||||
}
|
||||
if (cmdresult == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 执行简单SQL语句
|
||||
|
||||
/// <summary>
|
||||
/// 执行SQL语句,返回影响的记录数
|
||||
/// </summary>
|
||||
/// <param name="SQLString">SQL语句</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteSql(string SQLString)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
return rows;
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException e)
|
||||
{
|
||||
connection.Close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static int ExecuteSqlByTime(string SQLString, int Times)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
cmd.CommandTimeout = Times;
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
return rows;
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException e)
|
||||
{
|
||||
connection.Close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行多条SQL语句,实现数据库事务。
|
||||
/// </summary>
|
||||
/// <param name="SQLStringList">多条SQL语句</param>
|
||||
public static int ExecuteSqlTran(List<String> SQLStringList)
|
||||
{
|
||||
using (SqlConnection conn = new SqlConnection(connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
SqlCommand cmd = new SqlCommand();
|
||||
cmd.Connection = conn;
|
||||
SqlTransaction tx = conn.BeginTransaction();
|
||||
cmd.Transaction = tx;
|
||||
try
|
||||
{
|
||||
int count = 0;
|
||||
for (int n = 0; n < SQLStringList.Count; n++)
|
||||
{
|
||||
string strsql = SQLStringList[n];
|
||||
if (strsql.Trim().Length > 1)
|
||||
{
|
||||
cmd.CommandText = strsql;
|
||||
count += cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
tx.Commit();
|
||||
return count;
|
||||
}
|
||||
catch (Exception )
|
||||
{
|
||||
tx.Rollback();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行带一个存储过程参数的的SQL语句。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">SQL语句</param>
|
||||
/// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteSql(string SQLString, string content)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
SqlCommand cmd = new SqlCommand(SQLString, connection);
|
||||
System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);
|
||||
myParameter.Value = content;
|
||||
cmd.Parameters.Add(myParameter);
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
return rows;
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行带一个存储过程参数的的SQL语句。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">SQL语句</param>
|
||||
/// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static object ExecuteSqlGet(string SQLString, string content)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
SqlCommand cmd = new SqlCommand(SQLString, connection);
|
||||
System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);
|
||||
myParameter.Value = content;
|
||||
cmd.Parameters.Add(myParameter);
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
object obj = cmd.ExecuteScalar();
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
|
||||
/// </summary>
|
||||
/// <param name="strSQL">SQL语句</param>
|
||||
/// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
SqlCommand cmd = new SqlCommand(strSQL, connection);
|
||||
System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@fs", SqlDbType.Image);
|
||||
myParameter.Value = fs;
|
||||
cmd.Parameters.Add(myParameter);
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
return rows;
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行一条计算查询结果语句,返回查询结果(object)。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">计算查询结果语句</param>
|
||||
/// <returns>查询结果(object)</returns>
|
||||
public static object GetSingle(string SQLString)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
object obj = cmd.ExecuteScalar();
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException e)
|
||||
{
|
||||
connection.Close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public static object GetSingle(string SQLString, int Times)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
cmd.CommandTimeout = Times;
|
||||
object obj = cmd.ExecuteScalar();
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException e)
|
||||
{
|
||||
connection.Close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
|
||||
/// </summary>
|
||||
/// <param name="strSQL">查询语句</param>
|
||||
/// <returns>SqlDataReader</returns>
|
||||
public static SqlDataReader ExecuteReader(string strSQL)
|
||||
{
|
||||
SqlConnection connection = new SqlConnection(connectionString);
|
||||
SqlCommand cmd = new SqlCommand(strSQL, connection);
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
return myReader;
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回DataSet
|
||||
/// </summary>
|
||||
/// <param name="SQLString">查询语句</param>
|
||||
/// <returns>DataSet</returns>
|
||||
public static DataSet Query(string SQLString)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
|
||||
command.Fill(ds, "ds");
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
public static DataSet Query(string SQLString, int Times)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
|
||||
command.SelectCommand.CommandTimeout = Times;
|
||||
command.Fill(ds, "ds");
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region 执行带参数的SQL语句
|
||||
|
||||
/// <summary>
|
||||
/// 执行SQL语句,返回影响的记录数
|
||||
/// </summary>
|
||||
/// <param name="SQLString">SQL语句</param>
|
||||
/// <returns>影响的记录数</returns>
|
||||
public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
using (SqlCommand cmd = new SqlCommand())
|
||||
{
|
||||
try
|
||||
{
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
cmd.Parameters.Clear();
|
||||
return rows;
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 执行多条SQL语句,实现数据库事务。
|
||||
/// </summary>
|
||||
/// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
|
||||
public static void ExecuteSqlTran(Hashtable SQLStringList)
|
||||
{
|
||||
using (SqlConnection conn = new SqlConnection(connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
using (SqlTransaction trans = conn.BeginTransaction())
|
||||
{
|
||||
SqlCommand cmd = new SqlCommand();
|
||||
try
|
||||
{
|
||||
//循环
|
||||
foreach (DictionaryEntry myDE in SQLStringList)
|
||||
{
|
||||
string cmdText = myDE.Key.ToString();
|
||||
SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
|
||||
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
|
||||
int val = cmd.ExecuteNonQuery();
|
||||
cmd.Parameters.Clear();
|
||||
}
|
||||
trans.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
trans.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行多条SQL语句,实现数据库事务。
|
||||
/// </summary>
|
||||
/// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
|
||||
public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList)
|
||||
{
|
||||
using (SqlConnection conn = new SqlConnection(connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
using (SqlTransaction trans = conn.BeginTransaction())
|
||||
{
|
||||
SqlCommand cmd = new SqlCommand();
|
||||
try
|
||||
{
|
||||
int indentity = 0;
|
||||
//循环
|
||||
foreach (DictionaryEntry myDE in SQLStringList)
|
||||
{
|
||||
string cmdText = myDE.Key.ToString();
|
||||
SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
|
||||
foreach (SqlParameter q in cmdParms)
|
||||
{
|
||||
if (q.Direction == ParameterDirection.InputOutput)
|
||||
{
|
||||
q.Value = indentity;
|
||||
}
|
||||
}
|
||||
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
|
||||
int val = cmd.ExecuteNonQuery();
|
||||
foreach (SqlParameter q in cmdParms)
|
||||
{
|
||||
if (q.Direction == ParameterDirection.Output)
|
||||
{
|
||||
indentity = Convert.ToInt32(q.Value);
|
||||
}
|
||||
}
|
||||
cmd.Parameters.Clear();
|
||||
}
|
||||
trans.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
trans.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行一条计算查询结果语句,返回查询结果(object)。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">计算查询结果语句</param>
|
||||
/// <returns>查询结果(object)</returns>
|
||||
public static object GetSingle(string SQLString, params SqlParameter[] cmdParms)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
using (SqlCommand cmd = new SqlCommand())
|
||||
{
|
||||
try
|
||||
{
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
object obj = cmd.ExecuteScalar();
|
||||
cmd.Parameters.Clear();
|
||||
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
|
||||
/// </summary>
|
||||
/// <param name="strSQL">查询语句</param>
|
||||
/// <returns>SqlDataReader</returns>
|
||||
public static SqlDataReader ExecuteReader(string SQLString, params SqlParameter[] cmdParms)
|
||||
{
|
||||
SqlConnection connection = new SqlConnection(connectionString);
|
||||
SqlCommand cmd = new SqlCommand();
|
||||
try
|
||||
{
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
cmd.Parameters.Clear();
|
||||
return myReader;
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
// finally
|
||||
// {
|
||||
// cmd.Dispose();
|
||||
// connection.Close();
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回DataSet
|
||||
/// </summary>
|
||||
/// <param name="SQLString">查询语句</param>
|
||||
/// <returns>DataSet</returns>
|
||||
public static DataSet Query(string SQLString, params SqlParameter[] cmdParms)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
SqlCommand cmd = new SqlCommand();
|
||||
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
|
||||
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
da.Fill(ds, "ds");
|
||||
cmd.Parameters.Clear();
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
|
||||
{
|
||||
if (conn.State != ConnectionState.Open)
|
||||
conn.Open();
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = cmdText;
|
||||
if (trans != null)
|
||||
cmd.Transaction = trans;
|
||||
cmd.CommandType = CommandType.Text;//cmdType;
|
||||
if (cmdParms != null)
|
||||
{
|
||||
|
||||
|
||||
foreach (SqlParameter parameter in cmdParms)
|
||||
{
|
||||
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
|
||||
(parameter.Value == null))
|
||||
{
|
||||
parameter.Value = DBNull.Value;
|
||||
}
|
||||
cmd.Parameters.Add(parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 存储过程操作
|
||||
|
||||
/// <summary>
|
||||
/// 执行存储过程,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
|
||||
/// </summary>
|
||||
/// <param name="storedProcName">存储过程名</param>
|
||||
/// <param name="parameters">存储过程参数</param>
|
||||
/// <returns>SqlDataReader</returns>
|
||||
public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
SqlDataReader returnReader;
|
||||
connection.Open();
|
||||
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
|
||||
command.CommandType = CommandType.StoredProcedure;
|
||||
returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
return returnReader;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 执行存储过程
|
||||
/// </summary>
|
||||
/// <param name="storedProcName">存储过程名</param>
|
||||
/// <param name="parameters">存储过程参数</param>
|
||||
/// <param name="tableName">DataSet结果中的表名</param>
|
||||
/// <returns>DataSet</returns>
|
||||
public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
DataSet dataSet = new DataSet();
|
||||
connection.Open();
|
||||
SqlDataAdapter sqlDA = new SqlDataAdapter();
|
||||
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
|
||||
sqlDA.Fill(dataSet, tableName);
|
||||
connection.Close();
|
||||
return dataSet;
|
||||
}
|
||||
}
|
||||
public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
DataSet dataSet = new DataSet();
|
||||
connection.Open();
|
||||
SqlDataAdapter sqlDA = new SqlDataAdapter();
|
||||
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
|
||||
sqlDA.SelectCommand.CommandTimeout = Times;
|
||||
sqlDA.Fill(dataSet, tableName);
|
||||
connection.Close();
|
||||
return dataSet;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
|
||||
/// </summary>
|
||||
/// <param name="connection">数据库连接</param>
|
||||
/// <param name="storedProcName">存储过程名</param>
|
||||
/// <param name="parameters">存储过程参数</param>
|
||||
/// <returns>SqlCommand</returns>
|
||||
private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
|
||||
{
|
||||
SqlCommand command = new SqlCommand(storedProcName, connection);
|
||||
command.CommandType = CommandType.StoredProcedure;
|
||||
foreach (SqlParameter parameter in parameters)
|
||||
{
|
||||
if (parameter != null)
|
||||
{
|
||||
// 检查未分配值的输出参数,将其分配以DBNull.Value.
|
||||
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
|
||||
(parameter.Value == null))
|
||||
{
|
||||
parameter.Value = DBNull.Value;
|
||||
}
|
||||
command.Parameters.Add(parameter);
|
||||
}
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行存储过程,返回影响的行数
|
||||
/// </summary>
|
||||
/// <param name="storedProcName">存储过程名</param>
|
||||
/// <param name="parameters">存储过程参数</param>
|
||||
/// <param name="rowsAffected">影响的行数</param>
|
||||
/// <returns></returns>
|
||||
public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
int result;
|
||||
connection.Open();
|
||||
SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
|
||||
rowsAffected = command.ExecuteNonQuery();
|
||||
result = (int)command.Parameters["ReturnValue"].Value;
|
||||
//Connection.Close();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建 SqlCommand 对象实例(用来返回一个整数值)
|
||||
/// </summary>
|
||||
/// <param name="storedProcName">存储过程名</param>
|
||||
/// <param name="parameters">存储过程参数</param>
|
||||
/// <returns>SqlCommand 对象实例</returns>
|
||||
private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
|
||||
{
|
||||
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
|
||||
command.Parameters.Add(new SqlParameter("ReturnValue",
|
||||
SqlDbType.Int, 4, ParameterDirection.ReturnValue,
|
||||
false, 0, 0, string.Empty, DataRowVersion.Default, null));
|
||||
return command;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 自定义方法
|
||||
/// <summary>
|
||||
/// 数据库是否连接成功
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static bool ConnStatus()
|
||||
{
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
return true;
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException e)
|
||||
{
|
||||
connection.Close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
112
LeadDataBaseUtility/LeadDbHelperSQLite.cs
Normal file
112
LeadDataBaseUtility/LeadDbHelperSQLite.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using NX_LogUtility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LeadDataBaseUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// Copyright20181102 (C) sunyalong
|
||||
/// 数据访问抽象基础类(基于SQLite)
|
||||
/// 允许修改、添加满足自己项目的需要。
|
||||
/// 添加、修改后请详细注释。违者会强制删除不予采用。
|
||||
/// </summary>
|
||||
public abstract class LeadDbHelperSQLite
|
||||
{
|
||||
public static string connectionString = LeadConfigureConnectionString.getConnectionString("LeadSQLite");
|
||||
|
||||
/// <summary>
|
||||
/// 对SQLite数据库执行增删改操作,返回受影响的行数。
|
||||
/// </summary>
|
||||
/// <param name="sql">要执行的增删改的SQL语句</param>
|
||||
/// <returns></returns>
|
||||
public static int ExecuteSql(string sql)
|
||||
{
|
||||
int affectedRows = 0;
|
||||
|
||||
try
|
||||
{
|
||||
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
|
||||
{
|
||||
conn.Open();
|
||||
using (SQLiteCommand cmd = new SQLiteCommand(conn))
|
||||
{
|
||||
cmd.CommandText = sql;
|
||||
affectedRows = cmd.ExecuteNonQuery();
|
||||
}
|
||||
conn.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LogHelper.WriteLog($"数据库运行异常:【{e.Message}】。SQL语句:【{sql}】。", LogTypeEum.DbErr);
|
||||
}
|
||||
|
||||
return affectedRows;
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行查询语句,返回DataSet
|
||||
/// </summary>
|
||||
/// <param name="SQLString">查询语句</param>
|
||||
/// <returns>DataSet</returns>
|
||||
public static DataSet Query(string SQLString)
|
||||
{
|
||||
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
|
||||
command.Fill(ds, "ds");
|
||||
connection.Close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LogHelper.WriteLog($"数据库运行异常:【{e.Message}】。SQL语句:【{SQLString}】。", LogTypeEum.DbErr);
|
||||
}
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 执行一条计算查询结果语句,返回查询结果(object)。
|
||||
/// </summary>
|
||||
/// <param name="SQLString">计算查询结果语句</param>
|
||||
/// <returns>查询结果(object)</returns>
|
||||
public static object GetSingle(string SQLString)
|
||||
{
|
||||
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
|
||||
{
|
||||
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
object obj = cmd.ExecuteScalar();
|
||||
if ((Equals(obj, null)) || (Equals(obj, System.DBNull.Value)))
|
||||
{
|
||||
connection.Close();
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
connection.Close();
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
catch (System.Data.SqlClient.SqlException e)
|
||||
{
|
||||
connection.Close();
|
||||
LogHelper.WriteLog($"数据库运行异常:【{e.Message}】。SQL语句:【{SQLString}】。", LogTypeEum.DbErr);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
36
LeadDataBaseUtility/Properties/AssemblyInfo.cs
Normal file
36
LeadDataBaseUtility/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的一般信息由以下
|
||||
// 控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("LeadDataBaseUtility")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Microsoft")]
|
||||
[assembly: AssemblyProduct("LeadDataBaseUtility")]
|
||||
[assembly: AssemblyCopyright("Copyright © Microsoft 2018")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
//将 ComVisible 设置为 false 将使此程序集中的类型
|
||||
//对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
|
||||
//请将此类型的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("824348a6-ffff-4c1b-bbf5-8b507a8c8ce5")]
|
||||
|
||||
// 程序集的版本信息由下列四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
|
||||
// 方法是按如下所示使用“*”: :
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
Reference in New Issue
Block a user