Goedemiddag allen,
Ik ben een programmaatje aan het maken. Het is de bedoeling dat uit een ASCII bestand een aantal gegevens worden gelezen in textboxen worden geplaatst en wordt opgeslagen in een database. Het lukt mij echter maar om 1 regel te importeren. Zodra ik meerdere regels wil doen krijg ik het niet meer voor elkaar...
Dit is mijn code tot nog toe:
Ik wil dus eigenlijk een ASCII bestand inladen en de eerste 8 gegevens (= 1 record van 8 kolommen_ inladen. Zodra ik op toevoegen heb gedrukt moet hij de volgende regel inladen (= de volgende 8 gegevens) enz enz.
Mijn ASCII bestand ziet er zo uit:
INSERT;Ferrari;Jean Todt;Bridgestone;FerrariV8;Marlboro;Kimi Raikkonen;Felipe Massa;Luca Badoer
INSERT;Renault;Flavio Briatore;Bridgestond;RenaultV8;ING;Giancarlo Fisichella;Heiki Kovalainen;Nelsinho Piquet
Kan iemand mij helpen? Alvast bedankt!
Ik ben een programmaatje aan het maken. Het is de bedoeling dat uit een ASCII bestand een aantal gegevens worden gelezen in textboxen worden geplaatst en wordt opgeslagen in een database. Het lukt mij echter maar om 1 regel te importeren. Zodra ik meerdere regels wil doen krijg ik het niet meer voor elkaar...
Dit is mijn code tot nog toe:
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
| Public Class Form1 #Region "Private Declarations" Private objConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\formule1.mdb") Private objCommand As New OleDbCommand Private objDataAdapter As New OleDbDataAdapter Private objDataSet As New DataSet Dim objDataView As DataView Dim MyCurrencyManager As CurrencyManager Dim filenaam As String #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load VulGrid() VulDataSetEnDataView() TextBox9.Text = MyCurrencyManager.Position + 1 & " / " & MyCurrencyManager.Count BindDeVelden() Button2.Hide() End Sub Private Sub VulGrid() objDataSet.Clear() objCommand.CommandText = "Select * from teams" objCommand.Connection = objConnection objDataAdapter.SelectCommand = objCommand objDataAdapter.Fill(objDataSet, "queryAlleTeams") DataGridView1.DataSource = objDataSet DataGridView1.DataMember = "queryAlleTeams" End Sub Private Sub VulDataSetEnDataView() objDataSet = New DataSet() objDataAdapter.Fill(objDataSet, "AnyName") objDataView = New DataView(objDataSet.Tables("AnyName")) MyCurrencyManager = CType(Me.BindingContext(objDataView), CurrencyManager) End Sub Private Sub BindDeVelden() TextBox1.DataBindings.Clear() TextBox2.DataBindings.Clear() TextBox3.DataBindings.Clear() TextBox4.DataBindings.Clear() TextBox5.DataBindings.Clear() TextBox6.DataBindings.Clear() TextBox7.DataBindings.Clear() TextBox8.DataBindings.Clear() TextBox1.DataBindings.Add("Text", objDataView, "Teamnaam") TextBox2.DataBindings.Add("Text", objDataView, "Teambaas") TextBox3.DataBindings.Add("Text", objDataView, "Banden") TextBox4.DataBindings.Add("Text", objDataView, "Motor") TextBox5.DataBindings.Add("Text", objDataView, "Hoofdsponsor") TextBox6.DataBindings.Add("Text", objDataView, "EersteCoureur") TextBox7.DataBindings.Add("Text", objDataView, "TweedeCoureur") TextBox8.DataBindings.Add("Text", objDataView, "TestCoureur") End Sub Public Sub vindbestand() Dim objOpenFileDialog As New OpenFileDialog With objOpenFileDialog .Filter = "Tekstbestand (*.txt)|*.txt|Alle bestanden (*.*)|*.*" .FilterIndex = 1 .Title = "ASCII Bestand Openen" End With If objOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then 'Als er een Open_click was, dan selecteer de DatabaseNaam filenaam = objOpenFileDialog.FileName End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click vindbestand() TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() TextBox4.Clear() TextBox5.Clear() TextBox6.Clear() TextBox7.Clear() TextBox8.Clear() Dim text As String Dim bestandsnaam As String bestandsnaam = filenaam Dim objReader As New System.IO.StreamReader(bestandsnaam) text = objReader.ReadLine Dim tekstregel As String tekstregel = text Dim i As Integer Dim aryTekstbestand() As String aryTekstbestand = tekstregel.Split(";") For i = 0 To UBound(aryTekstbestand) Label10.Text = (aryTekstbestand(i)) i = i + 1 TextBox1.Text = (aryTekstbestand(i)) i = i + 1 TextBox2.Text = (aryTekstbestand(i)) i = i + 1 TextBox3.Text = (aryTekstbestand(i)) i = i + 1 TextBox4.Text = (aryTekstbestand(i)) i = i + 1 TextBox5.Text = (aryTekstbestand(i)) i = i + 1 TextBox6.Text = (aryTekstbestand(i)) i = i + 1 TextBox7.Text = (aryTekstbestand(i)) i = i + 1 TextBox8.Text = (aryTekstbestand(i)) i = i + 1 Next i Button2.Visible = True Button2.Enabled = True End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim objCommand As OleDbCommand = New OleDbCommand() objCommand.Connection = objConnection objCommand.CommandType = CommandType.Text objCommand.CommandText = "INSERT INTO teams " & _ "(teamnaam, teambaas, banden, motor, hoofdsponsor, eerstecoureur, tweedecoureur, testcoureur)" & _ "VALUES(@TeamNaam, @TeamBaas, @Banden, @Motor, @Hoofdsponsor, @EersteCoureur, @TweedeCoureur, @TestCoureur)" objCommand.Parameters.AddWithValue("@TeamNaam", TextBox1.Text) objCommand.Parameters.AddWithValue("@TeamBaas", TextBox2.Text) objCommand.Parameters.AddWithValue("@Banden", TextBox3.Text) objCommand.Parameters.AddWithValue("@Motor", TextBox4.Text) objCommand.Parameters.AddWithValue("@Hoofdsponsor", TextBox5.Text) objCommand.Parameters.AddWithValue("@EersteCoureur", TextBox6.Text) objCommand.Parameters.AddWithValue("@TweedeCoureur", TextBox7.Text) objCommand.Parameters.AddWithValue("@TestCoureur", TextBox8.Text) Try objConnection.Open() objCommand.ExecuteNonQuery() Catch Err As OleDbException MessageBox.Show(Err.Message) objConnection.Close() Exit Sub End Try objConnection.Close() VulGrid() VulDataSetEnDataView() TextBox9.Text = MyCurrencyManager.Position & " / " & MyCurrencyManager.Count BindDeVelden() Button2.Hide() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click If MyCurrencyManager.Position = 0 Then MessageBox.Show("Je bent al bij het eerste record.") Else MyCurrencyManager.Position = 0 TextBox9.Text = MyCurrencyManager.Position + 1 & " / " & MyCurrencyManager.Count End If End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click If MyCurrencyManager.Position = 0 Then MessageBox.Show("Je bent al bij het eerste record.") Else MyCurrencyManager.Position -= 1 TextBox9.Text = MyCurrencyManager.Position + 1 & " / " & MyCurrencyManager.Count End If End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click If MyCurrencyManager.Position = MyCurrencyManager.Count - 1 Then MessageBox.Show("Je bent al bij de laatste record.") Else MyCurrencyManager.Position += 1 TextBox9.Text = MyCurrencyManager.Position + 1 & " / " & MyCurrencyManager.Count End If End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click If MyCurrencyManager.Position = MyCurrencyManager.Count - 1 Then MessageBox.Show("Je bent al bij de laatste record.") Else MyCurrencyManager.Position = MyCurrencyManager.Count - 1 TextBox9.Text = MyCurrencyManager.Position + 1 & " / " & MyCurrencyManager.Count End If End Sub |
Ik wil dus eigenlijk een ASCII bestand inladen en de eerste 8 gegevens (= 1 record van 8 kolommen_ inladen. Zodra ik op toevoegen heb gedrukt moet hij de volgende regel inladen (= de volgende 8 gegevens) enz enz.
Mijn ASCII bestand ziet er zo uit:
INSERT;Ferrari;Jean Todt;Bridgestone;FerrariV8;Marlboro;Kimi Raikkonen;Felipe Massa;Luca Badoer
INSERT;Renault;Flavio Briatore;Bridgestond;RenaultV8;ING;Giancarlo Fisichella;Heiki Kovalainen;Nelsinho Piquet
Kan iemand mij helpen? Alvast bedankt!