


My environment when the error occurred: Windows 7, Framework 4, 0, Microsoft Office 2007, VS2010, c# WinForm;
Part of the code:
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"); }
The first thing that comes to mind when encountering this error is that there is no permission, but running as an administrator still gives the same error !
Added permissions through the following code, still the same error:
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);
Knowledge tutorial, there are too many connections Strings: Extended Properties='Excel 12.0; HDR =yes; IMEX=2'
Value of parameter HDR:
HDR= Yes, this means that the first row is the title and is not used as data. If HDR=NO is used, it means that the first row is not the title and is used as data. The system default is YES
Parameter Excel 8.0 For Excel 97 and above to 2003, use Excel 8.0, and for 2007 or 2010, use Extended Properties=Excel 12.0
IMEX (IMport EXport mode) settings
IMEX has three modes:
0 is Export mode
1 is Import mode
2 is Linked mode (full update capabilities)
I What needs special explanation here is the IMEX parameter, because different modes represent different reading and writing behaviors:
When IMEX=0, it is "export mode". The Excel file opened in this mode only Can be used for "writing" purposes.
When IMEX=1, it is "import mode". The Excel file opened in this mode can only be used for "reading" purposes.
When IMEX=2, it is "link mode". The Excel file opened in this mode can support both "reading" and "writing" purposes.
The meaning is as follows:
0 ---Output mode;
1---Input mode;
2----Link mode (full update capability)
According to the above description, the above connection string should be readable and recorded by the plug-in
But this is not the case. When executing the Insert Into statement, an exception occurs: "The operation must use an updatable query"!
Note that it is a c# WinForm program, not a Web application; if it is a Web application, you need to add the directory access permissions of the IIS_IUSRS or IIS_Service user;
It’s better to search and see how others solved it, but I’ve read it all The method to solve the problem failed to pass the test when it came to me!
I guess it’s still a problem with the IMEX value. If changing it to 1 doesn’t work, then change it to 0. Damn it, a miracle happened!
Then I tested setting IMEX to 4 or 10, and the results were all fine, except for 1 and 2. It was really a cheating rhythm.
The above is the detailed content of C# uses oledb to connect to excel to execute the Insert Into statement. The sample code of the solution 'must use an updateable query' appears. For more information, please follow other related articles on the PHP Chinese website!

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.