标签:c# excel imex oledb 操作必须使用一个可更新的查询
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;Persist Security Info=False;" + "data source=" + @excelPath + ";Extended Properties=‘Excel 12.0; HDR=yes; IMEX=2‘"; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = strConn; try { OleDbCommand cmd = null; try { cmd = new OleDbCommand("Insert Into [Sheet1$] Values(‘abc‘, ‘bac‘, ‘0‘, ‘123456‘, ‘test‘,‘测试‘,‘aa‘)", conn);//(A,B,C,D,E,F,G) cmd.ExecuteNonQuery(); } catch (System.Exception ex) { textBox1.Text += ("插入数据失败:" + ex.Message); textBox1.Text += ("\r\n"); }
FileInfo fi = new FileInfo(excelPath); System.Security.AccessControl.FileSecurity fileSecurity = fi.GetAccessControl(); fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow)); fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow)); fi.SetAccessControl(fileSecurity); DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(excelPath)); System.Security.AccessControl.DirectorySecurity dirSecurity = di.GetAccessControl(); dirSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow)); dirSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow)); di.SetAccessControl(dirSecurity);知识补习,这里的连接字符串多了:Extended Properties=‘Excel 12.0; HDR=yes; IMEX=2‘
参数HDR的值:
C#使用oledb连接excel执行Insert Into语句出现“操作必须使用一个可更新的查询”的解决办法,布布扣,bubuko.com
C#使用oledb连接excel执行Insert Into语句出现“操作必须使用一个可更新的查询”的解决办法
标签:c# excel imex oledb 操作必须使用一个可更新的查询
原文地址:http://blog.csdn.net/testcs_dn/article/details/25500973