数据访问公共类

using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Text;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace MDCRM.DAL
{
    /// <summary>
    /// 数据访问基类
    /// </summary>
    public abstract class BaseProvider
    {

        protected string ConnectionString { get; set; }

        protected BaseProvider()
        {
            this.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        }

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="connectionStringSectionName">链接字符串节点对应名称</param>
        protected BaseProvider(string connectionStringSectionName)
        {
            this.ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringSectionName].ConnectionString;
        }

        /// <summary>
        /// 执行 ExecuteNonQuery
        /// </summary>
        protected int ExecuteNonQuery(DbCommand cmd)
        {
            foreach (DbParameter param in cmd.Parameters)
            {
                if (param.Direction == ParameterDirection.Output || param.Direction == ParameterDirection.ReturnValue)
                {
                    switch (param.DbType)
                    {
                        case DbType.AnsiString:
                        case DbType.AnsiStringFixedLength:
                        case DbType.String:
                        case DbType.StringFixedLength:
                        case DbType.Xml:
                            param.Value = string.Empty;
                            break;
                        case DbType.Boolean:
                            param.Value = false;
                            break;
                        case DbType.Byte:
                            param.Value = byte.MinValue;
                            break;
                        case DbType.Date:
                        case DbType.DateTime:
                            param.Value = DateTime.MinValue;
                            break;
                        case DbType.Currency:
                        case DbType.Decimal:
                            param.Value = decimal.MinValue;
                            break;
                        case DbType.Guid:
                            param.Value = Guid.Empty;
                            break;
                        case DbType.Double:
                        case DbType.Int16:
                        case DbType.Int32:
                        case DbType.Int64:
                            param.Value = 0;
                            break;
                        default:
                            param.Value = null;
                            break;
                    }
                }
            }

            FilterSqlParameter(cmd);

            return cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 执行 ExecuteReader
        /// </summary>
        protected IDataReader ExecuteReader(DbCommand cmd)
        {
            FilterSqlParameter(cmd);

            return ExecuteReader(cmd, CommandBehavior.Default);
        }

        /// <summary>
        /// 执行 ExecuteReader
        /// </summary>
        protected IDataReader ExecuteReader(DbCommand cmd, CommandBehavior behavior)
        {
            FilterSqlParameter(cmd);

            return cmd.ExecuteReader(behavior);
        }

        /// <summary>
        /// 执行查询,并返回查询所返回的结果集中第一行的第一列。所有其他的列和行将被忽略。
        /// </summary>
        protected object ExecuteScalar(DbCommand cmd)
        {
            FilterSqlParameter(cmd);

            return cmd.ExecuteScalar();
        }

        /// <summary>
        /// 执行查询,返回数据结果集
        /// </summary>
        protected void FillDataSet(System.Data.DataSet dataSet, DbCommand cmd, string tableNames)
        {
            FillDataSet(dataSet, cmd, tableNames.Split(‘|‘));
        }

        /// <summary>
        /// 执行查询,返回数据结果集
        /// </summary>
        protected void FillDataSet(System.Data.DataSet dataSet, DbCommand cmd, string[] tableNames)
        {
            System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter((System.Data.SqlClient.SqlCommand)cmd);
            cmd.CommandTimeout = 10000;

            string mapTableName = "Table";
            for (int index = 0; index < tableNames.Length; index++)
            {
                if (tableNames[index] != null && tableNames[index].Length >= 0)
                {
                    dataAdapter.TableMappings.Add(mapTableName, tableNames[index]);
                    mapTableName = "Table" + (index + 1).ToString();
                }
            }

            FilterSqlParameter(cmd);

           dataAdapter.Fill(dataSet);
        }

        /// <summary>
        /// 集合转成搜索条件
        /// </summary>
        protected string ListToString<T>(List<T> list, char departChar)
        {
            string listStr = string.Empty;
            if (list != null && list.Count > 0)
            {
                foreach (T tType in list)
                {
                    if (!string.IsNullOrEmpty(tType.ToString().Trim()))
                        listStr += tType.ToString().Trim() + departChar.ToString();
                }
            }
            return listStr.Trim(departChar);
        }

        /// <summary>
        /// 集合转成搜索条件
        /// </summary>
        protected string ListToString<T>(List<T> list, string departStr)
        {
            string listStr = string.Empty;
            if (list != null && list.Count > 0)
            {
                foreach (T tType in list)
                {
                    if (!string.IsNullOrEmpty(tType.ToString().Trim()))
                        listStr += tType.ToString().Trim() + departStr.ToString();
                }
            }

            if (!string.IsNullOrEmpty(listStr))
                listStr = listStr.Remove(listStr.Length - departStr.Length);

            return listStr;
        }

        /// <summary>
        /// 数组转成搜索条件
        /// </summary>
        protected string ArrayToString<T>(T[] arr, char departChar)
        {
            string listStr = string.Empty;
            if (arr != null && arr.Length > 0)
            {
                foreach (T tType in arr)
                {
                    if (!string.IsNullOrEmpty(tType.ToString().Trim()))
                        listStr += tType.ToString().Trim() + departChar.ToString();
                }
            }
            return listStr.Trim(departChar);
        }

        /// <summary>
        /// 过滤注入式特殊符号
        /// </summary>
        protected SqlParameter[] FilterSqlParameterArray(params SqlParameter[] parameterValues)
        {
            int paraCount = 0;
            paraCount = parameterValues.Length;
            SqlParameter[] paraArray = new SqlParameter[paraCount];
            for (int i = 0; i < paraCount; i++)
            {
                SqlParameter parameterValue = parameterValues[i];
                if (parameterValue.DbType == DbType.AnsiString || parameterValue.DbType == DbType.String || parameterValue.SqlDbType == SqlDbType.VarChar || parameterValue.SqlDbType == SqlDbType.NVarChar || parameterValue.SqlDbType == SqlDbType.Text)
                {
                    if (parameterValue.Value != null && parameterValue.Value.ToString() != "")
                        parameterValue.Value = parameterValue.Value.ToString().Replace("‘", "‘‘").Replace("--", "");
                }
                paraArray[i] = parameterValue;
            }
            return paraArray;
        }

        /// <summary>
        /// 过滤注入式特殊符号
        /// </summary>
        protected void FilterSqlParameter(DbCommand cmd)
        {
            int paraCount = 0;
            paraCount = cmd.Parameters.Count;
            SqlParameter[] paraArray = new SqlParameter[paraCount];
            for (int i = 0; i < paraCount; i++)
            {
                SqlParameter parameterValue = (SqlParameter)cmd.Parameters[i];
                if (parameterValue.DbType == DbType.AnsiString || parameterValue.DbType == DbType.String || parameterValue.SqlDbType == SqlDbType.VarChar || parameterValue.SqlDbType == SqlDbType.NVarChar || parameterValue.SqlDbType == SqlDbType.Text)
                {
                    if (parameterValue.Value != null && parameterValue.Value.ToString() != "")
                        parameterValue.Value = parameterValue.Value.ToString().Replace("‘", "‘‘").Replace("--", "");
                }
                paraArray[i] = parameterValue;
            }

            cmd.Parameters.Clear();
            cmd.Parameters.AddRange(paraArray);
        }
        /// <summary>
        /// 将传入的参数生成为相应的sql语句
        /// </summary>
        /// <param name="param">SqlParameter[]</param>
        /// <param name="split">split</param>
        /// <returns>string</returns>
        protected string SqlParameterToSql(SqlParameter[] param, string split)
        {
            string sql = string.Empty;
            if (param == null) return sql;
            for (int i = 0; i < param.Length; i++)
            {
                SqlParameter key = param[i];
                if (i > 0)
                {
                    sql += " " + split;
                }
                sql += string.Format(" {0}[email protected]{1}", key.ParameterName, key.ParameterName);
            }
            return sql;
        }
        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="ConnectionString">ConnectionString</param>
        /// <param name="data">DataSet</param>
        /// <param name="tableName">string</param>
        /// <param name="param">SqlSelectEntity</param>
        protected void Select(string ConnectionString, DataSet data, string tableName, SqlSelectEntity param)
        {
            string select = string.Join(",", param.Select.ToArray());
            string where = " 1=1 ";
            if (param.Where != null)
            {
                where += "and " + SqlParameterToSql(param.Where, "and");
            }
            string sql = string.Format("select {0} from {1} where {2}", select, param.TableName, where);
            if (!string.IsNullOrEmpty(param.Sort)) {
                sql += string.Format(" order by {0}", param.Sort);
            }
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                if (param.Where != null)
                    cmd.Parameters.AddRange(param.Where);
                conn.Open();

                FillDataSet(data, cmd, tableName);
            }
        }
        /// <summary>
        /// 执行更新
        /// </summary>
        /// <param name="ConnectionString">ConnectionString</param>
        /// <param name="param">SqlUpdateEntity</param>
        /// <returns>int(受影响的行数)</returns>
        protected int Update(string ConnectionString, SqlUpdateEntity param)
        {
            string where = " 1=1 ";
            string update = SqlParameterToSql(param.Update,",");
            if (param.Where!=null)
            {
                where += "and " + SqlParameterToSql(param.Where, "and");
            }
            string sql = string.Format("update {0} set {1} where {2}", param.TableName, update, where);

            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddRange(param.Update);
                if (param.Where != null)
                    cmd.Parameters.AddRange(param.Where);
                conn.Open();

                return ExecuteNonQuery(cmd);
            }
        }
        /// <summary>
        /// 执行删除
        /// </summary>
        /// <param name="ConnectionString">ConnectionString</param>
        /// <param name="param">SqlDeleteEntity</param>
        /// <returns>int(受影响的行数)</returns>
        protected int Delete(string ConnectionString, SqlDeleteEntity param)
        {
            string where = " 1=1 ";
            if (param.Where!=null)
            {
                where += "and " + SqlParameterToSql(param.Where, "and");
            }
            string sql = string.Format("delete from {0} where {1}", param.TableName, where);

            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                if (param.Where != null)
                    cmd.Parameters.AddRange(param.Where);
                conn.Open();

                return ExecuteNonQuery(cmd);
            }
        }
        /// <summary>
        /// 执行插入
        /// </summary>
        /// <param name="ConnectionString">ConnectionString</param>
        /// <param name="param">SqlInsertEntity</param>
        /// <returns>int(受影响的行数)</returns>
        protected int Insert(string ConnectionString, SqlInsertEntity param)
        {

            List<string> fileds = new List<string>();
            List<string> values = new List<string>();
            foreach (SqlParameter parameter in param.Insert)
            {
                fileds.Add(parameter.ParameterName);
                values.Add("@" + parameter.ParameterName);
            }
            string sql = string.Format("insert into {0}({1}) values({2})", param.TableName, string.Join(",", fileds), string.Join(",", values));

            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddRange(param.Insert);
                conn.Open();

                return ExecuteNonQuery(cmd);
            }
        }
    }
    /// <summary>
    /// 查询的基类
    /// </summary>
    public class SqlEntity
    {
        /// <summary>
        /// 数据表名称
        /// </summary>
        public string TableName { get; set; }
    }
    /// <summary>
    /// Select Class
    /// </summary>
    public class SqlSelectEntity : SqlEntity
    {
        /// <summary>
        /// 需要查询的字段,查询所有字段传new List<string>(){"*"}
        /// </summary>
        public List<string> Select { get; set; }
        /// <summary>
        /// 查询条件new SqlParameter[]{ new SqlParameter("Filed",FiledValue) };
        /// 不需要@
        /// </summary>
        public SqlParameter[] Where { get; set; }
        /// <summary>
        /// 排序字符串 CreateTime Desc
        /// </summary>
        public string Sort { get; set; }

    }
    /// <summary>
    /// Insert Class
    /// </summary>
    public class SqlInsertEntity : SqlEntity
    {
        /// <summary>
        /// 插入的字段和值 new SqlParameter[]{ new SqlParameter("Filed",FiledValue) };
        /// 不需要@
        /// </summary>
        public SqlParameter[] Insert { get; set; }

    }
    /// <summary>
    /// Update Class
    /// </summary>
    public class SqlUpdateEntity : SqlEntity
    {
        /// <summary>
        /// 更新的字段和值 new SqlParameter[]{ new SqlParameter("Filed",FiledValue) };
        /// 不需要@
        /// </summary>
        public SqlParameter[] Update { get; set; }
        /// <summary>
        /// 查询条件new SqlParameter[]{ new SqlParameter("Filed",FiledValue) };
        /// 不需要@
        /// </summary>
        public SqlParameter[] Where { get; set; }
    }
    /// <summary>
    /// Delete Class
    /// </summary>
    public class SqlDeleteEntity : SqlEntity
    {
        /// <summary>
        /// 查询条件new SqlParameter[]{ new SqlParameter("Filed",FiledValue) };
        /// 不需要@
        /// </summary>
        public SqlParameter[] Where { get; set; }
    }
}

  

