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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:13:22+00:00 2026-05-25T02:13:22+00:00

MySqlCommand cmd = new MySqlCommand (@INSERT INTO Table(field) VALUES(‘somevalue’); + SELECT * FROM table,cn);

  • 0
MySqlCommand cmd = new MySqlCommand
                    (@"INSERT INTO Table(field) VALUES('somevalue');" +
                    "SELECT * FROM table",cn);

This works fine to me since I’m only passing those statements to my MySQL server.

Is it OK to use ExecuteReader() when inserting & updating & deleting?

I usually use ExecuteNonQuery() on those.

  • 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-05-25T02:13:23+00:00Added an answer on May 25, 2026 at 2:13 am

    You’re just fine bundling the extra INSERT in along with your SELECT with ExecuteReader(). This is okay.

    I do see two things that are potentially not okay… nothing in the code itself, but what you showed is simplified, and the simplified code hints at some potential poor practices:

    The first not okay is that your code looks like it might be using string concatenation to substitute values into your query. Something like this:

    MySqlCommand cmd = new MySqlCommand
                    (@"INSERT INTO Table(field) VALUES('" + somevariable + "');" +
                    "SELECT * FROM table",cn);
    

    That is a huge problem, as it opens a gaping security hole in your application. Instead, you need to use query parameters, so the code looks more like this:

    MySqlCommand cmd = new MySqlCommand
                    (@"INSERT INTO Table(field) VALUES(@somevalue);" +
                    "SELECT * FROM table",cn);
    cmd.Parameters.Add("@somevalue", SqlDbType.VarChar, 50).Value = somevariable;
    

    The other potential problem is that your command and, more importantly, your connection, should be wrapped in a try/finally block (or for preference a using block), like this:

    using (var cn = new MySqlConnection("..."))
    using (var cmd = new MySqlCommand("@INSERT INTO Table(field).... ", cn))
    {
        cmd.Parameters.Add(...);
        cn.Open();
        using (var rdr = cmd.ExecuteReader())
        {
            while (rdr.Read())
            {
                //...
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple query which returns 25,026 rows: MySqlCommand cmd = new MySqlCommand(SELECT
MySqlCommand checkUsername = conn.CreateCommand(); checkUsername.CommandText = SELECT COUNT(*) FROM users WHERE username='admin'; MessageBox.Show(The count
I made this test to select a row, which I created previously, from two
My idea is to create some generic classes for Insert/Update/Select via a C# (3.5)
string connectionString = ConfigurationManager.AppSettings[AllRttpDBConnectionString]; MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand command = connection.CreateCommand(); command.CommandText
I'm trying to add a row into a mysql database table using visual studio
I'm trying to populate a DataTable, to build a LocalReport, using the following: MySqlCommand
Does the MySQL command : FLUSH TABLES; flush every table in the current database,
Is there a MySQL command to count the number of rows in a table,
Why do I get the following exception when I did this, what mistake did

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.