Question : How do I populate a SQL Express table from Excel file?

Hello Experts.

I need to populate a table in a SQL Server Express table from an Excel file, but I don't know how to get it done.  Currently, I have a control with a datagrid that displays the contents of the table, it's a list of employees and their contact information:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ContactGrid.ascx.vb" Inherits="Sitefinity_UserControls_ContactGrid_ContactGrid" %>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="id" DataSourceID="SqlDataSource1" CellPadding="4" 
    ForeColor="#333333" GridLines="None">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="FullName" HeaderText="Employee Name" ReadOnly="True" 
                    SortExpression="FullName" />
                <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
                <asp:BoundField DataField="phoneNumber" HeaderText="Phone Number" 
                    SortExpression="phoneNumber" />
                <asp:CommandField SelectText="Send Message" ShowSelectButton="True" 
                    HeaderText="Email" />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#014686" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT [id], [FullName], [title], [phoneNumber] FROM [emailView] ORDER BY [FullName]">
        </asp:SqlDataSource>


The code behind is:
1:
2:
3:
4:
5:
6:
7:
Partial Class Sitefinity_UserControls_ContactGrid_ContactGrid
    Inherits System.Web.UI.UserControl
    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        Response.Redirect("EmailForm.aspx?id=" & GridView1.SelectedDataKey.Value.ToString)
    End Sub
End Class


I have added the following reference to the project:
Interop.Microsoft.Office.Core.dll

I have been given the following, but I don't know where to put it or how to make it work.
Imports Microsoft.Office.Interop
Imports System.Data.OleDb

'
'be sure to add a COM reference called "Microsoft Excel 12.0 Object Library" to the project
'

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xlsConnString As String
        Dim xlsConn As New OleDbConnection()
        Dim xlsReader As OleDbDataReader
        Dim xlscmd As New OleDbCommand()

        Dim FileName As String = "\\newcowboy\AllTDRA\AllTDRA\Website Contacts\Website Contact List.xlsx"  'J:\AllTDRA\Website Contacts
        Dim SheetName As String

        Dim FirstName As String
        Dim LastName As String
        Dim EmailAddress As String
        Dim JobTitle As String
        Dim Phone As String
        Dim DivisionCode As String
        Dim Division_name As String

        ' this is for excel 2007 (.xlsx)
        xlsConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & FileName & ";Extended Properties=""Excel 12.0 Xml;HDR=YES"""

        ' open excel file like a database table
        xlsConn.ConnectionString = xlsConnString
        xlsConn.Open()
        xlscmd.Connection = xlsConn

        ' get the name of the first worksheet in the file
        Dim dt As New System.Data.DataTable()
        dt = xlsConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
        SheetName = dt.Rows(0)("table_name").ToString()

        xlscmd.CommandText = "select * from [" & SheetName & "]"  ' select all the columns and all the rows from the first worksheet

        xlsReader = xlscmd.ExecuteReader()
        '     xlsReader.Read()   ' discard the first record (column headings)  (but not if first row is frozen)

        ' read each row
        While xlsReader.Read()
            FirstName = xlsReader(0).ToString
            LastName = xlsReader(1).ToString
            EmailAddress = xlsReader(2).ToString
            JobTitle = xlsReader(3).ToString
            Phone = xlsReader(4).ToString
            DivisionCode = xlsReader(5).ToString
            Division_name = xlsReader(6).ToString
            ' if you need to check for null or blanks, use something like this:  If ((Not IsDBNull(xlsReader(3).ToString)) And (xlsReader(3).ToString <> "")) Then
        End While

        xlsReader.Close()
        xlsConn.Close()

    End Sub
End Class

I have attached the Excel file from which the information will be pulled.  

Your help and patience is greatly appreciated.

Thanks,
Jerald







Attachments:
 
Excel contact list.
 

Answer : How do I populate a SQL Express table from Excel file?

Inorder to read the properties or methods or attributes of an object we should use
using System.Reflection; //Namespace.
Here i'm giving a sample



1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
class testclass
    {
        public string property1
        {
            set;
            get;
        }
        public string property2
        {
            set;
            get;
        }
        public int property3
        {
            set;
            get;
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            //creating object for testclass & setting values to properties
            testclass objtestclass = new testclass();
            objtestclass.property1 = "Value1";
            objtestclass.property2 = "Value2";
            objtestclass.property3 = 120;

            PropertyInfo[] pinfo;            
            pinfo = objtestclass.GetType().GetProperties();
                foreach (PropertyInfo p in pinfo)
                {
                    Console.WriteLine("Property Name: "+p.Name);
                    Console.WriteLine("Property Value: " +p.GetValue(objtestclass, null) + "\n");
                }
                Console.Read();
        }
    }
Random Solutions  
 
programming4us programming4us