Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8232341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:48:11+00:00 2026-06-07T17:48:11+00:00

I have been writing some simple c# and I usually use the same class

  • 0

I have been writing some simple c# and I usually use the same class and functions that insert and grab the data from the database.

For example, this is my function:

    public bool insert_and_ConfirmSQL(String Query, String comments)
    {
        bool success = false;
        NpgsqlCommand cmd = new NpgsqlCommand();
        NpgsqlConnection mycon = new NpgsqlConnection();
        string connstring = String.Format("Server={0};Port={1}; User Id={2};Password={3};Database={4};timeout=1000;CommandTimeout=120;", tbHost, tbPort, tbUser, tbPass, tbDataBaseName);
        mycon.ConnectionString = connstring;
        cmd = mycon.CreateCommand();
        cmd.CommandText = Query;
        mycon.Open();

        int temp = 0;
        try
        {
            temp = cmd.ExecuteNonQuery();
            success = true;
        }
        catch
        {
           if (mycon.State == ConnectionState.Open)
            {
                mycon.Close();
            }
        }

        return success;
    }

Now I know that this query is not safe against injections and I need to use prepared statements. But I don’t understand how should I approach this when each of my queries is different? is there a “universal” function that inserts any query and “prepares” it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:48:15+00:00Added an answer on June 7, 2026 at 5:48 pm

    If you are looking for a more generalized way of performing inserts/updates/deletes, perhaps the following is suitable (uses SqlClient but is easily adapted for NpgsqlClient):

    public static object ExecuteActionProcedure(System.Data.CommandType CommandType, string CommandText, string[] Parameters, object[] Values)
    {
      try
      {
        using (SqlConnection con = new SqlConnection())
        {
          con.ConnectionString = ConfigurationManager.ConnectionStrings["YourConnection"].ConnectionString;
          con.Open();
          using (SqlCommand cmd = new SqlCommand())
          {
    
            cmd.Connection = con;
            cmd.CommandType = CommandType;
            cmd.CommandText = CommandText;
            SqlParameter result = new SqlParameter();
            result.ParameterName = "ResultValue";
            result.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(result);
            for (int i = 0; i < Parameters.Length; i++)
            {
              cmd.Parameters.AddWithValue(Parameters[i], Values[i]);
            }
            cmd.ExecuteNonQuery();
            return (int)result.Value;
          }
        }
      }
        }
      }
      catch (Exception ex)
      {
        throw new Exception(ex.Message);
        return null;
      }
    }
    

    Call statement:

    ExecuteActionProcedure(CommandType.StoredProcedure, "aspnet_SaveFullName", new string[] { "UserName", "FullName" }, new object[] { model.UserName, model.FullName });
    

    Note that you can pass this method an SQL string that includes parameters as well as a stored procedure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been writing some jQuery functions that have JavaScript variables and looping, etc inside
I have been writing some code that creates a generic blog. Its features are
I have been learning some Haskell and writing very simple programs. I want to
I have been playing around with writing some simple card games in Python for
I've been writing some simple test cases for one of my assignments, and have
I have been writing some rspec tests, and right now I want to verify
I have been writing some code to create a byte array I will be
I am trying to learn more about the DOM and have been writing some
I have been messing around with writing some stored procedures in .NET code with
I have been given the task of re-writing some libraries written in C# so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.