Npgsql query and format with output parameters

  public string UploadExceldata(string json)

        {

            string connectionString = String.Format(new Connection().ConnectionString);

            DataSet ds = new DataSet();

            BaseResultData oBaseResult = new BaseResultData();

            var connection = new Npgsql.NpgsqlConnection(connectionString);

            try

            {

              

                if (!(connection.State == ConnectionState.Open))

                {

                    connection.Open();

                }

                //NpgsqlTransaction trans = connection.BeginTransaction();

                var command = new Npgsql.NpgsqlCommand("systemtracker_dbo.pr_create_user_from_excel_json", connection);

                command.CommandType = System.Data.CommandType.StoredProcedure;

                IDbDataAdapter dbDataAdapter = new NpgsqlDataAdapter();




                //Parameter Settings

                var parameter = command.CreateParameter();

                parameter.ParameterName = "par_p_json";

                parameter.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;

                parameter.Value = json;

                command.Parameters.Add(parameter);




                var parameterTwo = command.CreateParameter();

                parameterTwo.ParameterName = "statuscode";

                parameterTwo.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;

                parameterTwo.Value = null;

                parameterTwo.Direction = ParameterDirection.Output;

                command.Parameters.Add(parameterTwo);




                var parameterThree = command.CreateParameter();

                parameterThree.ParameterName = "errormsg";

                parameterThree.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;

                parameterThree.Value = null;

                parameterThree.Direction = ParameterDirection.Output;

                command.Parameters.Add(parameterThree);


                command.ExecuteNonQuery();


                if (string.IsNullOrEmpty(parameterTwo.Value.ToString()))

                {

                    oBaseResult.ResultCode = "The procedure execution has started however the return status remained to the default value.";

                }

                else if (parameterTwo.Value.ToString() == "0" && parameterTwo.Value.ToString() == "")

                {

                    oBaseResult.ResultCode = "The procedure execution has started however the return status remained to the default value.";

                }

                else if (parameterTwo.Value.ToString() == "0" && parameterTwo.Value.ToString() != "")

                {

                    oBaseResult.ResultCode = parameterThree.Value.ToString();

                }

                else if (parameterTwo.Value.ToString() == "1")

                {

                    oBaseResult.ResultCode = "Success";

                }

                else

                {

                    oBaseResult.ResultCode = parameterThree.Value.ToString();

                }

                //trans.Commit();

            }

            catch (Exception ex)

            {

                oBaseResult.ResultCode = "The Error is in Procedure execution function name is      AddErrorLogs and the parameters that were passed is Json shared " + json + "Error Message is " + ex.Message.ToString();

            }

            finally

            {

                connection.Close();

            }

            return oBaseResult.ResultCode;


        }



    public class BaseResultData

    {

        public string data { get; set; }

        public string success { get; set; }

        public string ResultCode { get; set; }

    }

Comments

Popular posts from this blog

Npgsql Helper class