473,396 Members | 1,879 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,396 software developers and data experts.

Problem using BulkCopy to imput delimited text file into SQL table

the file I'm importing has > 200,000 records

I have one problem:

One of the columns in the .txt file (customerID) has almost all Inetger values except for about 30 records. Those 30 records have one letter in the customerID field (M123456). The field they are going into is VARCHAR(1000) . All of the integers go in correctly but for some reason the cutomerID values that stert with a letter (M123456) are NULL.

Any help? I'm stumped.

The code i am using is below


Expand|Select|Wrap|Line Numbers
  1.  Dim fieldDelimeter As String = TextBox3.Text
  2.  
  3.             Dim timeStamp As String = Now.ToString("yyyyMMddHHmmss")
  4.             Dim importFile As New FileInfo(TextBox1.Text)
  5.             If importFile.Exists Then
  6.  
  7.  
  8.  
  9.  
  10.  
  11.                 Dim originalFileName As String = importFile.Name
  12.                 Dim newFileName As String = originalFileName.Replace(".", "") & ".txt"
  13.                 Dim path As String = importFile.DirectoryName
  14.                 File.Move(path & "\" & originalFileName, path & "\" & newFileName)
  15.  
  16.                 Dim schemaFile As New FileInfo(importFile.DirectoryName & "\schema.ini")
  17.  
  18.                 Dim schemaContents As String
  19.                 schemaContents = "[" & newFileName & "]" & System.Environment.NewLine
  20.                 schemaContents = schemaContents & "Format=Delimited(" & fieldDelimeter & ")" & System.Environment.NewLine
  21.                 If TextBox4.Text > "" Then
  22.                     schemaContents = schemaContents & "TextDelimiter=" & TextBox4.Text
  23.                 Else
  24.                     schemaContents = schemaContents & "TextDelimiter=none"
  25.                 End If
  26.                 Dim fs As FileStream = schemaFile.Create()
  27.                 fs.Close()
  28.  
  29.                 Dim objReader As StreamWriter
  30.                 objReader = schemaFile.AppendText()
  31.                 objReader.Write(schemaContents)
  32.                 objReader.Close()
  33.  
  34.                 Dim strCsvConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & path & ";" & _
  35.                                 "Extended Properties='text;FMT=Delimited;HDR=NO'"
  36.  
  37.  
  38.  
  39.                 Dim cn As New OleDb.OleDbConnection(strCsvConn)
  40.  
  41.                 Try
  42.  
  43.                     Dim myCommand As New System.Data.OleDb.OleDbCommand( _
  44.                                                     "SELECT COUNT(*) FROM " & newFileName, cn)
  45.                     cn.Open()
  46.                     Dim records As Integer = 0
  47.                     Dim newReader As OleDb.OleDbDataReader
  48.                     newReader = myCommand.ExecuteReader()
  49.                     newReader.Read()
  50.                     records = newReader.GetInt32(0)
  51.                     cn.Close()
  52.  
  53.                     myCommand = New System.Data.OleDb.OleDbCommand( _
  54.                                 "SELECT * FROM " & newFileName, cn)
  55.  
  56.  
  57.                     cn.Open()
  58.                     Dim i As Integer = 0
  59.                     Dim columns As Integer = myCommand.ExecuteReader().FieldCount
  60.                     Me.ProgressBar2.Maximum = records
  61.                     Dim strCreateTable As String
  62.                     strCreateTable = "CREATE TABLE " & ComboBox2.Text & "_" & timeStamp & " (" & System.Environment.NewLine
  63.                     Do While i < columns - 1
  64.                         strCreateTable = strCreateTable & "   [COLUMN " & i.ToString & "] VARCHAR(1000)," & System.Environment.NewLine
  65.                         i += 1
  66.                     Loop
  67.                     strCreateTable = strCreateTable & "   [COLUMN " & i.ToString & "] VARCHAR(1000)" & System.Environment.NewLine
  68.                     strCreateTable = strCreateTable & ")" & System.Environment.NewLine
  69.                     cn.Close()
  70.  
  71.                     Dim notes As New DataSet
  72.                     notes = QueryDatabase(strCreateTable, Me.sourceDatabaseName.Text)
  73.  
  74.  
  75.                     cn.Close()
  76.  
  77.  
  78.  
  79.                     cn.Open()
  80.  
  81.                     Dim dr As OleDb.OleDbDataReader = myCommand.ExecuteReader
  82.  
  83.  
  84.                     Using bulkCopy As New SqlClient.SqlBulkCopy("server=" & Me.sourceServerName.Text & ";user=" & Me.sourceUserID.Text & ";password=" & Me.sourcePassword.Text & ";database=" & Me.sourceDatabaseName.Text)
  85.  
  86.  
  87.                         AddHandler bulkCopy.SqlRowsCopied, AddressOf OnSqlRowsCopied
  88.                         bulkCopy.BulkCopyTimeout = 1200
  89.                         bulkCopy.NotifyAfter = 100
  90.                         bulkCopy.DestinationTableName = ComboBox2.Text & "_" & timeStamp
  91.                         bulkCopy.WriteToServer(dr)
  92.                         cn.Close()
  93.                         File.Move(path & "\" & newFileName, path & "\" & originalFileName)
  94.                         schemaFile = New FileInfo(importFile.DirectoryName & "\schema.ini")
  95.                         schemaFile.Delete()
  96.                         populateRawDataTablesGrid()
  97.  
  98.                         Me.ProgressBar2.Value = 0
  99.                         MessageBox.Show("Data successfully imported.")
  100.                     End Using
  101.                 Catch ex As Exception
  102.                     MessageBox.Show(ex.ToString)
  103.                     cn.Close()
  104.                     File.Move(path & "\" & newFileName, path & "\" & originalFileName)
  105.                     schemaFile = New FileInfo(importFile.DirectoryName & "\schema.ini")
  106.                     schemaFile.Delete()
  107.                     Me.ProgressBar2.Value = 0
  108.                 End Try
  109.             Else
  110.                 Throw New FileNotFoundException()
  111.             End If
Oct 4 '07 #1
0 1667

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

Similar topics

3
by: silven | last post by:
Hi all I am trying to do a Bulk Copy from a "tab delmimited" text file to a table in my database. I have it almost working except when the file has too few columns for the table (table has 421...
1
by: sparks | last post by:
I have never done this and wanted to ask people who have what is the best way. One person said import it to excel, then import it into access table. but since this will be done a lot, I am...
1
by: Steemer | last post by:
Okay, I checked with usual suspects and I'm still frustrated. ..txt file, fixed width. About 88 fields of varying length. All text fields. It will only let me add seperators to about the 410th...
4
by: Jon Asher | last post by:
Hi, I'm trying to do a simple import of a comma delimited text file with COPY but it's returning an error. The file has been granted all permissions in Linux, so it's not clear to me what the...
1
by: svijay | last post by:
hi I have got a strange problem. May I know any solution for this. Here is the detailed description about the problem We have got a mainframe system and also production and development...
4
by: raju5725 | last post by:
have a MS access table and I want to export it to comma delimited text file. How do I do this programmatically using VB.NET or C#? Thanks for any help in advance. Raju
1
by: Fordraiders | last post by:
vb.net 2003 Office 2003 What I have: C:\TestData\Input.txt Text File Pipe Delimited : 4 columns Of data example: 00001|NO BRAND NAME ASSIGNED|6DU27|M3-.5 X 6 FLAT HD SOCKET CAP SCREW,...
7
by: Pete | last post by:
I need to import multiple tab delimited files for which I do not have the formats. All files have column headers in the the line. The files must be dynamic. The only common value is that the...
3
by: Nils | last post by:
I use SQLDMO.Bulkcopy in an VB6 (have to) program to load data from a plain text file into a SQL Server 2000. One of the target columns is NOT NULL but it happens that I receive a missing value...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.