views:

176

answers:

2

I'm trying to execute a query that currently works in phpMyAdmin but it does not working when executing it in .NET using the MySqlAdapter. This is the Sql statement.

SELECT @rownum := @rownum +1 rownum, t . *
FROM (
  SELECT @rownum :=0
) r, (
  SELECT DISTINCT
    TYPE FROM `node`
  WHERE TYPE NOT IN ('ad', 'chatroom')
)t

It is using the @rownum to number each distinct row that is returned from my inner scalar query. But if I use it in .NET it's assuming that the @rownum is a parameter and throwing an exception because I didn't define it.

using (var sqlConnection = new MySqlConnection(SOURCE_CONNECTION))
{
    sqlConnection.Open();

    MySqlDataAdapter sqlAdapter = new MySqlDataAdapter(SqlStatement, sqlConnection);

    DataTable table = new DataTable();
    sqlAdapter.Fill(table);
    sqlConnection.Close();

    return table;
}

Any ideas for how I could get around this problem? Or possible ways for me to get a line number?

A: 

You could add a column to your table called rownum, then populate it with values:

table.Columns.Add("rownum");
for (int i=0; i < table.Rows.Count; i++)
{
table.Rows[i]["rownum"] = i;
}
Chook2330
I'll be honest I would shy away from manually generating the number this way but I could see this as a possible solution. I think really I need to rethink the way I'm pulling the data out.
Nathan Palmer
A: 

What version of the MySQL data provider are you using? You may need to update.

I found this in the 5.0 documentation:

"Prior versions of the provider used the '@' symbol to mark parameters in SQL. This is incompatible with MySQL user variables, so the provider now uses the '?' symbol to locate parameters in SQL. To support older code, you can set 'old syntax=yes' on your connection string. If you do this, please be aware that an exception will not be throw if you fail to define a parameter that you intended to use in your SQL."

From this page: http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlcommand.html

Kevin Thiart
Thanks for the update. I saw that in the documentation too. It's a pretty recent version since it was downloaded only a couple of weeks ago. I tried the old syntax=yes with no change in behavior.
Nathan Palmer