0% found this document useful (0 votes)
155 views11 pages

Working With Offline SQL Server Data in Excel

The document provides instructions for a walkthrough that loads data from a SQL Server database into an Excel workbook using ADO.NET and Visual Studio .NET. The walkthrough demonstrates importing data from SQL Server into a DataSet, loading that data into an Excel worksheet, making changes to the data in Excel, and synchronizing those changes back to the SQL Server database. The document outlines setting up the project, connecting to the database, loading and formatting the data, trapping changes in Excel and updating the database.

Uploaded by

Bachtiar Yanuari
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
155 views11 pages

Working With Offline SQL Server Data in Excel

The document provides instructions for a walkthrough that loads data from a SQL Server database into an Excel workbook using ADO.NET and Visual Studio .NET. The walkthrough demonstrates importing data from SQL Server into a DataSet, loading that data into an Excel worksheet, making changes to the data in Excel, and synchronizing those changes back to the SQL Server database. The document outlines setting up the project, connecting to the database, loading and formatting the data, trapping changes in Excel and updating the database.

Uploaded by

Bachtiar Yanuari
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Walkthrough: Working with Offline SQL Server Data in Excel

Brian A. Randell MCW Technologies, LLC Applies to: Microsoft Visual Studio Tools for the Microsoft Office System Summary: Excel makes it easy to work rectangular data such as data from an external database. In this walkthrough, youll load data from SQL Server into an ADO.NET Dataset. Youll then use Excels object model to support synchronizing changes made to the data in Excel with SQL Server. (11 printed pages)

Contents
Getting Started Hook Up the Event Handlers Add Code to Import Data Synchronize Changes with the Server Run Code Before Closing the Workbook

Introduction
Youll download data from the SQL Server Northwind sample database into a local DataSet object, releasing your connection to the server. Youll then load the data from the dataset into a sheet within an Excel workbook as shown in Figure 1. Once the data is loaded, youll make some changes and send the results back to SQL Server. Once youre done, the example will display a message box informing you of the number of records updated.

Figure 1. Excel with data loaded from SQL Server

Prerequisites
To follow this walkthrough, the following software and components must be installed on the development computer: Microsoft Visual Studio .NET 2003 or Microsoft Visual Basic .NET Standard 2003 Microsoft Visual Studio Tools for the Microsoft Office System Microsoft Office Professional 2003 SQL Server or MSDE (7.0 or 2000), with the Northwind sample database installed. This demonstration assumes that youve set up SQL Server/MSDE allowing access using integrated security.
Although its not required, this demonstration assumes that youve set the Option Strict setting in your project to On (or have added the Option Strict statement to each module in your project.) Setting the Option Strict setting to On requires a bit more code, as youll see, but it also ensures that you dont perform any unsafe type conversions. You can get by without it, but in the long run, the discipline required by taking advantage of this option will far outweigh the difficulties it adds as you write code.

TIP:

Getting Started
In order to get started, youll need to begin by creating a new Visual Studio .NET project that works with Excel.

Create the Project


1. Start Visual Studio .NET, and select File, New, Project. 2. In the Project Types pane, expand Microsoft Office 2003 Projects, and then select Visual Basic Projects. 3. In the Templates pane, select Excel Workbook. 4. Name the project ExcelSQLServerData, and store it in a convenient local path. 5. Accept the defaults on the Office Project Wizard dialog box, and click Finish to create the project. Visual Studio .NET opens the ThisWorkbook.vb file in the code editor for you.

Lay Out the Spreadsheet


In order to load the data and synchronize changes, youll need to add some way to start the code running. For this demonstration, youll create two hyperlinks within the workbook, and react to the SheetFollowHyperlink event of the Workbook object to run your code. 1. Press F5 to run the project, loading Excel and your new workbook. 2. Rename Sheet1 to Products. If youd like, remove Sheet2 and Sheet3. 3. Within Excel, put the cursor in cell G1, and select Insert, Hyperlink. 4. In the Insert Hyperlink dialog box, in the Link to: pane, on the left side of the dialog box, select Place in This Document. 5. Set the Text to display value to Load Data. 6. Make sure the cell reference in the dialog box matches the location of your hyperlink. When youre done, the dialog box should look like Figure 2. Click OK to dismiss the dialog boxyou should see the new hyperlink within the workbook.

Figure 2. The finished Insert HyperLink dialog box

