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

Importing .txt file

Skc
I have a .txt which has been exported as a .csv from an external source.
What i need to do is to import this into SQL2000 (into a table) but I need to
do special things on the data:

1. I need to look for the first three chars and import rows into separate
tables. E.g. if the first three chars begin with CCC, then this row goes
into the CCC_table, if it is TTT then into the TTT_table etc...
2. Once I have my tables built up, I need to do add more columns onto the
table, e.g. AAA, BBB, CCC and do a find and replace on the data. E.g. in
table CCC_table, if the 2nd row contains a AAA, then I need to output this to
the AAA field.

Please can someone help me on this. I am new to VB.net and Visual
Studio.net as a whole, but would appreciate some pointers.

Thanks,

skc
Oct 6 '05 #1
11 3042
Skc,

This is an often asked answer and filled with tons of answers in these
newsgroups.
The best newsgroups for this kind of questions are the next time.

microsoft.public.dotnet.framework.adonet
microsoft.public.dotnet.languages.vb

However, we have as well a sample for this on our website.

http://www.windowsformsdatagridhelp....f-212f9e0de193

The other way around is as well on our site.

I hope this helps

Cor

"Skc" <Sk*@discussions.microsoft.com> schreef in bericht
news:56**********************************@microsof t.com...
I have a .txt which has been exported as a .csv from an external source.
What i need to do is to import this into SQL2000 (into a table) but I need
to
do special things on the data:

1. I need to look for the first three chars and import rows into separate
tables. E.g. if the first three chars begin with CCC, then this row goes
into the CCC_table, if it is TTT then into the TTT_table etc...
2. Once I have my tables built up, I need to do add more columns onto the
table, e.g. AAA, BBB, CCC and do a find and replace on the data. E.g. in
table CCC_table, if the 2nd row contains a AAA, then I need to output this
to
the AAA field.

Please can someone help me on this. I am new to VB.net and Visual
Studio.net as a whole, but would appreciate some pointers.

Thanks,

skc

Oct 6 '05 #2
Skc
I don't understand - please can you elaborate.

"Cor Ligthert [MVP]" wrote:
Skc,

This is an often asked answer and filled with tons of answers in these
newsgroups.
The best newsgroups for this kind of questions are the next time.

microsoft.public.dotnet.framework.adonet
microsoft.public.dotnet.languages.vb

However, we have as well a sample for this on our website.

http://www.windowsformsdatagridhelp....f-212f9e0de193

The other way around is as well on our site.

I hope this helps

Cor

"Skc" <Sk*@discussions.microsoft.com> schreef in bericht
news:56**********************************@microsof t.com...
I have a .txt which has been exported as a .csv from an external source.
What i need to do is to import this into SQL2000 (into a table) but I need
to
do special things on the data:

1. I need to look for the first three chars and import rows into separate
tables. E.g. if the first three chars begin with CCC, then this row goes
into the CCC_table, if it is TTT then into the TTT_table etc...
2. Once I have my tables built up, I need to do add more columns onto the
table, e.g. AAA, BBB, CCC and do a find and replace on the data. E.g. in
table CCC_table, if the 2nd row contains a AAA, then I need to output this
to
the AAA field.

Please can someone help me on this. I am new to VB.net and Visual
Studio.net as a whole, but would appreciate some pointers.

Thanks,

skc


Oct 6 '05 #3
D>I don't understand - please can you elaborate.

What is it that you don't understand, did you try the sample?

In that way you get a datatable wherein you can handle all the columns
separately.

Cor
Oct 6 '05 #4
Skc
If I create a new button, shall I assign this code to it and see what happens?

"Cor Ligthert [MVP]" wrote:
D>I don't understand - please can you elaborate.

What is it that you don't understand, did you try the sample?

In that way you get a datatable wherein you can handle all the columns
separately.

Cor

Oct 6 '05 #5
Skc,

With your file than of course. Than you get a datatable.

If you got that, reply than, than we take the other problem.

