如何在C#中通过OLEDBAdapter编写正确的SQL

时间:2013-12-02 09:39:02

标签: sql excel c#-4.0 oledb

我想通过声明

更新Excel文件
UPDATE [Sheet1$] 
SET Status ='Imported' 
WHERE ID IN ( SELECT TOP 5 ID 
              FROM [Sheet1$] 
              WHERE Status <>'Imported' OR Status IS NULL )

但Excel中没有任何改变。这是我的代码

OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", myConnection);
DataTable dt = new DataTable();

adapter.Fill(dt);

dt.AsEnumerable().Take(5).ToList().ForEach(o => o.SetField("Status", @"Imported"));
dt.AcceptChanges();

string updateSQL = string.Format(@"UPDATE [Sheet1$] SET Status =? WHERE ID IN ( SELECT TOP 5 ID FROM [Sheet1$] WHERE Status <>? OR Status IS NULL )");

adapter.UpdateCommand = new OleDbCommand(updateSQL, myConnection);
adapter.UpdateCommand.Parameters.Add("@Status", OleDbType.Char, 255).SourceColumn = "Status";
adapter.UpdateCommand.Parameters.Add("@OldStatus", OleDbType.Char, 255, "Status").SourceVersion = DataRowVersion.Original;
adapter.Update(dt);
是谁能提供帮助?谢谢:)

0 个答案:

没有答案