using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Text;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace MDCRM.DAL
{
/// <summary>
/// 数据访问基类
/// </summary>
public abstract class BaseProvider
{

protected string ConnectionString { get; set; }

protected BaseProvider()
{
this.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
}

/// <summary>
/// 构造
/// </summary>
/// <param name="connectionStringSectionName">链接字符串节点对应名称</param>
protected BaseProvider(string connectionStringSectionName)
{
this.ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringSectionName].ConnectionString;
}

/// <summary>
/// 执行 ExecuteNonQuery
/// </summary>
protected int ExecuteNonQuery(DbCommand cmd)
{
foreach (DbParameter param in cmd.Parameters)
{
if (param.Direction == ParameterDirection.Output || param.Direction == ParameterDirection.ReturnValue)
{
switch (param.DbType)
{
case DbType.AnsiString:
case DbType.AnsiStringFixedLength:
case DbType.String:
case DbType.StringFixedLength:
case DbType.Xml:
param.Value = string.Empty;
break;
case DbType.Boolean:
param.Value = false;
break;
case DbType.Byte:
param.Value = byte.MinValue;
break;
case DbType.Date:
case DbType.DateTime:
param.Value = DateTime.MinValue;
break;
case DbType.Currency:
case DbType.Decimal:
param.Value = decimal.MinValue;
break;
case DbType.Guid:
param.Value = Guid.Empty;
break;
case DbType.Double:
case DbType.Int16:
case DbType.Int32:
case DbType.Int64:
param.Value = 0;
break;
default:
param.Value = null;
break;
}
}
}

FilterSqlParameter(cmd);

return cmd.ExecuteNonQuery();
}

/// <summary>
/// 执行 ExecuteReader
/// </summary>
protected IDataReader ExecuteReader(DbCommand cmd)
{
FilterSqlParameter(cmd);

return ExecuteReader(cmd, CommandBehavior.Default);
}

/// <summary>
/// 执行 ExecuteReader
/// </summary>
protected IDataReader ExecuteReader(DbCommand cmd, CommandBehavior behavior)
{
FilterSqlParameter(cmd);

return cmd.ExecuteReader(behavior);
}

/// <summary>
/// 执行查询,并返回查询所返回的结果集中第一行的第一列。所有其他的列和行将被忽略。
/// </summary>
protected object ExecuteScalar(DbCommand cmd)
{
FilterSqlParameter(cmd);

return cmd.ExecuteScalar();
}

/// <summary>
/// 执行查询,返回数据结果集
/// </summary>
protected void FillDataSet(System.Data.DataSet dataSet, DbCommand cmd, string tableNames)
{
FillDataSet(dataSet, cmd, tableNames.Split(‘|‘));
}

/// <summary>
/// 执行查询,返回数据结果集
/// </summary>
protected void FillDataSet(System.Data.DataSet dataSet, DbCommand cmd, string[] tableNames)
{
System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter((System.Data.SqlClient.SqlCommand)cmd);
cmd.CommandTimeout = 10000;

string mapTableName = "Table";
for (int index = 0; index < tableNames.Length; index++)
{
if (tableNames[index] != null && tableNames[index].Length >= 0)
{
dataAdapter.TableMappings.Add(mapTableName, tableNames[index]);
mapTableName = "Table" + (index + 1).ToString();
}
}

FilterSqlParameter(cmd);

dataAdapter.Fill(dataSet);
}

/// <summary>
/// 集合转成搜索条件
/// </summary>
protected string ListToString<T>(List<T> list, char departChar)
{
string listStr = string.Empty;
if (list != null && list.Count > 0)
{
foreach (T tType in list)
{
if (!string.IsNullOrEmpty(tType.ToString().Trim()))
listStr += tType.ToString().Trim() + departChar.ToString();
}
}
return listStr.Trim(departChar);
}

/// <summary>
/// 集合转成搜索条件
/// </summary>
protected string ListToString<T>(List<T> list, string departStr)
{
string listStr = string.Empty;
if (list != null && list.Count > 0)
{
foreach (T tType in list)
{
if (!string.IsNullOrEmpty(tType.ToString().Trim()))
listStr += tType.ToString().Trim() + departStr.ToString();
}
}

if (!string.IsNullOrEmpty(listStr))
listStr = listStr.Remove(listStr.Length - departStr.Length);

return listStr;
}

/// <summary>
/// 数组转成搜索条件
/// </summary>
protected string ArrayToString<T>(T[] arr, char departChar)
{
string listStr = string.Empty;
if (arr != null && arr.Length > 0)
{
foreach (T tType in arr)
{
if (!string.IsNullOrEmpty(tType.ToString().Trim()))
listStr += tType.ToString().Trim() + departChar.ToString();
}
}
return listStr.Trim(departChar);
}

/// <summary>
/// 过滤注入式特殊符号
/// </summary>
protected SqlParameter[] FilterSqlParameterArray(params SqlParameter[] parameterValues)
{
int paraCount = 0;
paraCount = parameterValues.Length;
SqlParameter[] paraArray = new SqlParameter[paraCount];
for (int i = 0; i < paraCount; i++)
{
SqlParameter parameterValue = parameterValues[i];
if (parameterValue.DbType == DbType.AnsiString || parameterValue.DbType == DbType.String || parameterValue.SqlDbType == SqlDbType.VarChar || parameterValue.SqlDbType == SqlDbType.NVarChar || parameterValue.SqlDbType == SqlDbType.Text)
{
if (parameterValue.Value != null && parameterValue.Value.ToString() != "")
parameterValue.Value = parameterValue.Value.ToString().Replace("‘", "‘‘").Replace("--", "");
}
paraArray[i] = parameterValue;
}
return paraArray;
}

/// <summary>
/// 过滤注入式特殊符号
/// </summary>
protected void FilterSqlParameter(DbCommand cmd)
{
int paraCount = 0;
paraCount = cmd.Parameters.Count;
SqlParameter[] paraArray = new SqlParameter[paraCount];
for (int i = 0; i < paraCount; i++)
{
SqlParameter parameterValue = (SqlParameter)cmd.Parameters[i];
if (parameterValue.DbType == DbType.AnsiString || parameterValue.DbType == DbType.String || parameterValue.SqlDbType == SqlDbType.VarChar || parameterValue.SqlDbType == SqlDbType.NVarChar || parameterValue.SqlDbType == SqlDbType.Text)
{
if (parameterValue.Value != null && parameterValue.Value.ToString() != "")
parameterValue.Value = parameterValue.Value.ToString().Replace("‘", "‘‘").Replace("--", "");
}
paraArray[i] = parameterValue;
}

cmd.Parameters.Clear();
cmd.Parameters.AddRange(paraArray);
}
/// <summary>
/// 将传入的参数生成为相应的sql语句
/// </summary>
/// <param name="param">SqlParameter[]</param>
/// <param name="split">split</param>
/// <returns>string</returns>
protected string SqlParameterToSql(SqlParameter[] param, string split)
{
string sql = string.Empty;
if (param == null) return sql;
for (int i = 0; i < param.Length; i++)
{
SqlParameter key = param[i];
if (i > 0)
{
sql += " " + split;
}
sql += string.Format(" {0}[email protected]{1}", key.ParameterName, key.ParameterName);
}
return sql;
}
/// <summary>
/// 执行查询
/// </summary>
/// <param name="ConnectionString">ConnectionString</param>
/// <param name="data">DataSet</param>
/// <param name="tableName">string</param>
/// <param name="param">SqlSelectEntity</param>
protected void Select(string ConnectionString, DataSet data, string tableName, SqlSelectEntity param)
{
string select = string.Join(",", param.Select.ToArray());
string where = " 1=1 ";
if (param.Where != null)
{
where += "and " + SqlParameterToSql(param.Where, "and");
}
string sql = string.Format("select {0} from {1} where {2}", select, param.TableName, where);
if (!string.IsNullOrEmpty(param.Sort)) {
sql += string.Format(" order by {0}", param.Sort);
}
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
if (param.Where != null)
cmd.Parameters.AddRange(param.Where);
conn.Open();

FillDataSet(data, cmd, tableName);
}
}
/// <summary>
/// 执行更新
/// </summary>
/// <param name="ConnectionString">ConnectionString</param>
/// <param name="param">SqlUpdateEntity</param>
/// <returns>int(受影响的行数)</returns>
protected int Update(string ConnectionString, SqlUpdateEntity param)
{
string where = " 1=1 ";
string update = SqlParameterToSql(param.Update,",");
if (param.Where!=null)
{
where += "and " + SqlParameterToSql(param.Where, "and");
}
string sql = string.Format("update {0} set {1} where {2}", param.TableName, update, where);

using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddRange(param.Update);
if (param.Where != null)
cmd.Parameters.AddRange(param.Where);
conn.Open();

return ExecuteNonQuery(cmd);
}
}
/// <summary>
/// 执行删除
/// </summary>
/// <param name="ConnectionString">ConnectionString</param>
/// <param name="param">SqlDeleteEntity</param>
/// <returns>int(受影响的行数)</returns>
protected int Delete(string ConnectionString, SqlDeleteEntity param)
{
string where = " 1=1 ";
if (param.Where!=null)
{
where += "and " + SqlParameterToSql(param.Where, "and");
}
string sql = string.Format("delete from {0} where {1}", param.TableName, where);

using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
if (param.Where != null)
cmd.Parameters.AddRange(param.Where);
conn.Open();

return ExecuteNonQuery(cmd);
}
}
/// <summary>
/// 执行插入
/// </summary>
/// <param name="ConnectionString">ConnectionString</param>
/// <param name="param">SqlInsertEntity</param>
/// <returns>int(受影响的行数)</returns>
protected int Insert(string ConnectionString, SqlInsertEntity param)
{

List<string> fileds = new List<string>();
List<string> values = new List<string>();
foreach (SqlParameter parameter in param.Insert)
{
fileds.Add(parameter.ParameterName);
values.Add("@" + parameter.ParameterName);
}
string sql = string.Format("insert into {0}({1}) values({2})", param.TableName, string.Join(",", fileds), string.Join(",", values));

using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddRange(param.Insert);
conn.Open();

return ExecuteNonQuery(cmd);
}
}
}
/// <summary>
/// 查询的基类
/// </summary>
public class SqlEntity
{
/// <summary>
/// 数据表名称
/// </summary>
public string TableName { get; set; }
}
/// <summary>
/// Select Class
/// </summary>
public class SqlSelectEntity : SqlEntity
{
/// <summary>
/// 需要查询的字段,查询所有字段传new List<string>(){"*"}
/// </summary>
public List<string> Select { get; set; }
/// <summary>
/// 查询条件new SqlParameter[]{ new SqlParameter("Filed",FiledValue) };
/// 不需要@
/// </summary>
public SqlParameter[] Where { get; set; }
/// <summary>
/// 排序字符串 CreateTime Desc
/// </summary>
public string Sort { get; set; }

}
/// <summary>
/// Insert Class
/// </summary>
public class SqlInsertEntity : SqlEntity
{
/// <summary>
/// 插入的字段和值 new SqlParameter[]{ new SqlParameter("Filed",FiledValue) };
/// 不需要@
/// </summary>
public SqlParameter[] Insert { get; set; }

}
/// <summary>
/// Update Class
/// </summary>
public class SqlUpdateEntity : SqlEntity
{
/// <summary>
/// 更新的字段和值 new SqlParameter[]{ new SqlParameter("Filed",FiledValue) };
/// 不需要@
/// </summary>
public SqlParameter[] Update { get; set; }
/// <summary>
/// 查询条件new SqlParameter[]{ new SqlParameter("Filed",FiledValue) };
/// 不需要@
/// </summary>
public SqlParameter[] Where { get; set; }
}
/// <summary>
/// Delete Class
/// </summary>
public class SqlDeleteEntity : SqlEntity
{
/// <summary>
/// 查询条件new SqlParameter[]{ new SqlParameter("Filed",FiledValue) };
/// 不需要@
/// </summary>
public SqlParameter[] Where { get; set; }
}
}

