// 01 excel文档的绝对物理路径
            string xlsPath = @"E:\Test11.xlsx";
            string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=\"Excel  8.0;HDR=NO;IMEX=2\";" + "data source=" + xlsPath;
            // 02 命令
            OleDbConnection conn = new OleDbConnection(connStr);            string sql = "select * from [Test$]";
            // 调用AADO.NET获取excel的sheet1中的数据
            DataTable dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter();
            da.SelectCommand = new OleDbCommand(sql, conn);            da.Fill(dt);            OleDbCommandBuilder cb = new OleDbCommandBuilder(da);            DataRow[] rows = dt.Select("F1='Suegy'");
            if (rows.Length > 0)
            {
                rows[0][1] = "23";
            }            int i = da.Update(dt);
            dt.AcceptChanges();
            Console.WriteLine(i);
            Console.ReadLine();
在int i=da.Update(dt);这一行报错,错误为:Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.请问这是什么原因?Office版本是2010,顺便请教一下,为什么一定要打开Excel文件才能执行?

解决方案 »

  1.   

    Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.
    不支持SelectCommand你的这个又不是数据库,能支持SQL 语言吗?
    我没操作过EXCEL ,建议你还是换种方式
      

  2.   

    int i = da.Update(dt);
    用这种方法更具数据库,必须是有主键的表才能更新,Excel里面应该没有主键的。