7. Repeat the process, putting the cursor in cell G2, and put Update Data in the Text to display field. 8. Select File, Save to save your changes. The workbook should look something like Figure 3.

Figure 3. Your Excel workbook ready to go.

Hook Up the Event Handlers


In order to start your code running, youll need to react to the Workbook.SheetFollowHyperlink event. In this section, youll add support for reacting to this event. Follow these steps to hook up the event handler:

1. From the Class Name dropdown list in the upper-left corner of the code editor window, select ThisWorkbook. 2. From the Method Name dropdown list in the upper-right corner of the code editor window, select SheetFollowHyperlink. Visual Studio .NET creates the event handler stub for you. 3. Add the following procedure stubs to the current class. Youll fill in the details later:
Private Sub LoadData() End Sub Private Sub UpdateData() End Sub

4. Modify the ThisWorkbook_SheetFollowHyperlink procedure, adding the following code:


Try Select Case Target.Name Case "Load Data" LoadData() Case "Update Data" UpdateData() End Select Catch ex As Exception MessageBox.Show(ex.Message, ex.Source, _ MessageBoxButtons.OK, MessageBoxIcon.Error) End Try

Add Code to Import Data


Next, youll need to add the code that creates a DataSet, loading the data from SQL Server, and add code to copy that data into the Products sheet within the Excel workbook. 1. Scroll to the top of the code module, and add the following statement, which will reduce the amount of typing required to refer to the objects and members youll reference:
Imports System.Data.SqlClient

2. Add the following declarations, immediately beneath the existing declarations for the ThisApplication and ThisWorkbook variables:
Private DisableWorkSheetChanges As Boolean = False Private mda As SqlDataAdapter

Private mds As DataSet Private mdt As System.Data.DataTable Private xlSheet As Excel.Worksheet Private rngUC As Excel.Range Private rngData As Excel.Range

WARNING!

Excel provides a DataTable object, as does ADO.NET. Its important to distinguish between the two, and the code uses an explicit namespace reference to avoid the ambiguity.

3. Add the following procedure to the OfficeCodeBehind class. This procedure connects to SQL Server on the local computer, using integrated security, and fills a DataSet:
Private Sub GetDataSet() Dim cnn As SqlConnection Try If mds Is Nothing Then mds = New DataSet Else mds.Tables.Remove(mdt) End If cnn = New SqlConnection( _ "Server='.';" & _ "Database=Northwind;" & _ "Integrated Security=true") Dim strSQL As String = "SELECT " & _ "ProductId AS [Id], ProductName AS [Name], " & _ "UnitsInStock AS [On Hand], " & _ "UnitsOnOrder AS [On Order], " & _ "ReorderLevel AS [Reorder Level] " & _ "FROM Products WHERE Discontinued = 0 " & _ "ORDER BY UnitsInStock" Dim cmd As New SqlCommand(strSQL, cnn) mda = New SqlDataAdapter(cmd) mda.Fill(mds) mdt = mds.Tables(0)