时间: 2024-10-03 06:46:36

数据访问公共类的相关文章

【ASP.NET-中级】SQLHelper数据访问公共类

ASP.NET开发中的三层开发思想指的是UI层(界面显示层),BLL层(业务逻辑层),DAL层(数据访问层)三层,三层之间通过函数的调用来达到降低耦合,易于系统维护的目的,SQLHelper助手类的主要作用在于接收并执行来自各个数据表累传来的sql语句或存储过程.一般的SQLHelper类中主要包括以下几个函数功能: 1.执行不带参数的增删改方法 2.执行带参数的增删改方法. 3.执行不带参数的查询方法. 4.执行带参数的查询方法. 作为一个程序员SQLHelper类编写的好坏不仅影响着系统的可

C# 数据库访问公共类

Web.config配置 <appSettings> <!--数据连接字符串--> <add key="DbProviderType" value="Oracle" /> <add key="ConnectionString" value="Data Source=(DESCRIPTION= (ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0

ADO数据访问助手类SQLServeHelper.cs

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Data.SqlClient; using System.Data; namespace test { /// <summary> /// 封装对数据库的操作 /// </summary> public sealed class SQ

Util应用程序框架公共操作类(一):数据类型转换公共操作类(介绍篇)

本系列文章将介绍一些对初学者有帮助的辅助类,这些辅助类本身并没有什么稀奇之处,如何能发现需要封装它们可能更加重要,所谓授之以鱼不如授之以渔,掌握封装公共操作类的技巧才是关键,我会详细说明创建这些类的动机和思考过程,以帮助初学者发现和封装自己需要的东西.创建公共操作类的技巧,大家可以参考我的这篇文章——应用程序框架实战十二:公共操作类开发技巧(初学者必读). 封装公共操作类,不仅要把技术上困难的封装进来,还需要不断观察自己的代码,以找出哪些部分可以更加简化.本文将介绍一个容易被大家所忽视的东西——

重要!!!实体类、数据访问类

创建两个类: users类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 实体类_数据访问类.App_Code { public class Users { private int _Ids; /// <summary> /// ids /// </summary> public int Ids { get { return _Ids;

实体类、数据访问类中的属性拓展

类中: using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; namespace 实体类_数据访问类.App_Code { public class Users { SqlConnection conn = null; SqlCommand cmd = null; public Users() { conn = new S

数据访问类的封装与三大类

数据访问类的封装: 数据访问类分为三大类:连接类,实体类,数据访问类. 1.连接类: public class lianjie { private static string connection="server=.;database=mydb;uid=sa;pwd=789234178"; public static Sqlconnection conn { get { SqlConnection conn=new Sqlconnection(connecction); return

C#连接层的数据访问类(简单版)

目前只写了与SqlServer的数据处理. 首先是静态工厂类. using System; using System.Configuration; namespace XPSoft.BaseLibrary.DataAccess { /// <summary> /// 数据访问对象的静态工厂类,用于创建不同类型的数据访问对象. /// </summary> public static class DataAccessFactory { /// <summary> ///

实体类,数据访问类.字符串攻击.防攻击

程序分三层:界面层.业务逻辑层.数据访问层 这里主要操作的是数据访问层,数据访问层又分为: 1.实体类 2.数据访问类 存放位置:APP_Code文件中 一.实体类 最简单的封装把数据库的表名变成类的类名把数据库的每一个列,变为实体类中的成员变量和属性列名与属性名一致 有几行就有几个实体类 这是一个简单的实体类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namesp