0

I have an application that run a lot of query using OleDbCommand. Every query is plain SQL text, and the command does not use parameters.

Now, the application should support chinese char and I do not wanto to change every query by adding the N char in front of the string to specify.

I am trying to do something like that, but It does not work:

        OleDbCommand command = new OleDbCommand("?", connect);
        command.CommandType = CommandType.Text;
        command.Parameters.AddWithValue("query", qry);
        OleDbDataAdapter da = new OleDbDataAdapter(command);

where qry is the query to execute without the N char in front of every string.

I really do not wat to change every query, it would be a mess.

Do you have a solution for me?

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Angelo Badellino
  • 2,141
  • 2
  • 24
  • 45

1 Answers1

1

You will want to set the data type of the parameter, either using one of the Add overloads or creating a parameter using new, setting the property and then adding it to the collection.

Here are the OleDb datatypes:

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbtype.aspx

VarWChar is unicode

If you are using SqlServer, then you might want to use the native driver so you can get the Sql data types, which have a more accurate matching.

If you are going for cross db compatiblity, then you might go in the opposite direction and use the System.Data.Common datatypes, DbType.XXX, ref http://msdn.microsoft.com/en-us/library/system.data.dbtype.aspx (or ODBC and it's type enumeration since MS said they are phasing out OleDb as a technology in favor of ODBC)

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
  • I do not use parameter in my query. – Angelo Badellino Apr 24 '13 at 12:27
  • Then you may have posted the wrong source code, because this adds a parameter: command.Parameters.AddWithValue("query", qry); Which is named query and has a value of qry. I don't see where you set the commandText though. – MatthewMartin Apr 24 '13 at 14:58
  • The code I have posted is just a try. I have only literal sql to execute without parameter. I have to execute the command "as is" and I have not the possibility to set parameter to solve Unicode issue. So i am trying to treat the command text as a parameter but it does not work. – Angelo Badellino Apr 24 '13 at 15:45