Search code examples
c#excelconnection-stringoledb

Why doesn't the update OLEDB query do nothing?


I'm using OLEDB in my program to update an excel sheet. The Execute returns 1 as row update count, but doesn't change nothing. My code is as below:

public static void updateExcel(string sql, string path)
    {
        try
        {
            OleDbConnection con;
            OleDbCommand comm = new OleDbCommand();

            con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                                            path +
                                            ";Extended Properties=Excel 12.0;");
            con.Open();
            comm.Connection = con;
            comm.CommandText = sql;
            int y = comm.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception ex)
        {


        } 

    }

And the calling is:

string sql = "update [Sh1$] set j = j + ' AAAA ' where a = '" + excelData.Rows[i]["a"].ToString() + "'";
EXCEL.updateExcel(sql, excelFile);

Does someone know what is the problem?

Thanks!


Solution

  • Check if the cells in the excel are empty. If so, this: set j = j + ' AAAA ' won't work, because j is NULL.

    In this case, this will be better:

    set j = ' AAAA '

    or

    set j = IIF(j IS NULL, ' AAAA ', j + ' AAAA ')