ByteScout Spreadsheet SDK - VB.NET - Convert XLS to SQL Server (via CSV BULK INSERT) - ByteScout

ByteScout Spreadsheet SDK – VB.NET – Convert XLS to SQL Server (via CSV BULK INSERT)

  • Home
  • /
  • Articles
  • /
  • ByteScout Spreadsheet SDK – VB.NET – Convert XLS to SQL Server (via CSV BULK INSERT)

How to convert XLS to SQL server (via CSV BULK insert) in VB.NET using ByteScout Spreadsheet SDK

This code in VB.NET shows how to convert XLS to SQL server (via CSV BULK insert) with this how to tutorial

Source code documentation samples provide quick and easy way to add a required functionality into your application. What is ByteScout Spreadsheet SDK? It is the SDK component for writing, reading, modifying and calculating Excel and CSV spreadsheets. Can calculate and reculculate formulas with Excel installed. You may import or export data to and from CSV, XML, JSON. Supports export to databases, arrays, streams. It can help you to convert XLS to SQL server (via CSV BULK insert) in your VB.NET application.

Fast application programming interfaces of ByteScout Spreadsheet SDK for VB.NET plus the instruction and the code below will help you quickly learn how to convert XLS to SQL server (via CSV BULK insert). In your VB.NET project or application you may simply copy & paste the code and then run your app! Enjoy writing a code with ready-to-use sample VB.NET codes.

Our website provides trial version of ByteScout Spreadsheet SDK for free. It also includes documentation and source code samples.

Try it today: Get 60 Day Free Trial or sign up for Web API

ExportToSQLServer.VS2005.vbproj
      
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProductVersion>8.0.50727</ProductVersion> <SchemaVersion>2.0</SchemaVersion> <ProjectGuid>{8E9081E3-B6D8-4B15-8EBD-EE32D7A5F63F}</ProjectGuid> <OutputType>Exe</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>ExportToSQLServer</RootNamespace> <AssemblyName>ExportToSQLServer</AssemblyName> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> <DefineConstants>DEBUG,TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DebugType>pdbonly</DebugType> <Optimize>true</Optimize> <OutputPath>bin\Release\</OutputPath> <DefineConstants>TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <Import Project="$(MSBuildBinPath)\Microsoft.VisualBasic.Targets" /> <ItemGroup> <Import Include="Microsoft.VisualBasic" /> <Import Include="System" /> <Reference Include="Bytescout.Spreadsheet, Version=2.2.0.307, Culture=neutral, PublicKeyToken=f7dd1bd9d40a50eb, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> </Reference> <Reference Include="System" /> <Reference Include="System.Data" /> <Reference Include="System.Xml" /> <Compile Include="Program.vb" /> <Compile Include="Properties\AssemblyInfo.vb" /> <Content Include="SimpleReport.xls"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Content> </ItemGroup> <ItemGroup> </ItemGroup> </Project>

Try it today: Get 60 Day Free Trial or sign up for Web API

ExportToSQLServer.VS2008.vbproj
      
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProductVersion>8.0.50727</ProductVersion> <SchemaVersion>2.0</SchemaVersion> <ProjectGuid>{8E9081E3-B6D8-4B15-8EBD-EE32D7A5F63F}</ProjectGuid> <OutputType>Exe</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>ExportToSQLServer</RootNamespace> <AssemblyName>ExportToSQLServer</AssemblyName> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> <DefineConstants>DEBUG,TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DebugType>pdbonly</DebugType> <Optimize>true</Optimize> <OutputPath>bin\Release\</OutputPath> <DefineConstants>TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <Import Project="$(MSBuildBinPath)\Microsoft.VisualBasic.Targets" /> <ItemGroup> <Import Include="Microsoft.VisualBasic" /> <Import Include="System" /> <Reference Include="Bytescout.Spreadsheet, Version=2.2.0.307, Culture=neutral, PublicKeyToken=f7dd1bd9d40a50eb, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> </Reference> <Reference Include="System" /> <Reference Include="System.Data" /> <Reference Include="System.Xml" /> <Compile Include="Program.vb" /> <Compile Include="Properties\AssemblyInfo.vb" /> <Content Include="SimpleReport.xls"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Content> </ItemGroup> <ItemGroup> </ItemGroup> </Project>

