Home Backend Development C#.Net Tutorial 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

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

Mar 13, 2017 pm 05:46 PM

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");
                        }
Copy after login


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);
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

See all articles