I have a XML file where each tag of the XML is a column in my table, that code looks like this:
try
{
SqlCommand makeCols = new SqlCommand
{
CommandText = "ALTER TABLE Devices ADD " + reader.Name + " nvarchar(100)",
CommandType = CommandType.Text,
Connection = conn
};
makeCols.ExecuteNonQuery();
}
catch (Exception)
{
break;
}
And then I have code that updates the SQL table with data at each column like so:
SqlCommand fillCols = new SqlCommand
{
CommandText = "UPDATE Devices SET "+ reader.Name + "=" + reader.Value + ", WHERE ID=1",
CommandType = CommandType.Text,
Connection = conn
};
fillCols.ExecuteNonQuery();
Where reader.Name is the XML tag, and reader.Value is the data.
I have data that will look like this: v2.14, - 0.00 kg/s, -99.00 Deg C, and so on. When I try to update the table I get the following error: Exception Occured while creating table:Incorrect syntax near 'v2'. System.Data.SqlClient.SqlException.
My question is, how can I store data in SQL table that will have symbols like . , - / \
Edit - To Clarify
Reader is just a loop that reads the XML file. reader.Name is the XML tag, so for example <port1> becomes port and is made a column. reader.Value is what is between <port1> and </port1> which needs to be stored in my database. reader.Value can have symbols such as: . , - / \ %. I just want to know how to store those symbols in the database.