Cor
"Skc" <Sk*@discussions.microsoft.com> schreef in bericht
news:D1**********************************@microsof t.com...
If I create a new button, shall I assign this code to it and see what
happens?

"Cor Ligthert [MVP]" wrote:
D>I don't understand - please can you elaborate.
>

What is it that you don't understand, did you try the sample?

In that way you get a datatable wherein you can handle all the columns
separately.

Cor

Oct 6 '05 #6
Skc
This is my code... I have errors on it...help!

Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class Form1
Inherits System.Windows.Forms.Form
#Region "Windows Form Designer generated code "
Public Sub New()
MyBase.New()
If m_vb6FormDefInstance Is Nothing Then
If m_InitializingDefInstance Then
m_vb6FormDefInstance = Me
Else
Try
'For the start-up form, the first instance created is the default
instance.
If
System.Reflection.Assembly.GetExecutingAssembly.En tryPoint.DeclaringType Is
Me.GetType Then
m_vb6FormDefInstance = Me
End If
Catch
End Try
End If
End If
'This call is required by the Windows Form Designer.
InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
If Disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(Disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Public ToolTip1 As System.Windows.Forms.ToolTip
Public WithEvents cboDelimiter As System.Windows.Forms.ComboBox
Public WithEvents txtTable As System.Windows.Forms.TextBox
Public WithEvents txtDatabaseFile As System.Windows.Forms.TextBox
Public WithEvents cmdImport As System.Windows.Forms.Button
Public WithEvents txtTextFile As System.Windows.Forms.TextBox
Public WithEvents _Label2_1 As System.Windows.Forms.Label
Public WithEvents _Label1_2 As System.Windows.Forms.Label
Public WithEvents _Label1_1 As System.Windows.Forms.Label
Public WithEvents _Label1_0 As System.Windows.Forms.Label
Public WithEvents Label1 As
Microsoft.VisualBasic.Compatibility.VB6.LabelArray
Public WithEvents Label2 As
Microsoft.VisualBasic.Compatibility.VB6.LabelArray
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Public WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label3 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.cboDelimiter = New System.Windows.Forms.ComboBox()
Me.txtTable = New System.Windows.Forms.TextBox()
Me.txtDatabaseFile = New System.Windows.Forms.TextBox()
Me.cmdImport = New System.Windows.Forms.Button()
Me.txtTextFile = New System.Windows.Forms.TextBox()
Me._Label2_1 = New System.Windows.Forms.Label()
Me._Label1_2 = New System.Windows.Forms.Label()
Me._Label1_1 = New System.Windows.Forms.Label()
Me._Label1_0 = New System.Windows.Forms.Label()
Me.Label1 = New
Microsoft.VisualBasic.Compatibility.VB6.LabelArray (Me.components)
Me.Label2 = New
Microsoft.VisualBasic.Compatibility.VB6.LabelArray (Me.components)
Me.Button1 = New System.Windows.Forms.Button()
Me.Label3 = New System.Windows.Forms.Label()
CType(Me.Label1, System.ComponentModel.ISupportInitialize).BeginIni t()
CType(Me.Label2, System.ComponentModel.ISupportInitialize).BeginIni t()
Me.SuspendLayout()
'
'cboDelimiter
'
Me.cboDelimiter.BackColor = System.Drawing.SystemColors.Window
Me.cboDelimiter.Cursor = System.Windows.Forms.Cursors.Default
Me.cboDelimiter.ForeColor = System.Drawing.SystemColors.WindowText
Me.cboDelimiter.Items.AddRange(New Object() {"*", ";", ",", "<tab>",
"<space>"})
Me.cboDelimiter.Location = New System.Drawing.Point(120, 144)
Me.cboDelimiter.Name = "cboDelimiter"
Me.cboDelimiter.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.cboDelimiter.Size = New System.Drawing.Size(48, 21)
Me.cboDelimiter.TabIndex = 8
Me.cboDelimiter.Text = "*"
'
'txtTable
'
Me.txtTable.AcceptsReturn = True
Me.txtTable.AutoSize = False
Me.txtTable.BackColor = System.Drawing.SystemColors.Window
Me.txtTable.Cursor = System.Windows.Forms.Cursors.IBeam
Me.txtTable.ForeColor = System.Drawing.SystemColors.WindowText
Me.txtTable.Location = New System.Drawing.Point(120, 120)
Me.txtTable.MaxLength = 0
Me.txtTable.Name = "txtTable"
Me.txtTable.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.txtTable.Size = New System.Drawing.Size(176, 19)
Me.txtTable.TabIndex = 5
Me.txtTable.Text = "DataValues"
'
'txtDatabaseFile
'
Me.txtDatabaseFile.AcceptsReturn = True
Me.txtDatabaseFile.AutoSize = False
Me.txtDatabaseFile.BackColor = System.Drawing.SystemColors.Window
Me.txtDatabaseFile.Cursor = System.Windows.Forms.Cursors.IBeam
Me.txtDatabaseFile.ForeColor = System.Drawing.SystemColors.WindowText
Me.txtDatabaseFile.Location = New System.Drawing.Point(120, 96)
Me.txtDatabaseFile.MaxLength = 0
Me.txtDatabaseFile.Name = "txtDatabaseFile"
Me.txtDatabaseFile.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.txtDatabaseFile.Size = New System.Drawing.Size(176, 19)
Me.txtDatabaseFile.TabIndex = 3
Me.txtDatabaseFile.Text = "C:\Temp\test.mdb"
'
'cmdImport
'
Me.cmdImport.BackColor = System.Drawing.SystemColors.Control
Me.cmdImport.Cursor = System.Windows.Forms.Cursors.Default
Me.cmdImport.ForeColor = System.Drawing.SystemColors.ControlText
Me.cmdImport.Location = New System.Drawing.Point(120, 184)
Me.cmdImport.Name = "cmdImport"
Me.cmdImport.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.cmdImport.Size = New System.Drawing.Size(81, 33)
Me.cmdImport.TabIndex = 2
Me.cmdImport.Text = "Import"
'
'txtTextFile
'
Me.txtTextFile.AcceptsReturn = True
Me.txtTextFile.AutoSize = False
Me.txtTextFile.BackColor = System.Drawing.SystemColors.Window
Me.txtTextFile.Cursor = System.Windows.Forms.Cursors.IBeam
Me.txtTextFile.ForeColor = System.Drawing.SystemColors.WindowText
Me.txtTextFile.Location = New System.Drawing.Point(120, 72)
Me.txtTextFile.MaxLength = 0
Me.txtTextFile.Name = "txtTextFile"
Me.txtTextFile.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.txtTextFile.Size = New System.Drawing.Size(176, 19)
Me.txtTextFile.TabIndex = 0
Me.txtTextFile.Text = "C:\Temp\test.txt"
'
'_Label2_1
'
Me._Label2_1.BackColor = System.Drawing.SystemColors.Control
Me._Label2_1.Cursor = System.Windows.Forms.Cursors.Default
Me._Label2_1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label2.SetIndex(Me._Label2_1, CType(1, Short))
Me._Label2_1.Location = New System.Drawing.Point(16, 144)
Me._Label2_1.Name = "_Label2_1"
Me._Label2_1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label2_1.Size = New System.Drawing.Size(65, 17)
Me._Label2_1.TabIndex = 7
Me._Label2_1.Text = "Delimiter"
'
'_Label1_2
'
Me._Label1_2.BackColor = System.Drawing.SystemColors.Control
Me._Label1_2.Cursor = System.Windows.Forms.Cursors.Default
Me._Label1_2.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label1.SetIndex(Me._Label1_2, CType(2, Short))
Me._Label1_2.Location = New System.Drawing.Point(16, 120)
Me._Label1_2.Name = "_Label1_2"
Me._Label1_2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label1_2.Size = New System.Drawing.Size(73, 17)
Me._Label1_2.TabIndex = 6
Me._Label1_2.Text = "Table"
'
'_Label1_1
'
Me._Label1_1.BackColor = System.Drawing.SystemColors.Control
Me._Label1_1.Cursor = System.Windows.Forms.Cursors.Default
Me._Label1_1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label1.SetIndex(Me._Label1_1, CType(1, Short))
Me._Label1_1.Location = New System.Drawing.Point(16, 96)
Me._Label1_1.Name = "_Label1_1"
Me._Label1_1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label1_1.Size = New System.Drawing.Size(96, 17)
Me._Label1_1.TabIndex = 4
Me._Label1_1.Text = "Database File"
'
'_Label1_0
'
Me._Label1_0.BackColor = System.Drawing.SystemColors.Control
Me._Label1_0.Cursor = System.Windows.Forms.Cursors.Default
Me._Label1_0.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label1.SetIndex(Me._Label1_0, CType(0, Short))
Me._Label1_0.Location = New System.Drawing.Point(16, 72)
Me._Label1_0.Name = "_Label1_0"
Me._Label1_0.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label1_0.Size = New System.Drawing.Size(65, 17)
Me._Label1_0.TabIndex = 1
Me._Label1_0.Text = "Text File"
'
'Button1
'
Me.Button1.BackColor = System.Drawing.SystemColors.Control
Me.Button1.Cursor = System.Windows.Forms.Cursors.Default
Me.Button1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Button1.Location = New System.Drawing.Point(264, 184)
Me.Button1.Name = "Button1"
Me.Button1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Button1.Size = New System.Drawing.Size(81, 33)
Me.Button1.TabIndex = 9
Me.Button1.Text = "Sort out data"
'
'Label3
'
Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif",
15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(8, 16)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(328, 32)
Me.Label3.TabIndex = 10
Me.Label3.Text = "Import Program from .txt to .mdb"
'
'Form1
'
Me.AcceptButton = Me.cmdImport
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(584, 294)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label3,
Me.Button1, Me.cboDelimiter, Me.txtTable, Me.txtDatabaseFile, Me.cmdImport,
Me.txtTextFile, Me._Label2_1, Me._Label1_2, Me._Label1_1, Me._Label1_0})
Me.Location = New System.Drawing.Point(76, 101)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
Me.Text = "Form1"
CType(Me.Label1, System.ComponentModel.ISupportInitialize).EndInit( )
CType(Me.Label2, System.ComponentModel.ISupportInitialize).EndInit( )
Me.ResumeLayout(False)

