tailieunhanh - Connecting to a Microsoft Excel Workbook

[ Team LiB ] Recipe Connecting to a Microsoft Excel Workbook Problem You want to access data stored in a Microsoft Excel workbook. Solution Use the OLE DB Jet provider to create, access, and modify data stored in an Excel workbook. The sample code contains two event handlers: | Team LiB Recipe Connecting to a Microsoft Excel Workbook Problem You want to access data stored in a Microsoft Excel workbook. Solution Use the OLE DB Jet provider to create access and modify data stored in an Excel workbook. The sample code contains two event handlers Creates an OleDbDataAdapter that uses the Jet OLE DB provider to access an Excel workbook. Custom insert and update logic is created for the DataAdapter. A DataTable is filled from the first worksheet Sheetl in the Excel workbook and the default view of the table is bound to a data grid on the form. Update Uses the DataAdapter created in the event handler to update the Excel workbook with the programmatic changes. The C code is shown in Example 1-2. Example 1-2. File Namespaces Variables and Constants using System using using private OleDbDataAdapter da private DataTable dt . . . private void ExcelForm_Load object sender e Create the DataAdapter. da new OleDbDataAdapter SELECT FROM Sheet1 Excel_0115_ConnectString Create the insert command. String insertSql INSERT INTO Sheet1 CategorylD CategoryName Description VALUES new OleDbCommand insertSql @CategoryID 0 CategorylD @CategoryName 15 CategoryName @Description 100 Description Create the update command. String updateSql UPDATE Sheet1 SET CategoryName Description WHERE CategoryID new OleDbCommand updateSql @CategoryName 15 CategoryName @Description 100 Description @CategoryID 0 CategoryID Fill the table from the Excel spreadsheet. dt new .