I'm working on a program that allows the user to select an MS Access table and then will export it to a .csv file in a particular destination. The problem is I'm getting an error when the selected table contains sql special characters.
I have done some research on the topic and I understand that I need to use parameters but I can't figure out how to actually do so. Here's my code sample:
string destination = Form2.destination;
string selectString = "SELECT * FROM " + tablename;
string path = destination + "\\" + tablename + ".csv";
File.Create(path).Dispose();
TextWriter tw = new StreamWriter(path);
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtDataPath.Text);
connection.Open();
DataSet myDataSet = new DataSet();
OleDbCommand command = new OleDbCommand(selectString,connection);
OleDbDataAdapter adapter = new OleDbDataAdapter(command.CommandText,connection);
adapter.Fill(myDataSet, tablename);
So far what happens is if a table name that does not have special characters is selected, the csv file is created and looks good, but if it contains something like a '-' then my program produces a "Syntax error in FROM clause" which is because of the special character.
None of the examples I've seen so far work with what I'm trying to do specifically I guess so hopefully someone can help me out. Thanks.