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 8951825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:45:03+00:00 2026-06-15T13:45:03+00:00

I want to remove manual escaping from my code. Instead of this I want

  • 0

I want to remove manual escaping from my code. Instead of this I want to use SqlParameter object.

foreach (ThreadPost post in ThreadPosts)
{
     string newMessage = Clean(post.Message);
     string oldMessage = post.Message;

     // escape ' character for prevent errors in SQL script
     // I want to pass newMessage and oldMessage as an SqlParameters to prevent unexpected changes, but how it could be done based on the code bellow???
     newMessage = newMessage.Replace("'", "''");
     oldMessage = oldMessage.Replace("'", "''");

     cmdText += string.Format("INSERT INTO ThreadCleanup VALUES ({0}, {1}, '{2}', '{3}')",
          post.ID.ToString(),
          "NULL",
          oldMessage,
          newMessage);
     }
}
if (!string.IsNullOrEmpty(cmdText))
{
     using (SqlConnection con = new SqlConnection(CONNSTR))
     {
          con.Open();
          SqlTransaction tran = con.BeginTransaction(IsolationLevel.ReadUncommitted);
          try
          {
              using (SqlCommand cmd = new SqlCommand(cmdText, con, tran))
              {
                 cmd.ExecuteNonQuery(); // updated records
              }
              tran.Commit();
          }
          catch (SqlException ex)
          {
              tran.Rollback();
          }
          con.Close();
     }
}
  • 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-15T13:45:04+00:00Added an answer on June 15, 2026 at 1:45 pm

    You can do it roughly the same way that you do now – by formatting an insert in a loop, but instead of using one insert per the new/old pair, use one statement for all pairs. You should use column names explicitly to avoid relying on the column order in the table (a big no-no in production) and to avoid creating the second parameter in a loop, even though you always send a NULL to it.

    var cmdText = new StringBuilder("INSERT INTO ThreadCleanup (id,oldMessage,newMessage) VALUES ");
    var args = new List<Tuple<long,string,string>>();
    foreach (ThreadPost post in ThreadPosts) {
         int cnt = args.Count();
         if (cnt != 0) {
             cmdText.Append(",");
         }
         cmdText.AppendFormat("(@id{0}, @old{0}, @new{0})", cnt);
         args.Add(new Tuple<long,string,string>(post.ID, post.Message, Clean(post.Message)));
    }
    

    At this point, you have a SQL string that looks like this:

    INSERT INTO ThreadCleanup (id,oldMessage,newMessage) VALUES
    (@id0, @old0, @new0), (@id1, @old1, @new1), (@id2, @old2, @new2), ...
    

    You also have a list of Tuple<long,string,string> for each of the parameters, so you can do this:

    if (args.Count != 0) {
        using (SqlConnection con = new SqlConnection(CONNSTR)) {
            con.Open();
            SqlTransaction tran = con.BeginTransaction(IsolationLevel.ReadUncommitted);
            try {
                using (SqlCommand cmd = new SqlCommand(cmdText, con, tran)) {
                   for (var i = 0 ; i != args.Count ; i++) {
                       cmd.Parameters.AddWithValue("@id"+i, args[i].Item1);
                       cmd.Parameters.AddWithValue("@old"+i, args[i].Item2);
                       cmd.Parameters.AddWithValue("@new"+i, args[i].Item3);
                   }
                   cmd.ExecuteNonQuery(); // updated records
                }
                tran.Commit();
            } catch (SqlException ex) {
                tran.Rollback();
            }
            con.Close();
        }
    }
    

    The advantage of doing it in one go is that you make a single round-trip to your database no matter how many records you must insert. Other than that, the solution follows the same pattern as your current one, so the performance should be comparable.

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

Sidebar

Related Questions

So I am trying to remove index.php from address using this manual: http://dev.lohv.eu/1/user_guide/general/urls.html Atm
I use this code: I'm trying to remove the categoryPath node but keep its
i want to remove the link from my header using CSS so that only
I want to remove the string code=hads1328fas& on my URL, so that BEFORE http://example.com/main/index.php?code=hads1328fas&store=food#home
I want to remove duplicate lines from a file, without sorting the file. Example
I want to remove a key from a dictionary if it is present. I
I want to remove an item from ListView , but I don't know how
I have a list and I want to remove a single element from it.
The following example is taken from: http://php.net/manual/en/function.curl-multi-close.php#example-3540 This example will create two cURL handles,
I want to use this page to determine if the user has either updated

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.