0

I need to add this command.CommandText result to command2.CommandText instead "result"

string connString = "connect data;";

MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
MySqlCommand command1 = conn.CreateCommand();
MySqlCommand command2 = conn.CreateCommand();

command.CommandText = "SELECT `order_id` FROM `test` WHERE `order_item_type`='line_item' AND `order_offer_send`='0';";
command2.CommandText = "SELECT `meta_value` FROM `test1` WHERE `order_item_id`='" + result + "'"; 
1
  • First of all, please parameterize your Queries, like this: Working with Parameters ! Second: Please show us what you have tried so far. Commented Mar 19, 2015 at 14:38

1 Answer 1

1

i would work with parameters to avoid SQL Injection attacks and using directive to avoid open connections and a better usage of the gc:

string connString = "connect data;";
string Command = "SELECT `order_id` FROM `test` WHERE `order_item_type`='line_item' AND `order_offer_send`= @order_offer_send limit 1;";
string Command2 = "SELECT `meta_value` FROM `test1` WHERE `order_item_id`= @result limit 1"; 
int OfferID = -1;
string meta_value = null;
using (MySqlConnection mConnection = new MySqlConnection(connString))
{
    mConnection.Open();
    using (MySqlCommand myCmd = new MySqlCommand(Command, mConnection))
    {
        myCmd.Parameters.Add(new MySqlParameter("@order_offer_send", "0"));
        OfferID = (int)myCmd.ExecuteScalar();
    }
    using (MySqlCommand myCmd = new MySqlCommand(Command2, mConnection))
    {
        myCmd.Parameters.Add(new MySqlParameter("@result", OfferID));
        meta_value = (string)myCmd.ExecuteScalar();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

An other function to get the order for an order_offer_send would be even better. +1 for using by the way.
you're right, probably this can be done even with a join within one select
Yeah that would be another option, probably a better one unless there were some re-use opportunities. I was thinking more replacing OfferID in the second query with a function to get it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.