コード例 #1
0
        public void ExecNonQuery <TModel, TOut>(string sqlCommandString, TModel objectmodel, string outPutName,
                                                out TOut outPutValue) where TModel : class
        {
            try
            {
                var dataTable = ConvertToDataTable(objectmodel);

                using (var sqlConnection = new SqlConnection(GetConnectionString))
                {
                    sqlConnection.Open();
                    using (var sqlCommand = new SqlCommand(sqlCommandString, sqlConnection))
                    {
                        sqlCommand.CommandType = CommandType.StoredProcedure;

                        var returnVal = sqlCommand.Parameters.Add($"@{outPutName}", SqlDbTypeHelper.GetDbType(typeof(TOut)));
                        returnVal.Direction = ParameterDirection.Output;

                        var param = sqlCommand.Parameters.AddWithValue("@dt", dataTable);
                        param.SqlDbType = SqlDbType.Structured;

                        sqlCommand.ExecuteNonQuery();

                        outPutValue = (TOut)returnVal.Value;
                    }
                }
            }
            catch (Exception ex)
            {
                var serviceDataError = new DataErrorDto();
                serviceDataError.ErrorMessage = ex.Message;
                serviceDataError.ErrorDetails = ex.ToString();
                log.Error($"Type Error: {serviceDataError.ErrorMessage}\n ErrorDetails {ex.ToString()}");
                throw new FaultException <DataErrorDto>(serviceDataError, ex.ToString());
            }
        }
コード例 #2
0
        public TModel[] ExecProcGetModels <TModel, TOut>(string sqlCommandString, string outPutName, out TOut outPutValue,
                                                         params SqlParameter[] inputParametrs) where TModel : class, new()
        {
            var result = default(TModel[]);

            try
            {
                using (var sqlConnection = new SqlConnection(GetConnectionString))
                {
                    sqlConnection.Open();
                    using (var sqlCommand = new SqlCommand(sqlCommandString, sqlConnection))
                    {
                        sqlCommand.CommandType = CommandType.StoredProcedure;
                        var outPutParametr = sqlCommand.Parameters.Add($"@{outPutName}", SqlDbTypeHelper.GetDbType(typeof(TOut)));
                        outPutParametr.Direction = ParameterDirection.Output;
                        sqlCommand.Parameters.AddRange(inputParametrs);

                        using (var dataTable = new DataTable())
                        {
                            dataTable.Load(sqlCommand.ExecuteReader());
                            result      = dataTable.ConvertToArrayOfModels <TModel>();
                            outPutValue = (TOut)outPutParametr.Value;
                        }
                        return(result);
                    }
                }
            }
            catch (Exception ex)
            {
                var serviceDataError = new DataErrorDto();
                serviceDataError.ErrorMessage = ex.Message;
                serviceDataError.ErrorDetails = ex.ToString();
                log.Error($"Type Error: {serviceDataError.ErrorMessage}\n ErrorDetails {ex.ToString()}");
                throw new FaultException <DataErrorDto>(serviceDataError, ex.ToString());
            }
        }
コード例 #3
0
        public void ExecNonQuery <TOut>(string sqlCommandString, string outPutName, out TOut outPutValue, params SqlParameter[] parametrs)
        {
            try
            {
                using (var sqlConnection = new SqlConnection(GetConnectionString))
                {
                    sqlConnection.Open();
                    using (var sqlCommand = new SqlCommand(sqlCommandString, sqlConnection))
                    {
                        var outPutParametr = sqlCommand.Parameters.Add($"@{outPutName}", SqlDbTypeHelper.GetDbType(typeof(TOut)));
                        outPutParametr.Direction = ParameterDirection.Output;

                        sqlCommand.CommandType = CommandType.StoredProcedure;
                        sqlCommand.Parameters.AddRange(parametrs);
                        sqlCommand.ExecuteNonQuery();
                        outPutValue = (TOut)outPutParametr.Value;
                    }
                }
            }
            catch (Exception ex)
            {
                var serviceDataError = new DataErrorDto();
                serviceDataError.ErrorMessage = ex.Message;
                serviceDataError.ErrorDetails = ex.ToString();
                log.Error($"Type Error: {serviceDataError.ErrorMessage}\n ErrorDetails {ex.ToString()}");
                throw new FaultException <DataErrorDto>(serviceDataError, ex.ToString());
            }
        }