End Sub
#End Region
#Region "Upgrade Support "
Private Shared m_vb6FormDefInstance As Form1
Private Shared m_InitializingDefInstance As Boolean
Public Shared Property DefInstance() As Form1
Get
If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed
Then
m_InitializingDefInstance = True
m_vb6FormDefInstance = New Form1()
m_InitializingDefInstance = False
End If
DefInstance = m_vb6FormDefInstance
End Get
Set
m_vb6FormDefInstance = Value
End Set
End Property
#End Region

Private Sub cmdImport_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cmdImport.Click
Dim delimiter As String
Dim contents As String
Dim lines() As String
Dim fields() As String
Dim wks As DAO.Workspace
Dim db As DAO.Database
Dim fnum As Short
Dim line_num As Short
Dim field_num As Short
Dim sql_statement As String
Dim num_records As Integer

delimiter = cboDelimiter.Text
If delimiter = "<space>" Then delimiter = " "
If delimiter = "<tab>" Then delimiter = vbTab

' Grab the file's contents.
fnum = FreeFile()
On Error GoTo NoTextFile
FileOpen(fnum, txtTextFile.Text, OpenMode.Input)
contents = InputString(fnum, LOF(fnum))
FileClose(fnum)

' Split the contents into lines.
lines = Split(contents, vbCrLf)

