0

Is there a way to use the @ character in a MySQL query from a C# .net program without using parametrised queries?

So for example in the following program

String s = 
@"LOAD DATA LOCAL INFILE C:\foo.txt'  
INTO TABLE Bar
(idFoo, @baa)
SET  
baa = nullif(@baa,'')
";
MySqlCommand myCommand = new MySqlCommand(s, myConnection);
MySqlDataReader myReader = myCommand.ExecuteReader();

@baa is treated like a parameter. ie: as one would use when calling

myCommand.Parameters.Add("@baa", SqlDbType.VarChar );

However in this case I do not want @baa to be a parameter. How do I write this?

6
  • just dont add the parameter, it will be treated as normal text! Commented Apr 26, 2013 at 1:36
  • I think last time I didn't provide the parameter I got an error.. Commented Apr 26, 2013 at 1:38
  • try @@ or \@ one of them must escape it. i do not have mysql installed that i can test. Commented Apr 26, 2013 at 1:39
  • Just figured it out now, I needed to add "allow user variables" to the connection string Commented Apr 26, 2013 at 1:48
  • although, as I have framed my question, you have given the correct answer Commented Apr 26, 2013 at 1:50

1 Answer 1

1

MySQL uses the back-tick character to quote tokens with spaces or other special characters. This is used for example to handle field names with spaces, etc.

So in your code, where you want @baa to refer to a field name, place back-tick characters around the name like so: `@baa`

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

4 Comments

already tried that, I needed to add allow user variables to the connection string
That wasn't clear from the question. What exactly are you trying to achieve?
Loading the data into a table, was originally doing this with insert statements except it took too long (about an hour). It only takes a few seconds now.
@sav Have you tried the SqlBulkCopy class? I've used it to insert thousands of records in well under a second. Check out this blog: adathedev.co.uk/2010/02/…

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.