|
-
Feb 14th, 2012, 12:32 PM
#1
Thread Starter
Fanatic Member
import text file into access database table
if i got text file with the following data for example
0,"first text","
(rc)"
1,"second text","a b c d"
2,"",""
3,,
the first field inthe table is number the second field is text the third field is memo
how can i insert into it from text file
i need help about the code
-
Feb 14th, 2012, 11:43 PM
#2
Re: import text file into access database table
We know you're using a CSV file and we know you're using Access. You say you need help with code but what language? VB6? VB.NET? VBA? C#? Something else? Remember that we know NOTHING about what you're doing so you have to give us ALL the relevant information.
-
Feb 15th, 2012, 03:21 AM
#3
Thread Starter
Fanatic Member
Re: import text file into access database table
i'm using bv.net 2010
and i'm useing nomal text.txt file not csv
and i save the table from the access database. but now i'm looking for away to insert it back into the table.all the ways i find so far is through datagridview..but i'm looking for direct way
-
Feb 15th, 2012, 07:21 AM
#4
Re: import text file into access database table
CSV stands for comma-separated values. You have a text file with a record per line (other than line breaks within fields) and field values separated by commas. That is a CSV file.
You should populate a DataTable with your data. You can then use an OleDbDataAdapter to insert all the records into the database in a single batch. For reading the data in you have two choices: ADO.NET (OleDbDataAdapter or OleDbDataReader) or a TextFieldParser. For the ADO.NET examples, follow the CodeBank link in my signature and check out my Receiving & Saving Data thread. The one extra point to note is that, if you use a data adapter to load the data, set its AcceptChangesDuringFill property to False. The MSDN documentation for the TextFieldParser class has code examples if you want to go that way.
-
Feb 15th, 2012, 10:26 AM
#5
Thread Starter
Fanatic Member
Re: import text file into access database table
ok i'm using this code but i get the error vshost32-clr2.exe has stopped working and then the application close
vb Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
OpenFileDialog1.ShowDialog()
Dim filepath As String = OpenFileDialog1.FileName
Dim AccessConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath & ";user id=admin;jet oledb:database password=123")
AccessConn.Open()
Dim cmd As New OleDb.OleDbCommand("SELECT * INTO [import] FROM [Text;Database=C:\Users\BS\Desktop\;Hdr=No].[Messages.txt]", AccessConn)
cmd.ExecuteNonQuery()
AccessConn.Close()
End Sub
-
Feb 15th, 2012, 11:15 AM
#6
Re: import text file into access database table
I have already directed you to my CodeBank thread. Where in that thread do I use ExecuteNonQuery to execute a SELECT statement? In my previous post I specifically said to populate a DataTable using a data adapter or a data reader. Where are you doing that? I don't provide these instructions for fun. I provide them because they will solve your problem. If you don't follow the instructions I provide to help you then why am I here?
-
Feb 15th, 2012, 11:27 AM
#7
Thread Starter
Fanatic Member
Re: import text file into access database table
i dont know how to do DataTable from text file..
and the problem is that the whole application close
-
Feb 15th, 2012, 11:40 AM
#8
Thread Starter
Fanatic Member
Re: import text file into access database table
this is my final code and it still the program close when it excute
vb Code:
Imports System.IO
Imports System.Data.OleDb
Public Class Form1
Sub LoadData()
Dim objStreamReader As StreamReader = File.OpenText("C:\Users\BS\Desktop\Messages.txt")
While (objStreamReader.Peek() <> -1)
Dim Str1() As String = Split(objStreamReader.ReadLine(), ",")
AddRecord(Str1(0), Str1(1), Str1(2))
End While
objStreamReader.Close()
End Sub
Sub AddRecord(ByVal Field1 As String, ByVal field2 As String, ByVal field3 As String)
OpenFileDialog1.ShowDialog()
Dim filepath As String = OpenFileDialog1.FileName
Dim objConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath & ";user id=admin;jet oledb:database password=123")
Dim saveinto As New OleDb.OleDbCommand
saveinto.Connection = objConnection
saveinto.CommandType = CommandType.Text
saveinto.CommandText = " update Messages set Label= '" & field2.ToString & "' ,message='" & field3.ToString & "' WHERE Index = " & Field1.ToString()
objConnection.Open()
saveinto.ExecuteNonQuery()
objConnection.Close()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.LoadData()
End Sub
End Class
-
Feb 15th, 2012, 04:37 PM
#9
Thread Starter
Fanatic Member
Re: import text file into access database table
ok i got the code working now
ther's just few things i think it wont be hard how can i get rid of the quotes of the text ""
0,"first text"," "
also if the text file contain unicode caracters like arabic or korean words it change to questions mark how to fix this
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|