' Open the database.
On Error GoTo NoDatabase
wks = DAODBEngine_definst.Workspaces(0)
db = wks.OpenDatabase(txtDatabaseFile.Text)
On Error GoTo 0

' Process the lines and create records.
For line_num = LBound(lines) To UBound(lines)
' Read a text line.
If Len(lines(line_num)) > 0 Then
' Build an INSERT statement.
sql_statement = "INSERT INTO " & txtTable.Text & " VALUES ("

fields = Split(lines(line_num), delimiter)
For field_num = LBound(fields) To UBound(fields)
' Add the field to the statement.
sql_statement = sql_statement & "'" & fields(field_num)
& "', "
Next field_num

' Remove the last comma.
sql_statement = VB.Left(sql_statement, Len(sql_statement) -
2) & ")"

' Insert the record.
On Error GoTo SQLError
db.Execute(sql_statement)
On Error GoTo 0
num_records = num_records + 1
End If
Next line_num

' Close the database.
db.Close()
wks.Close()
MsgBox("Inserted " & VB6.Format(num_records) & " records")
Exit Sub

NoTextFile:
MsgBox("Error opening text file.")
Exit Sub

NoDatabase:
MsgBox("Error opening database.")
FileClose(fnum)
Exit Sub

SQLError:
MsgBox("Error executing SQL statement '" & sql_statement & "'")
FileClose(fnum)
db.Close()
wks.Close()
Exit Sub
End Sub
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs
As System.EventArgs) Handles MyBase.Load
' Enter default file and database names.
txtTextFile.Text = VB6.GetPath & "\testdata.txt"
txtDatabaseFile.Text = VB6.GetPath & "\testdata.mdb"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim file As String = VB6.GetPath & "\testdata.txt"
Dim path As String = VB6.GetPath & "\Test1\"
Dim ds As New DataSet()
Try
Dim f As System.IO.File
If f.Exists(path & file) Then
Dim ConStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
path & ";Extended Properties=""Text;HDR=No;FMT=Delimited\"""
Dim conn As New OleDb.OleDbConnection(ConStr)
Dim da As New OleDb.OleDbDataAdapter("Select * from " &
file, conn)
da.Fill(ds, "TextFile")
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
DataGrid1.DataSource = ds.Tables(0)
End Sub
End Class

