Quick questin for OLEDB and creating a new table. In one part of the program I have a code that creates new table in database and the name of table is what user inserts into textbox. For regular words it all works ok, but when I try to add ".", ",","-" or "/" the program breaks down and it can't create the table.
Is there any way to enable creating tables with "-" or "/" in the table name? That possibility is pretty important
connection string
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C_\\.....\\robnoknjigovodstvo1.mdb";
Table is beeing created after a button click
OleDbCommand cmd4 = new OleDbCommand();
cmd4.Connection = konekcija;
cmd4.CommandText = "CREATE TABLE " + opis + "(ID INT IDENTITY(1,1), nazivMaterijala VARCHAR(50), jedMjera VARCHAR(50), kolicina FLOAT)";
cmd4.ExecuteNonQuery();
Try to enclose the table name in square brakets
cmd4.CommandText = "CREATE TABLE [" + opis + "] (ID INT IDENTITY(1,1), ...."";
However keep in mind that Microsoft discourages the use of special characters in table names.
I think that it is better for you and for your code to prepare a method that searches these special characters and replace them with something more easy to handle in future query. (Like an underscore)
string ReplaceSpecialChars(string inputName)
{
char[] verbotten = new char[] { ' ','\'', '"','\'','@','`','#','%','>','<','!','.','[',']','*','$',';',':','?','^','{','}','+','-','=','~','\\' };
int pos = -1;
while((pos = inputName.IndexOfAny(verbotten)) != -1)
inputName = inputName.Substring(0, pos) + '_' + inputName.Substring(pos+1);
return inputName;
}