示例#1
0
        /// <summary>
        /// 对象参数转换DbParameter
        /// </summary>
        /// <returns></returns>
        public static DbParameter[] GetParameter(Hashtable ht)
        {
            IList <DbParameter> parameter = new List <DbParameter>();
            DbType dbtype = new DbType();

            foreach (string key in ht.Keys)
            {
                if (ht[key] is DateTime)
                {
                    dbtype = DbType.DateTime;
                }
                else
                {
                    dbtype = DbType.String;
                }
                parameter.Add(DbParameters.CreateDbParameter(DbParameters.CreateDbParmCharacter() + key, ht[key], dbtype));
            }
            return(parameter.ToArray());
        }
示例#2
0
        /// <summary>
        /// 对象参数转换DbParameter
        /// </summary>
        /// <returns></returns>
        public static DbParameter[] GetParameter <T>(T entity)
        {
            IList <DbParameter> parameter = new List <DbParameter>();
            DbType dbtype = new DbType();
            Type   type   = entity.GetType();

            PropertyInfo[] props = type.GetProperties();
            foreach (PropertyInfo pi in props)
            {
                if (pi.GetValue(entity, null) != null)
                {
                    switch (pi.PropertyType.ToString().ToLower())
                    {
                    case "system.nullable`1[system.int32]":
                        dbtype = DbType.Int32;
                        break;

                    case "system.nullable`1[system.decimal]":
                        dbtype = DbType.Decimal;
                        break;

                    case "system.nullable`1[system.datetime]":
                        dbtype = DbType.DateTime;
                        break;

                    case "system.nullable`1[system.int64]":
                        dbtype = DbType.Int64;
                        break;

                    case "system.nullable`1[system.boolean]":
                        dbtype = DbType.Boolean;
                        break;

                    default:
                        dbtype = DbType.String;
                        break;
                    }
                    parameter.Add(DbParameters.CreateDbParameter(DbParameters.CreateDbParmCharacter() + pi.GetMappingAttributeName(), pi.GetValue(entity, null), dbtype));
                }
            }
            return(parameter.ToArray());
        }
示例#3
0
        /// <summary>
        /// 执行存储过程  存储过程必须带Out的Out_Res参数
        /// </summary>
        /// <param name="dbConnection">连接</param>
        /// <param name="storedProcName">存储过程名称</param>
        /// <param name="parameters">入参数</param>
        /// <returns>Out_Res为Out参数的执行结果</returns>
        public static object ExecuteByProc(IDbConnection dbConnection, string storedProcName, DbParameter[] parameters, out int rowsAffected)
        {
            try
            {
                using (IDbConnection conn = dbConnection)
                {
                    using (IDbCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = storedProcName;
                        if (conn.State != ConnectionState.Open)
                        {
                            conn.Open();
                        }
                        if (parameters != null)
                        {
                            parameters = DbParameters.ToDbParameter(parameters);

                            foreach (var parameter in parameters)
                            {
                                cmd.Parameters.Add(parameter);
                            }
                        }
                        cmd.Parameters.Add(DbParameters.CreateDbParameter("@Out_Res", "", DbType.String, 2000, ParameterDirection.Output));
                        rowsAffected = cmd.ExecuteNonQuery();
                        DbParameter obj = (DbParameter)cmd.Parameters["@Out_Res"]; //@Out_Res和具体的存储过程参数对应
                        cmd.Parameters.Clear();
                        return(Convert.ToString(obj.Value));
                    }
                }
            }
            catch (DbException ex)
            {
                DbLog.WriteLog(ex, storedProcName);
                if (dbConnection != null)
                {
                    dbConnection.Close();
                }
                throw ex;
            }
        }