"Cor Ligthert [MVP]" wrote:
Skc,

With your file than of course. Than you get a datatable.

If you got that, reply than, than we take the other problem.

Cor
"Skc" <Sk*@discussions.microsoft.com> schreef in bericht
news:D1**********************************@microsof t.com...
If I create a new button, shall I assign this code to it and see what
happens?

"Cor Ligthert [MVP]" wrote:
D>I don't understand - please can you elaborate.
>
What is it that you don't understand, did you try the sample?

In that way you get a datatable wherein you can handle all the columns
separately.

Cor


Oct 7 '05 #7
Skc
Cor,

I have created a new button and have started my own subroutine:

Private Sub Button1_Click(ByVal oTable As DataTable, ByVal oConn As
System.EventArgs) Handles Button1.Click
Dim oRow As DataRow
For Each oRow In oTable.Rows()

Next

End Sub

in here I want to pick up the first cell of the .csv and step through the
rows one-by-one and dump the cells to variables and then drop in in the rows
later.

"Cor Ligthert [MVP]" wrote:
Skc,

With your file than of course. Than you get a datatable.

If you got that, reply than, than we take the other problem.

Cor
"Skc" <Sk*@discussions.microsoft.com> schreef in bericht
news:D1**********************************@microsof t.com...
If I create a new button, shall I assign this code to it and see what
happens?

"Cor Ligthert [MVP]" wrote:
D>I don't understand - please can you elaborate.
>
What is it that you don't understand, did you try the sample?

In that way you get a datatable wherein you can handle all the columns
separately.

Cor


Oct 7 '05 #8
> I have created a new button and have started my own subroutine:

Private Sub Button1_Click(ByVal oTable As DataTable, ByVal oConn As
System.EventArgs) Handles Button1.Click
Dim oRow As DataRow
For Each oRow In oTable.Rows()

Next

End Sub

in here I want to pick up the first cell of the .csv and step through the
rows one-by-one and dump the cells to variables and then drop in in the
rows
later.

As far as I can see, you are on the right track,

And please clean up your code first before you sent it to the newsgroups.

I assume that your program can at least be 70% smaller.

Not that I investigated it.

