Avatar billede schneiderjan Nybegynder
16. januar 2006 - 20:47

CSV fil ind i Access - VB.net

Hejsa

Jeg har vedhæftet nedenstående eksempel som kan læse en tekstfil ind i en Access database. Tekstfilen dog i en fixed length, hvor jeg har brug for en Kommasepereret version. Nogen der kan tilpasse eksemplet eller pege på et andet eksempel?

/Jan


Module mdlDeclarações
Structure Modelo
<VBFixedString(16)> Dim Modelo As String
<VBFixedString(30)> Dim Aparelho As String
'Usei esta variável Vazio pois só irei utilizar as
'informações de Modelo e Aparelho do arquivo
<VBFixedString(46)> Dim Vazio As String
End Structure

'Esta declaração é para funcionar o biblioteca DAO no programa
Friend DAODBEngine_definstant As New DAO.DBEngine()

End Module


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

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

'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.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
Me.StatusBar1 = New System.Windows.Forms.StatusBar
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(104, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Importar"
'
'ProgressBar1
'
Me.ProgressBar1.Location = New System.Drawing.Point(8, 48)
Me.ProgressBar1.Name = "ProgressBar1"
Me.ProgressBar1.Size = New System.Drawing.Size(280, 23)
Me.ProgressBar1.TabIndex = 1
'
'StatusBar1
'
Me.StatusBar1.Location = New System.Drawing.Point(0, 85)
Me.StatusBar1.Name = "StatusBar1"
Me.StatusBar1.Size = New System.Drawing.Size(292, 24)
Me.StatusBar1.SizingGrip = False
Me.StatusBar1.TabIndex = 2
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(139, 8)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(75, 23)
Me.Button2.TabIndex = 3
Me.Button2.Text = "Button2"
Me.Button2.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 109)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.StatusBar1)
Me.Controls.Add(Me.ProgressBar1)
Me.Controls.Add(Me.Button1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "Form1"
Me.Text = "Importar Arquivo Modelo"
Me.ResumeLayout(False)

End Sub

#End Region
Dim Area As DAO.Workspace
Dim Banco As DAO.Database

Private Sub Status(ByVal Texto As String)
StatusBar1.Text = Texto
StatusBar1.Refresh()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rs As DAO.Recordset 'Recorset set do Banco de Dados, Access neste caso
Dim Rec As Modelo 'Recorset do arquivo de texto
Dim Texto As String 'Usado para as informações obtidas por record lido do arquivo de texto
Dim C As Long 'Usado para o contador do ProgressBar
Dim FileNum As Integer 'Número do arquivo
Dim RecLen As Integer 'usado para armazenar o tamanho da variável Rec declarada acima
Status("Deletando os dados anteriores...")

'Usar esta rotina para deletar as informações anterios do banco de dados
Banco.Execute("DELETE * FROM MODELO")
rs = Banco.OpenRecordset("MODELO") 'Abrir a tabela MODELO
FileNum = FreeFile()
RecLen = Len(Rec)
ProgressBar1.Maximum = FileLen("Modelo.txt") + 1 'Define o tamanho máximo do ProgressBar
ProgressBar1.Visible = True
FileOpen(FileNum, "Modelo.txt", OpenMode.Random, , , RecLen)
Status("Importando os Dados...")
Do Until EOF(FileNum)
C += 1
ProgressBar1.Value = RecLen * C 'Aqui que está o incrementador do ProgressBar
FileGet(FileNum, Rec)

rs.AddNew()
rs.Fields("Modelo").Value = RTrim(Rec.Modelo)
rs.Fields("Aparelho").Value = RTrim(Rec.Aparelho)
rs.Update()
Loop
FileClose(FileNum) 'Fecha o arquivo texto
rs.Close()
ProgressBar1.Visible = False
Status("")
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Area = DAODBEngine_definstant.CreateWorkspace("", "admin", "")
Banco = Area.OpenDatabase("Modelos.mdb")
End Sub
End Class
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester