2

I am using parameterized select queries in conjunction with my program and as a whole, it works except when I try to join tables.

Here is a small excerpt what I have done:

using (MySqlCommand cmd = new MySqlCommand(paramQuery.ToSql(), connection)) {

/*
   paramQuery.ToSql() equals:

   "Select tableOne.ID, tableOne.Department, tableTwo.Name
   From tableOne, tableTwo
   Where tableOne.ID = @param0"
*/


    for (int index = 0; index < paramQuery.Parameters().Count; index++) 
        cmd.Parameters.AddWithValue(paramQuery.Parameters().ElementAt(index).Key,
            paramQuery.Parameters().ElementAt(index).Value);

    /*
       paramQuery.Parameters().ElementAt(index).Key = "@param0"
       paramQuery.Parameters().ElementAt(index).Value = "tableTwo.ID"
    */

    MySqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read()) {
            // Do stuff
    }

}

One of the tables that I am try to join will retrieve everyone in tableTwo with the same ID for all. Is there something that I am doing wrong here?

1 Answer 1

1

try this:

  Select tableOne.ID, tableOne.Department, tableTwo.Name
  From tableOne join tableTwo
    on tableOne.ID=tableTwo.ID
  Where tableOne.ID = @param0

as you understand tableOne.ID=tableTwo.ID is the name of the columns which you joining tables on. At your current example I do not see how it can work. There is also Left join and right join which serves each to a different purposes. Here is some example to look how it works: http://dev.mysql.com/doc/refman/5.0/en/join.html

Sign up to request clarification or add additional context in comments.

3 Comments

Using the sql query in MySql has been successful but I will try your solution and report back.
Keep in mind that Join syntax also greatly depends on Version of MySQL you are using.Your example would be valid in MySQL 4.0 or earlier but in 5 and later they seems to decide to make it more SQL Server like.
I tried using my original statement in MySQL and it worked but following your advice, I used a proper join and it now works.

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.