I am trying to read one excel sheet and then creating another excel sheet through OleDb
In this another excel sheet, I made some colums like Name char(10), Age Number
now when I reading that excel sheet and trying to insert the values in this another excel sheet then values are inserting but also contains one extra char i.e single quote(‘) as the first char.
Why all the values are instered with single quote pre to actual value? How to get rid of it? I am using c#.net for this.
@Edit
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
cmd.CommandText = "CREATE TABLE Data1 (Name char(10), Age char(10)) ";
cmd.ExecuteNonQuery();
foreach (DataRow row in someDataTable.Rows)
{
cmd.CommandText = "INSERT INTO Data1 values ('" + row["Name"] +
"','" + row["Age"] + "')";
cmd.ExecuteNonQuery();
}
I have exactlly the same data in someDataTable as in excel which I need to copy.
It would be strange, but maybe it is some sort of attempt of OleDb to avoid SQL Injection? (What if
row["Name"]contains a single quote?) Even if it does not explain, like @Treb said, why there is a leading quote only, without a trailing one.What happens if you try to use the query with parameters, ie.:
After a few attempts, I still can’t reproduce the observed behavior. I suggest to do several things:
Post in your question the whole source code which produces the single quote problem,
Try the following source (Console application) and see what happens. In my case, values are inserted well, without any leading single quotes. The only problem are the ages, inserted as text instead of numbers (which can probably be solved by explicitly specifying the type of the row):
Source code: