0

I am a novice in programming and I hope somebody can help me. I want to store data which I received in C# to MySql: The table should be variable according to the data I received.

string myInsertQuery = String.Format("INSERT INTO volumen (date, time, bidvol, bidprice, askprice, askvol, lastprice, volume) VALUES({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7})", now_date_marketlast, now_time_marketlast, bidvol, bidprice, askprice, askvol, ePrice1, e.Volume);

works, but how to define the table variable. If I replace volumen with {0} and , table doesen´t work. Please help me. Thank you in advance.

1
  • Your code has a serious security risk known as SQL injection. I suggest you read up on Parameterized Queries (they will not solve every SQL security issue, but are easy and go a long way toward safer code). Commented Oct 24, 2011 at 19:00

3 Answers 3

1

First off, you probably don't want to do what you're attempting because it opens your code to SQL injection attacks. There are ways, using the SqlCommand object, to dynamically create SQL statements that are not open to SQL injection.

That being said, when you replace volumen with {0}, unless you are adding the table name to the beginning of your list of values, then now_date_marketlast would be inserted as your table name. The proper way to write this would be:

string myInsertQuery = String.Format("INSERT INTO {0} (date, time, bidvol, bidprice, askprice, askvol, lastprice, volume) VALUES({1}, {2}, {3}, {4}, {5}, {6}, {7}, {8})", "volumen", now_date_marketlast, now_time_marketlast, bidvol, bidprice, askprice, askvol, ePrice1, e.Volume);

You're still going to have to supply the table name, you're just doing it at a different point in the code. Again, not a good idea due to SQL injection.

If you're looking for some way for MySQL to determine what table you're using according to what data you're passing in, then you're out of luck as database engines don't tend to work this way.

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

1 Comment

you have an error with your statement. you are assigning wrong value to {0}
0

try this:

var tableName = "volumen";

string myInsertQuery = String.Format("INSERT INTO {8} (date, time, bidvol, bidprice, askprice, askvol, lastprice, volume) VALUES({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7})", now_date_marketlast, now_time_marketlast, bidvol, bidprice, askprice, askvol, ePrice1, e.Volume, tableName);

1 Comment

That works but if string table; if ((Instrument.FullName) == "$EURUSD") table = "eurusd"; var tableName = table; there occur "not assigned local variable table". What can I do?
0

If you want the table name to be assigned. you can do this:

string myInsertQuery = String.Format("INSERT INTO {8} (date, time, bidvol, bidprice, askprice, askvol, lastprice, volume) VALUES({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7})", now_date_marketlast, now_time_marketlast, bidvol, bidprice, askprice, askvol, ePrice1, e.Volume, volumen);

Volumen is your table name

Comments

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.