Try it today: Get 60 Day Free Trial or sign up for Web API

ExportToSQLServer.VS2010.vbproj
      
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProductVersion> </ProductVersion> <SchemaVersion>2.0</SchemaVersion> <ProjectGuid>{8E9081E3-B6D8-4B15-8EBD-EE32D7A5F63F}</ProjectGuid> <OutputType>Exe</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>ExportToSQLServer</RootNamespace> <AssemblyName>ExportToSQLServer</AssemblyName> <OldToolsVersion>3.5</OldToolsVersion> <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> <DefineConstants>DEBUG,TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DebugType>pdbonly</DebugType> <Optimize>true</Optimize> <OutputPath>bin\Release\</OutputPath> <DefineConstants>TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.Targets" /> <ItemGroup> <Import Include="Microsoft.VisualBasic" /> <Import Include="System" /> <Reference Include="Bytescout.Spreadsheet, Version=2.2.0.307, Culture=neutral, PublicKeyToken=f7dd1bd9d40a50eb, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> </Reference> <Reference Include="System" /> <Reference Include="System.Data" /> <Reference Include="System.Xml" /> <Compile Include="Program.vb" /> <Compile Include="Properties\AssemblyInfo.vb" /> <Content Include="SimpleReport.xls"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Content> </ItemGroup> <ItemGroup> </ItemGroup> </Project>

Try it today: Get 60 Day Free Trial or sign up for Web API

Program.vb
      
Imports System.IO Imports Bytescout.Spreadsheet Imports System.Data.SqlClient Class Program Friend Shared Sub Main(args As String()) Try ' Load XLS document Using document As New Spreadsheet() document.LoadFromFile("SimpleReport.xls") Dim csvFile As String = Path.GetTempPath() & "SimpleReport.csv" ' Save the document as CSV file document.Workbook.Worksheets(0).SaveAsCSV(csvFile) document.Close() If File.Exists(csvFile) Then ' MODIFY THE CONNECTION STRING WITH YOUR CREDENTIALS!!! Dim connectionString As String = "Data Source=localhost;Initial Catalog=master;Integrated Security=true;" Using connection As New SqlConnection(connectionString) connection.Open() ' Drop test database if exists ExecuteQueryWithoutResult(connection, "IF DB_ID ('XlsTests') IS NOT NULL DROP DATABASE XlsTests") ' Create empty database ExecuteQueryWithoutResult(connection, "CREATE DATABASE XlsTests") ' Switch to created database ExecuteQueryWithoutResult(connection, "USE XlsTests") ' Create a table for CSV data ExecuteQueryWithoutResult(connection, "CREATE TABLE CsvTest (Name VARCHAR(40), FullName VARCHAR(255))") ' Export CSV data from local file ExecuteQueryWithoutResult(connection, "BULK INSERT CsvTest FROM '" & csvFile & "' WITH ( FIELDTERMINATOR = ';', ROWTERMINATOR = '" & vbLf & "')") ' Check the data successfully exported Using command As New SqlCommand("SELECT * from CsvTest", connection) Dim reader As SqlDataReader = command.ExecuteReader() If reader IsNot Nothing Then Console.WriteLine() Console.WriteLine("Exported CSV data:") Console.WriteLine() While reader.Read() Console.WriteLine([String].Format("{0} | {1}", reader(0), reader(1))) End While End If End Using Console.WriteLine() Console.WriteLine("Press any key.") Console.ReadKey() End Using End If End Using Catch ex As Exception Console.WriteLine("Error: " & ex.Message) Console.ReadKey() End Try End Sub Private Shared Sub ExecuteQueryWithoutResult(connection As SqlConnection, query As String) Using command As New SqlCommand(query, connection) command.ExecuteNonQuery() End Using End Sub End Class

Try it today: Get 60 Day Free Trial or sign up for Web API

MORE INFORMATION

Get 60 Day Free Trial or Visit ByteScout Spreadsheet SDK page

Explore ByteScout Spreadsheet SDK documentation

WEB API VERSION

Sign Up for free Web API key

Explore Web API Documentation

Tutorials:

prev
next