Dim cb As New SqlCommandBuilder(mda) mda.UpdateCommand = cb.GetUpdateCommand() Catch ex As Exception MessageBox.Show(ex.Message, ex.Source, _ MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub

4. Add the following procedure to the class. This procedure creates the column headings in the worksheet using the field names from the Columns collection of the DataTable contained within the new DataSet, and provides basic formatting:
Private Sub SetupWorksheet() Try DisableWorkSheetChanges = True xlSheet = DirectCast( _ ThisWorkbook.Worksheets("Products"), Excel.Worksheet) rngUC = DirectCast(xlSheet.Cells(2, 1), Excel.Range) rngUC.CurrentRegion.Clear() Dim i As Integer = 0 Dim col As DataColumn Dim rng As Excel.Range For Each col In mdt.Columns I += 1 rng = DirectCast(xlSheet.Cells(1, i), Excel.Range) rng.Value = col.ColumnName rng.Font.Bold = True Next Finally DisableWorkSheetChanges = False End Try End Sub

5. Add the following procedure, which takes the data from the DataTable in the previously loaded DataSet and puts it into the Products worksheet:
Private Sub PutDataInXL() DisableWorkSheetChanges = True Dim i As Integer = 0 Dim j As Integer = 0 Dim dr As DataRow

Try ThisApplication.ScreenUpdating = False If Not mdt Is Nothing Then For i = 0 To mdt.Rows.Count - 1 dr = mdt.Rows(i) For j = 0 To mdt.Columns.Count - 1 rngUC.Offset(i, j).Value = dr(j).ToString() Next j Next rngData = rngUC.CurrentRegion End If Finally ThisApplication.ScreenUpdating = True DisableWorkSheetChanges = False End Try End Sub

6. Add the following procedure, which applies some formatting to the newly loaded data in Excel:
Private Sub FormatColumns() Try DisableWorkSheetChanges = True rngData.Columns.NumberFormat = "0" rngData.Columns.AutoFit() Finally DisableWorkSheetChanges = False End Try End Sub

7. Add the following code to the LoadData procedure created earlier:


GetDataSet() SetupWorksheet() PutDataInXL() FormatColumns()

8. Select File, Save All to save the entire solution. 9. Press F5 to run the project, loading Excel and your workbook. 10. Within Excel, click the Load Data link you added previously, and verify that your code has imported and formatted the data, as shown in Figure 1.

11. Close the Excel and the workbook, saving changes if you desire, and return to Visual Studio .NET.

Synchronize Changes with the Server


You need to add some code to trap the Workbooks SheetChange event and put any changes made to the loaded data into the correct columns in the DataTable objects Rows collection. Then, youll add code to actually send the changes to SQL Server when you click the Update Data hyperlink. 1. From the Class Name dropdown list in the upper-left corner of the code editor window, select ThisWorkbook. 2. From the Method Name dropdown list in the upper-right corner of the code editor window, select ThisWorkbook_SheetChange. Visual Studio .NET creates the event handler stub for you. 3. Modify the ThisWorkbook_SheetChange procedure, so that it looks like the following:
Private Sub ThisWorkbook_SheetChange( _ ByVal Sh As Object, ByVal Target As Excel.Range) _ Handles ThisWorkbook.SheetChange If Not DisableWorkSheetChanges Then If Not ThisApplication. _ Intersect(rngData, Target) Is Nothing Then ' Subtract 1 because Excel is 1-based, ' then subtract one row for the header. ' Don't handle changes to the ' header row, however. If Target.Row > 1 Then Dim intRow As Integer = Target.Row - 2 Dim intCol As Integer = Target.Column - 1 mdt.Rows(intRow)(intCol) = Target.Value End If End If End If End Sub

4. Add the following code to the existing UpdateData procedure stub, sending the DataTables modified rows to SQL Server:
Try If mds.HasChanges Then Dim intRV As Integer = mda.Update(mdt) If intRV > 0 Then

MessageBox.Show(String.Format( _ "{0} records were updated successfully.", _ intRV.ToString()), _ "Success", MessageBoxButtons.OK, _ MessageBoxIcon.Information) End If End If Catch ex As Exception MessageBox.Show(ex.Message, ex.Source, _ MessageBoxButtons.OK, MessageBoxIcon.Error) End Try

5. Save your project, then press F5 to run it. 6. Within Excel, click the Load Data link, and verify that youve successfully loaded the data. 7. Modify any of the loaded data and click the Update Data hyperlink to send your changes back to SQL Server. 8. Close Excel, but this time dont save the changes made to the workbook. Once back in Visual Studio .NET, press F5 again to start debugging again. 9. Click the Load Data link again, and verify that that your submitted changes show up. 10. Close Excel (saving the workbook, if you like), returning to Visual Studio .NET.

Run Code Before Closing the Workbook


To complete the walkthrough, you need to add code to the Workbooks BeforeClose event. This event will check to see if there are any changes that have not yet been submitted to the server. If so, the code displays an alert asking if changes should be submitted. 1. Add the following code to the ThisWorkbook_BeforeClose procedure:
If mds.HasChanges Then If MessageBox.Show( _ "There are changes that need to submitted. " & _ "Do you want to send the changes in now?", _ "Question", MessageBoxButtons.YesNo, _ MessageBoxIcon.Question) = DialogResult.Yes Then UpdateData() End If End If

2. Save your project, then press F5 to run it.

3. Within Excel, click the Load Data link. 4. Modify any of the loaded data and then attempt to close the workbook. You will see an alert indicating that you have changes to be saved. Click Yes to save your changes. 5. When Excel prompts you to save the workbook, select No. 6. Once back in Visual Studio .NET, press F5 again to start debugging again. 7. Click the Load Data link again, and verify that that your changes show up. 8. Close Excel (saving the workbook, if you like), returning to Visual Studio .NET.

You might also like