0
我有一個很棒的excel文檔,所以我決定使用OleDbconneciton打開並編輯它。打開它時不是問題。我只是使用Select命令就像SQL一樣,但是當我嘗試更改某列的數據時,出現錯誤 - >條件表達式中的數據類型不匹配。 即更新串產生的一個樣本:我無法更新來自oledb的excel文件的數據C#
this._updateCommand = string.Format("UPDATE [{0}${1}{2}:{1}{2}] SET F{3} = ?", workSheet, ((DATA_COL)this._cellIndex).ToString(), this._rowIndex, "1");
關於出口它得到:
"UDPATE [worksheet$B1:B1] SET F1 = ?"
NEXTSTEP:
Task task = Task.Run(() => {String connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0; Mode=ReadWrite; Data Source={0}; Extended Properties='Excel 8.0; HDR=NO; IMEX=3;'", this.WorkBook);
for (int i = 0; i < this.editCollection.Count; i++)
{
OleDbConnection connection = new OleDbConnection(connectionString);
try
{
OleDbCommand command = new OleDbCommand(editCollection[i]._updateCommand, connection);
command.Parameters.AddWithValue("@thisData", editCollection[i]._value);
command.CommandType = CommandType.Text;
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
connection.Dispose();
}
}
editCollection.Clear();
});
所以,我做錯了什麼?
嘿Vadim,您的發佈代碼看起來沒問題。這個問題似乎是一些無效的DataType。爲了解決這個問題,很高興看到你的更新命令。您嘗試傳遞一個Excel無法使用的值,或者將其傳遞到具有已定義的其他格式的Excel-Cell中。 – Sebi
嘿Sebi。也許我使用了錯誤的連接字符串?根據角色,在連接字符串中使用IMEX參數允許將任何類型的數據表示爲字符串數據類型... –