Cor
Oct 7 '05 #9
On Thu, 6 Oct 2005 01:14:02 -0700, Skc <Sk*@discussions.microsoft.com> wrote:

¤ I have a .txt which has been exported as a .csv from an external source.
¤ What i need to do is to import this into SQL2000 (into a table) but I need to
¤ do special things on the data:
¤
¤ 1. I need to look for the first three chars and import rows into separate
¤ tables. E.g. if the first three chars begin with CCC, then this row goes
¤ into the CCC_table, if it is TTT then into the TTT_table etc...
¤ 2. Once I have my tables built up, I need to do add more columns onto the
¤ table, e.g. AAA, BBB, CCC and do a find and replace on the data. E.g. in
¤ table CCC_table, if the 2nd row contains a AAA, then I need to output this to
¤ the AAA field.
¤
¤ Please can someone help me on this. I am new to VB.net and Visual
¤ Studio.net as a whole, but would appreciate some pointers.

You can probably do most of this using just SQL. I don't know whether you're working with existing
tables or are creating new tables on the fly but something like the following should work (F1 is the
first column of the text file):

Dim TextConnection As New
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;" & _
"Data Source=" & "E:\My
Documents\TextFiles" & ";" & _
"Extended
Properties=""Text;HDR=NO;""")

TextConnection.Open()

'New table
Dim TextCommand As New System.Data.OleDb.OleDbCommand("SELECT * INTO [CCC_Table] IN ''
[ODBC;Driver={SQL Server};Server=(local);Database=Northwind;Trusted_ Connection=yes] FROM
[FileNamet#csv] WHERE Left(F1,3) = 'CCC'", TextConnection)

TextCommand.ExecuteNonQuery()
TextConnection.Close()

As for question #2, you can add columns using ALTER TABLE:

http://msdn.microsoft.com/library/en...asp?frame=true

....and use an UPDATE query to populate these new columns:

http://msdn.microsoft.com/library/en...asp?frame=true
Paul
~~~~
Microsoft MVP (Visual Basic)
Oct 7 '05 #10
Skc
Paul,

Based on what I copies/pasted a few days ago, do you think you can email me
the code I need to have in order for me to do this please? I am at a total
deadend. My email address is: sk*@coltsyard.com

Thanks,

skc

"Paul Clement" wrote:
On Thu, 6 Oct 2005 01:14:02 -0700, Skc <Sk*@discussions.microsoft.com> wrote:

¤ I have a .txt which has been exported as a .csv from an external source.
¤ What i need to do is to import this into SQL2000 (into a table) but I need to
¤ do special things on the data:
¤
¤ 1. I need to look for the first three chars and import rows into separate
¤ tables. E.g. if the first three chars begin with CCC, then this row goes
¤ into the CCC_table, if it is TTT then into the TTT_table etc...
¤ 2. Once I have my tables built up, I need to do add more columns onto the
¤ table, e.g. AAA, BBB, CCC and do a find and replace on the data. E.g. in
¤ table CCC_table, if the 2nd row contains a AAA, then I need to output this to
¤ the AAA field.
¤
¤ Please can someone help me on this. I am new to VB.net and Visual
¤ Studio.net as a whole, but would appreciate some pointers.

You can probably do most of this using just SQL. I don't know whether you're working with existing
tables or are creating new tables on the fly but something like the following should work (F1 is the
first column of the text file):

Dim TextConnection As New
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;" & _
"Data Source=" & "E:\My
Documents\TextFiles" & ";" & _
"Extended
Properties=""Text;HDR=NO;""")

TextConnection.Open()

