Ejemplo n.º 1
0
        /// <summary>
        /// Accept only int, Int16, long, DateTime, string (NVarcha of size  50),
        /// bool, decimal ( of size 16,2), float
        /// </summary>
        /// <typeparam name="T">Given type of object.</typeparam>
        /// <param name="storedProcedureName">Accept SQL procedure name in string.</param>
        /// <param name="parameterCollection">Accept key value collection for parameters.</param>
        /// <param name="outputParameterName">Accept output parameter for the stored procedures.</param>
        /// <param name="outputParameterValue">OutPut parameter value.</param>
        /// <returns>Type of the object implementing.</returns>
        public T ExecuteNonQueryAsGivenType <T>(string storedProcedureName, List <KeyValuePair <string, object> > parameterCollection, string outputParameterName, object outputParameterValue)
        {
            using (NpgsqlConnection conn = new NpgsqlConnection(ConnectionString))
            {
                NpgsqlCommand command = new NpgsqlCommand();
                command.Connection  = conn;
                command.CommandText = storedProcedureName;
                command.CommandType = CommandType.StoredProcedure;

                for (int i = 0; i < parameterCollection.Count; i++)
                {
                    NpgsqlParameter sqlParaMeter = new NpgsqlParameter();
                    sqlParaMeter.IsNullable    = true;
                    sqlParaMeter.ParameterName = parameterCollection[i].Key;
                    sqlParaMeter.Value         = parameterCollection[i].Value;
                    command.Parameters.Add(sqlParaMeter);
                }

                command = DataSourceHelper.AddOutPutParametrofGivenType <T>(command, outputParameterName, outputParameterValue);
                conn.Open();
                command.ExecuteNonQuery();
                return((T)command.Parameters[outputParameterName].Value);;
            }
        }