• 数据访问公共类(BaseProvider)


    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}=@{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}=@{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; }
    }
    }

  • 相关阅读:
    android matrix
    canvas变换
    Gradle常用命令
    android View实现变暗效果
    常用设计模式
    Android无埋点数据收集SDK关键技术
    如何打开小米,oppo,华为等手机的系统应用的指定页面
    Android 性能优化
    SpringBoot入坑-请求参数传递
    SpringBoot入坑-配置文件使用
  • 原文地址:https://www.cnblogs.com/ahlx/p/5226840.html
Copyright © 2020-2023  润新知