Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vb.net 4.0 and ADO need to import Excel into SQL

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I have no idea why I can't get the [highlight #FCE94F]value[/highlight] of a ADODB field now in .NET. It returns this: "ADODB.InternalField" instead of its value, this "AD1453".
I need to open an Excel file and load the entire thing into a SQL table I want this to be Local SQL .sdf file but could not get the connection right for that either. if someone has a better solution Great!


Code:
        Dim conExcel As ADODB.Connection
        Dim rs As ADODB.Recordset
        conExcel = New ADODB.Connection
        rs = New ADODB.Recordset
        Dim SQLExcelString As String = "Select * from [Application List$]"
  
        Dim ApplicationId As String
        ''Connection to Excel file
        With conExcel
            .Provider = "Microsoft.Jet.OLEDB.4.0"
            .ConnectionString = "Data Source=" & FileName & ";" & _
            "Extended Properties=Excel 8.0;"
            .CursorLocation = adUseClient
            .Open()
        End With

        Dim SQLFilePath As String = Application.StartupPath
        Dim SQLFileName As String = SQLFilePath & "\ForteSQLDB.sdf"
        rs.Open(SQLExcelString, conExcel)
        Dim totalRecs As Integer = rs.RecordCount
        Dim cmd As New SqlCommand
        Dim conSQL As New SqlConnection
        Dim SQLString As String = ""

        Try
            'conSQL.ConnectionString = "Data Source=" & SQLFileName & ""
            conSQL.ConnectionString = "Data Source=MyServer;Initial Catalog=MyTable; " & _
                            "Persist Security Info=True;User ID=MyUser; " & _
                            "Password=MyPass"
            conSQL.Open()
            cmd.Connection = conSQL

            For a = 1 To totalRecs
                [highlight #8AE234]ApplicationId = TryCast(rs.Fields("Application ID").ToString, String)[/highlight]                SQLString = "INSERT INTO table([Application ID]) VALUES('" + ApplicationId + "')"
                cmd.CommandText = SQLString
                cmd.ExecuteNonQuery()
            Next

DougP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top