Mentioned
In
|
 |
 |
 |
 |
Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms
of Use |
Trademarks
Article ID: 316756 - Last Review: January 9, 2007 - Revision: 7.2 You receive error messages when you try to use ADO.NET OLEDbDataAdapter to modify an Excel workbookThis article was previously published under Q316756 On This PageSYMPTOMS When you try to add, to update, or to delete records in an
Excel workbook that uses the OLEDbDataAdapter with ADO.NET, you receive one of the following error messages:
When you add a record: Syntax error in INSERT
INTO statement. When you update a record:
Dynamic SQL generation for the UpdateCommand is not supported against a
SelectCommand that does not return any key column information. When
you delete a record: Dynamic SQL generation for the
DeleteCommand is not supported against a SelectCommand that does not return any
key column information. CAUSE The Microsoft Jet OLE DB provider does not return key or
index information for Excel workbooks. Therefore, the OLEDbCommandBuilder cannot automatically generate SQL statements to perform the
intended action. RESOLUTION To resolve this problem when you insert new records by
using the OLEDbDataAdapter, you must provide primary key information through the InsertCommand member of the adapter. To modify existing records, you must
provide primary key information through the UpdateCommand member. The Jet OLE DB provider does not support delete
operations for Excel workbooks. Therefore, you cannot delete records in a
workbook by using ADO or ADO.NET. STATUSThis
behavior is by design. MORE INFORMATIONSteps to Reproduce the Behavior- Create a workbook for this test. To do this, follow these
steps:
- Open a new workbook in Excel.
- In cell Sheet1!A1, type ID, and
then in cell Sheet1!B1, type Name.
- In cell Sheet1!A2, type AAA, and
then in cell Sheet1!B2, type John.
- In Excel 2000 and in Excel 2002, save the workbook as C:\Test.xls, and then quit
Excel. In Excel 2007, save the workbook as C:\Test.xlsx, and then quit Excel.
- In Visual Basic .NET, open a new Windows Application
project. Form1 is created by default.
- Add two Button controls to Form1.
- Change the Text property of the first button to Add Record, and then change the Text property of the second button to Update Record.
- On the View menu, click Code.
- Add the following code to the beginning of the code module:
Imports System.Data.OleDb
- In Excel 2000 and in Excel 2003, add the following code example to the Form1 class.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
ModifyXLData(0) 'Add record.
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles Button2.Click
ModifyXLData(1) 'Update record.
End Sub
Private Sub ModifyXLData(ByVal nAction As Int16)
Dim sConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Test.xls;" & _
"Extended Properties=""Excel 8.0;HDR=YES"""
' Create an instance of a DataAdapter.
Dim da As New OleDbDataAdapter("Select * From [Sheet1$]", sConn)
Dim cb As New OleDbCommandBuilder(da)
' Create an instance of a DataSet from Sheet1 data.
Dim ds As New DataSet("Data")
da.FillSchema(ds, SchemaType.Source, "[Sheet1$]")
da.Fill(ds, "[Sheet1$]")
'Add, modify, or delete a row.
Dim dr As DataRow
Select Case nAction
Case 0 'Add a new row
Dim rowVals(1) As Object
rowVals(0) = "hello"
rowVals(1) = "world"
ds.Tables(0).Rows.Add(rowVals)
Case 1 'Modify an existing row
dr = ds.Tables(0).Rows(0)
dr.BeginEdit()
dr(0) = "hi"
dr.EndEdit()
End Select
'Attempt the update.
Try
da.Update(ds, "[Sheet1$]")
Catch ex As OleDbException
Dim er As OleDbError
For Each er In ex.Errors
MsgBox(er.Message)
Next
Catch ex2 As System.InvalidOperationException
MsgBox(ex2.Message)
End Try
End Sub
In Excel 2007, add the following code example to the Form1 class.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
ModifyXLData(0) 'Add record.
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles Button2.Click
ModifyXLData(1) 'Update record.
End Sub
Private Sub ModifyXLData(ByVal nAction As Int16)
Dim sConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
'"Data Source=C:\Test.xls;" & _
'"Extended Properties=""Excel 12.0;HDR=YES"""
' Create an instance of a DataAdapter.
Dim da As New OleDbDataAdapter("Select * From [Sheet1$]", sConn)
Dim cb As New OleDbCommandBuilder(da)
' Create an instance of a DataSet from Sheet1 data.
Dim ds As New DataSet("Data")
da.FillSchema(ds, SchemaType.Source, "[Sheet1$]")
da.Fill(ds, "[Sheet1$]")
'Add, modify, or delete a row.
Dim dr As DataRow
Select Case nAction
Case 0 'Add a new row.
Dim rowVals(1) As Object
rowVals(0) = "hello"
rowVals(1) = "world"
ds.Tables(0).Rows.Add(rowVals)
Case 1 'Modify an existing row
dr = ds.Tables(0).Rows(0)
dr.BeginEdit()
dr(0) = "hi"
dr.EndEdit()
End Select
'Attempt the update.
Try
da.Update(ds, "[Sheet1$]")
Catch ex As OleDbException
Dim er As OleDbError
For Each er In ex.Errors
MsgBox(er.Message)
Next
Catch ex2 As System.InvalidOperationException
MsgBox(ex2.Message)
End Try
End Sub
- Press F5 to build and run the sample. Form1 is
loaded.
- On the form, click Add Record. Note that you receive an error.
- On the form, click Update Record. Note that you receive an error.
REFERENCES For additional information, see the following Microsoft
Developer Network (MSDN) Web site:
For additional informationabout using InsertCommand or UpdateCommand to update Excel workbooks, click the following article number to view the article in the Microsoft Knowledge Base:
316934Â
(http://kbalertz.com/Feedback.aspx?kbNumber=316934/
)
HOW TO: Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET
APPLIES TO- Microsoft ADO.NET 2.0
- Microsoft ADO.NET 1.0
- Microsoft Office Excel 2007
- Microsoft Office Excel 2003
- Microsoft Excel 2002 Standard Edition
- Microsoft Excel 2000 Standard Edition
| kbtshoot kberrmsg kbprb KB316756 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please
|
 |
 |
 |
 |
 |
 |
 |
| |