473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SQL Bulk Copy AutoMapping in VB.NET

33
I have this simple code on VB.NET that inserts data from Excel Spreed Sheet to SQL Server database, now i want the data to be inserted on my database automatically without manually calling the columnmapping.add. Basically, i will insert the data base on the header row on the excel spread sheet.

Here is my code..
Expand|Select|Wrap|Line Numbers
  1. Using connection As New OleDbConnection(Excelconnection)
  2.             Dim cmd1 As New OleDbCommand("SELECT * FROM [Sheet1$]", connection)
  3.             connection.Open()
  4.             Dim dr As OleDbDataReader = cmd1.ExecuteReader
  5.             Dim sqlconnection As New SqlConnection(ServerConnectionStringProperty)
  6.             sqlconnection.Open()
  7.             Dim sqlbulkcopy As New SqlBulkCopy(sqlconnection)
  8.             Dim destination As String = txtTableName.Text
  9.             sqlbulkcopy.DestinationTableName = destination
  10.             While dr.Read()
  11.                 sqlbulkcopy.ColumnMappings.Add(0, 13)    
  12.                 sqlbulkcopy.ColumnMappings.Add(1, 14)
  13.                 sqlbulkcopy.ColumnMappings.Add(2, 15)
  14.                 sqlbulkcopy.ColumnMappings.Add(3, 17)
  15.                 sqlbulkcopy.ColumnMappings.Add(4, 26)
  16.                 sqlbulkcopy.ColumnMappings.Add(5, 56)       
  17.                 sqlbulkcopy.ColumnMappings.Add(6, 57)       
  18.                 sqlbulkcopy.ColumnMappings.Add(7, 58)      
  19.                 sqlbulkcopy.ColumnMappings.Add(8, 61)       
  20.                 sqlbulkcopy.ColumnMappings.Add(9, 63)      
  21.                 sqlbulkcopy.ColumnMappings.Add(10, 59)     
  22.                 sqlbulkcopy.ColumnMappings.Add(11, 67)
  23.                 sqlbulkcopy.WriteToServer(dr)
  24.                 Dim rowsCopied As Integer = SqlBulkCopyHelper.GetRowsCopied(sqlbulkcopy)
  25.                 MessageBox.Show(String.Concat(rowsCopied, " rows affected"), "Success")
  26.  
  27.             End While
  28.         End Using
Dec 18 '15 #1
0 1364

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jim Geissman | last post by:
I am trying to bulk insert a text file. The file has fixed-length fields with no field terminators. BOL says that field terminators are only needed when the data does *not* contain fixed-length...
1
by: Patrick Dunnigan | last post by:
Hi, I am attempting a bulk copy from a c program into SQL Server 2000 using DBLib in freeTDS 0.63 RC11 (gcc 3.4.3, RH 9). I am getting an error message that I cannot find any documentation on. ...
9
by: David Rysdam | last post by:
I have a large amount of data that I copy in and out of Sybase very often. Now I also want to copy this data in and out of postgres. I have an existing script that creates the entire database(s)...
19
by: Khafancoder | last post by:
Hi guys, in my db i have these three tables 1.Stores 2.Products 3.Parts their structure is something like : Stores ----Products ----Parts
7
by: twinklyblue | last post by:
Hi The Scripts team, I would like to know if there is a same function like bcp (for sybase and mssql 2k) for postgres? Is there any way that I can copy my csv datafile into the postgres database?...
4
by: manoop | last post by:
Hi friends How to bulk copy the output of a query to textfile in PL/SQL Thanks Manoop
4
by: alpareshamwala | last post by:
Hi Friends, i wanted to bulk copy an MS Excel file to Oracle Database the way "bcp" is used in MS Sql Server. pl. help ASAP Alpa
1
by: Garima12 | last post by:
i need to export file1.txt file's data in sql server. want to use bulk copy command from command prompt.can someone please let me know the syntax? thks
4
by: labmonkey111 | last post by:
The company I work for uses a very large program in Access/VBA. Whenever I edit it, I copy the latest edition to my harddrive and make the necessary changes, then copy the modified...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.