tags:

views:

52

answers:

1

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.

+1  A: 

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.:

using (OleDbCommand addLine = new OleDbCommand("INSERT INTO Data1 values (@name, @age)"))
{
    addLine.Parameters.AddWithValue("@name", row["Name"]);
    addLine.Parameters.AddWithValue("@age", row["Age"]);
    int affectedRows = addLine.ExecuteNonQuery();
    Debug.Assert(1 == affectedRows);
}

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:

namespace Demos.StackOverflow.ExcelUpdate
{
    using System;
    using System.Collections.ObjectModel;
    using System.Data.OleDb;

    class Program
    {
        static void Main(string[] args)
        {
            // -> Change the following name of the file to use.
            string fullPath = @"C:\Users\Arsene\Desktop\Book1.xlsx";
            string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0'", fullPath);

            Collection<Tuple<string, int>> dataToPut = new Collection<Tuple<string, int>>();

            dataToPut.Add(new Tuple<string, int>("Some name", 34));
            dataToPut.Add(new Tuple<string, int>("Other name here", 41));
            dataToPut.Add(new Tuple<string, int>("Last one", 36));

            using (OleDbConnection oleConnection = new OleDbConnection(connectionString))
            {
                oleConnection.Open();

                using (OleDbCommand createTable = new OleDbCommand("create table Data1 (Name char(10), Age char(10))", oleConnection))
                {
                    createTable.ExecuteNonQuery();
                }

                foreach (var t in dataToPut)
                {
                    using (OleDbCommand addItem = new OleDbCommand("insert into Data1 values (@name, @age)", oleConnection))
                    {
                        addItem.Parameters.AddWithValue("@name", t.Item1);
                        addItem.Parameters.AddWithValue("@age", t.Item2);
                        addItem.ExecuteNonQuery();
                    }
                }
            }
        }
    }
}
MainMa
I checked through debugging row["Name"] it contains the value only, without any quote but on inserting it shows signle quote in value in that excel
Nits
@Nits: I know. In all cases, you tried the suggestion of @Treb in the third comment to the question, and it didn't work, when it should. Well, try with parameters and see if it works.
MainMa
even though fields name also have single quote prefixed. Like 'Name and 'Age
Nits
it is still givign me the same single quote pre to each value, it also didn't work
Nits
@Nits: see the last edit to my answer.
MainMa
Although problem with the single quote is not solved but when I tried to Import the excel sheet in oracle it didn't give any error and took those values without that single quote.
Nits
@Nits: well, if the source code I posted in my edit still produces single quote problem for you, there is probably something wrong with your Microsoft Office installation. Try on other PCs, then try to reinstall Microsoft Office to see if it works. You can also report the bug to Microsoft Connect.
MainMa
I'll give a try on another PC also and will post a update in the same concern but if the values are having single quote pre to each value then why while importing it is taking the correct value automatically? this is a question in my head. Also is the Excel error or it is oracle's, i.e also not clear to me.
Nits