'New table
Dim TextCommand As New System.Data.OleDb.OleDbCommand("SELECT * INTO [CCC_Table] IN ''
[ODBC;Driver={SQL Server};Server=(local);Database=Northwind;Trusted_ Connection=yes] FROM
[FileNamet#csv] WHERE Left(F1,3) = 'CCC'", TextConnection)

TextCommand.ExecuteNonQuery()
TextConnection.Close()

As for question #2, you can add columns using ALTER TABLE:

http://msdn.microsoft.com/library/en...asp?frame=true

....and use an UPDATE query to populate these new columns:

http://msdn.microsoft.com/library/en...asp?frame=true
Paul
~~~~
Microsoft MVP (Visual Basic)

Oct 9 '05 #11
Skc
Paul,

The other problem I am facing is that the .TXT file I am importing contains
delimiatation by """, so I have "AAA", "222" but I only need the AAA and 222
values without the speech marks!!

Please help.

skc

"Paul Clement" wrote:
On Thu, 6 Oct 2005 01:14:02 -0700, Skc <Sk*@discussions.microsoft.com> wrote:

¤ I have a .txt which has been exported as a .csv from an external source.
¤ What i need to do is to import this into SQL2000 (into a table) but I need to
¤ do special things on the data:
¤
¤ 1. I need to look for the first three chars and import rows into separate
¤ tables. E.g. if the first three chars begin with CCC, then this row goes
¤ into the CCC_table, if it is TTT then into the TTT_table etc...
¤ 2. Once I have my tables built up, I need to do add more columns onto the
¤ table, e.g. AAA, BBB, CCC and do a find and replace on the data. E.g. in
¤ table CCC_table, if the 2nd row contains a AAA, then I need to output this to
¤ the AAA field.
¤
¤ Please can someone help me on this. I am new to VB.net and Visual
¤ Studio.net as a whole, but would appreciate some pointers.

You can probably do most of this using just SQL. I don't know whether you're working with existing
tables or are creating new tables on the fly but something like the following should work (F1 is the
first column of the text file):

Dim TextConnection As New
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;" & _
"Data Source=" & "E:\My
Documents\TextFiles" & ";" & _
"Extended
Properties=""Text;HDR=NO;""")

TextConnection.Open()

'New table
Dim TextCommand As New System.Data.OleDb.OleDbCommand("SELECT * INTO [CCC_Table] IN ''
[ODBC;Driver={SQL Server};Server=(local);Database=Northwind;Trusted_ Connection=yes] FROM
[FileNamet#csv] WHERE Left(F1,3) = 'CCC'", TextConnection)

TextCommand.ExecuteNonQuery()
TextConnection.Close()

As for question #2, you can add columns using ALTER TABLE:

http://msdn.microsoft.com/library/en...asp?frame=true

....and use an UPDATE query to populate these new columns:

http://msdn.microsoft.com/library/en...asp?frame=true
Paul
~~~~
Microsoft MVP (Visual Basic)

Oct 9 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jeff Wagner | last post by:
I am importing a file which contains a persons name (firstName, middleName, etc). If I define a function to do this, how can I use the variables outside of that function? Here is the code: ...
12
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In...
1
by: Thomas R. Hummel | last post by:
Hello, I am importing a file using BCP, with a format file. It is a fixed-width file, which I am importing into a table that has a column for each field in the file. The columns in my import...
11
by: Grim Reaper | last post by:
I am importing a .csv file into Access that has 37 fields. My problem is that sometimes the last field only has data at the end of the column (it looks like when you import a file into Access, for...
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...
5
by: dixie | last post by:
If I sent a user an empty database container - dB with no tables and I needed them to import their tables into it and one of their tables was a hidden table with the prefix Usys, is there any way...
3
by: mukeshsrivastav | last post by:
dear sir i want to move form excel to access. i have 5 excel file having same formats and fields.now i want to import all data in one access table. importing one file is easy .but importing and...
0
by: Alun Jones | last post by:
I'm getting the above error in a dialog box from Visual Studio 2005 when trying to sign an assembly using a PFX file, and would like to know how to resolve the problem. Background: The PFX...
7
by: hg | last post by:
Hi, I have the following problem. I find in a directory hierarchy some files following a certain sets of rules: ..../.../../plugin/name1/name1.py ..... ..../.../../plugin/namen/namen.py
12
by: JMO | last post by:
I can import a csv file with no problem. I can also add columns to the datagrid upon import. I want to be able to start importing at the 3rd row. This will pick up the headers necessary for the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.