running32
asked on
bcp from vb.net app to sql
I want to take a csv file and import it into a table called upload in sql. How can I do this from a vb.net app please.
thank you
thank you
ASKER
What I would like to do is take a csv file and import the contents into a sql table called upload from a vb application.
I would need to delete any exiting data in the upload table and then populate with the new data from the file.
Thanks
I would need to delete any exiting data in the upload table and then populate with the new data from the file.
Thanks
Two ways
(Easier)
Create a DTS Package to do the job and either store it on the database server or embed it in your application. Microsoft supplies a CSV ODBC driver
so you can open the CSV as simply another table. Very easy. Then in VB.Net just tell SQL-Server/MSDE to run the job
(Harder)
Again, pointing out CSV files can be opened as Database objects, use ADO.NET to open the CSV file, then loop through the CSV table
taking each row and using it to fill a prepared statement. Any fixes can be done as each row is read.
Cheers
-Bill
(Easier)
Create a DTS Package to do the job and either store it on the database server or embed it in your application. Microsoft supplies a CSV ODBC driver
so you can open the CSV as simply another table. Very easy. Then in VB.Net just tell SQL-Server/MSDE to run the job
(Harder)
Again, pointing out CSV files can be opened as Database objects, use ADO.NET to open the CSV file, then loop through the CSV table
taking each row and using it to fill a prepared statement. Any fixes can be done as each row is read.
Cheers
-Bill
ASKER
thanks, the user needs to upload the file so I have a variable file name. Here is what I have so far
****************vb.net *******************
Dim ExcelObj = New Excel.Application
Dim currentfilename
Me.OpenFileDialog1.FileNam e = "*.csv"
If (Me.OpenFileDialog1.ShowDi alog() = DialogResult.OK) Then
currentfilename = OpenFileDialog1.FileName
'pass currentfilename to stored procedure and upload csv file to upload table in sql
Dim objCmd As New SqlCommand
Dim objDR As SqlDataReader
Connection1.Open()
'Upload current file name to tblUpload on biz-talk healthdistrict db.
objCmd.Connection = Connection1
objCmd.CommandType = CommandType.StoredProcedur e
objCmd.CommandText = "usp_Update_Emp11"
objCmd.Parameters.Add(New SqlParameter("@FILE_NAME", SqlDbType.Text, 20))
objCmd.Parameters("@FILE_N AME").Valu e = currentfilename
objCmd.ExecuteNonQuery()
Console.WriteLine("Bulk inserted")
Connection1.Open()
***************sql******** ********** ***
CREATE PROCEDURE usp_Update_Emp11
@FILE_NAME AS VARCHAR(70)
AS
Delete from upload
EXEC('BULK INSERT upload FROM '''+@FILE_NAME+''' WITH (FIELDTERMINATOR = '','', ROWTERMINATOR = ''\n'')')
GO
usp_update_emp11
****************vb.net *******************
Dim ExcelObj = New Excel.Application
Dim currentfilename
Me.OpenFileDialog1.FileNam
If (Me.OpenFileDialog1.ShowDi
currentfilename = OpenFileDialog1.FileName
'pass currentfilename to stored procedure and upload csv file to upload table in sql
Dim objCmd As New SqlCommand
Dim objDR As SqlDataReader
Connection1.Open()
'Upload current file name to tblUpload on biz-talk healthdistrict db.
objCmd.Connection = Connection1
objCmd.CommandType = CommandType.StoredProcedur
objCmd.CommandText = "usp_Update_Emp11"
objCmd.Parameters.Add(New SqlParameter("@FILE_NAME",
objCmd.Parameters("@FILE_N
objCmd.ExecuteNonQuery()
Console.WriteLine("Bulk inserted")
Connection1.Open()
***************sql********
CREATE PROCEDURE usp_Update_Emp11
@FILE_NAME AS VARCHAR(70)
AS
Delete from upload
EXEC('BULK INSERT upload FROM '''+@FILE_NAME+''' WITH (FIELDTERMINATOR = '','', ROWTERMINATOR = ''\n'')')
GO
usp_update_emp11
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It will not let me upload because the data is stored as below and the lastname, firstname are together. Therefore I get an error on the dob. thank you
Cxxxxx, Lxxx 11-02-48 58 S1111-02 1-Feb-06 d22 mfc 203.3 12455 124-42-1245 f 401 Taaddd Dr xxxxx Cxxxxx xx 8xxxx XX
Cxxxxx, Lxxx 11-02-48 58 S1111-02 1-Feb-06 d22 mfc 203.3 12455 124-42-1245 f 401 Taaddd Dr xxxxx Cxxxxx xx 8xxxx XX
ASKER
I can upload it if ok now if i create a fname lastname but I get " around the name" . How can I avoid this.
Thanks
Thanks
Can you paste a sample row?
Also, that looks tab separated, not comma, I would look at either cleaning up the "tsv" file first or transform it during the load. One way would be to make the first column
fullname, then use a stored proc or query update to split the first and last names.
Cheers,
-Bill
Also, that looks tab separated, not comma, I would look at either cleaning up the "tsv" file first or transform it during the load. One way would be to make the first column
fullname, then use a stored proc or query update to split the first and last names.
Cheers,
-Bill
Or are you wanting to just read the CSV, and map to SQL Insert statements, called over and over again?