954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Upload txt data to MS SQL Database (Using C# and ASP.NET)

Hi, it's me agian... I was asked to do reasearch on how to upload a txt file's data into a MS SQL Database (corresponding columns etc.) I've searched a bit on the internet but don't find what i'm looking for... I just want to ask does anyone have an idea of where i can go and look for information on this. (I'm still very new to C# and ASP.NET programming) (It's so that i can present this topic in class and implement it in my class project) I would really appreciate any suggestions, and i thank you in advance!

phoenix_dwarf
Junior Poster in Training
57 posts since May 2009
Reputation Points: 11
Solved Threads: 0
 
vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 

Lol tnx i just got that page before i saw your reply tnx!

phoenix_dwarf
Junior Poster in Training
57 posts since May 2009
Reputation Points: 11
Solved Threads: 0
 

Am glad you got your Solution . Mark your Post as Resolved

Kind Regards

Vuyiswa Maseko

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 

i still dont think its solved... XD

phoenix911
Junior Poster
170 posts since May 2009
Reputation Points: 38
Solved Threads: 21
 

What is the Problem ?

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 

Oky i've got it partially working now. My only question now is will i be able to have a autonumber column aswell in the database table so that the autonumber keeps on adding numbers as i upload my data into the table? (i hope so but i know that it will most probably only work with an insert statement? right or wrong?)

I saw that to skip the first row you can simply say FIRSTROW = 2 and so i tried FIRSTCOLUMN = 2 but doesn't work. Here is my code:

BULK INSERT dbo.testTBLUpload 
FROM 'C:\Users\GOgGA\Desktop\upload1.txt'
WITH (
		FIRSTROW = 2,
		FIELDTERMINATOR = '|',
		ROWTERMINATOR = '\n'
	 )
phoenix_dwarf
Junior Poster in Training
57 posts since May 2009
Reputation Points: 11
Solved Threads: 0
 

problem stated above :D

phoenix911
Junior Poster
170 posts since May 2009
Reputation Points: 38
Solved Threads: 21
 

O snd sorry for this little bit of SQL code in the C# section on the forum. But this is for my C# class so yea!

phoenix_dwarf
Junior Poster in Training
57 posts since May 2009
Reputation Points: 11
Solved Threads: 0
 

Remove the First Column and let SQl add it for you as an identity Column.

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 
Remove the First Column and let SQl add it for you as an identity Column.


hmmm that actually sounds the obvious ... but there wasnt a column in the textfile which has numbers for the autonumber field

phoenix911
Junior Poster
170 posts since May 2009
Reputation Points: 38
Solved Threads: 21
 

Can you Post your TextFile and a Create Script of your SQl Table and i will give you an Exact code to write.

kind Regards

Vuyiswa Maseko

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 

itll be posted now, give a sec plz...

and thanx

phoenix911
Junior Poster
170 posts since May 2009
Reputation Points: 38
Solved Threads: 21
 
vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 
CREATE TABLE mytable
(
   /*id INT IDENTITY(1,1) PRIMARY KEY,*/
   Fname text,
   Surname text,
   Tel text,
   Salary DECIMAL(18,2)
);


text file:
Hein|Oosthuyzen|0123313470|15489.62
Johan|du Toit|0126547896|13240.27
Nico|Fourie|0124784532|14500.20
Shaun|Venter|0123546987|16320.12
Burnabe|Jordaan|0124872134|12480.80

phoenix_dwarf
Junior Poster in Training
57 posts since May 2009
Reputation Points: 11
Solved Threads: 0
 

Here is your Solution

SqlConnection con =new SqlConnection(@"User id=sa;Password=oops;Server=VUYISWA\SQLEXPRESS;Database=Vuyiswa");
        
        SqlCommand cmdinsert = new SqlCommand();
        
        cmdinsert.CommandText=@"BULK INSERT mytable FROM 'C:\WebSite29\Data.txt' WITH (FIELDTERMINATOR = '|', ROWTERMINATOR = '\n' );";

        cmdinsert.CommandTimeout = 0;

        cmdinsert.CommandType= CommandType.Text;

        cmdinsert.Connection = con;

        try
        {
            con.Open();

            cmdinsert.ExecuteNonQuery();

        }
        catch(SqlException ex)
        {

            Label1.Text = ex.Message.ToString();
        }   
        finally
        {
            if(con != null)
            {
                con.Close();
            }
        }


Kind Regards

Vuyiswa

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 

tnx i'll try it out and let you know!

phoenix_dwarf
Junior Poster in Training
57 posts since May 2009
Reputation Points: 11
Solved Threads: 0
 

No Problem and dont Forget to mark the post as resolved and give me my reputation. am going home now enjoy your weekend

Kind Regards

Vuyiswa Maseko

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 

Sorry i thought it worked but i see now that i still can't have a autonumber column in my database...? the bulk insert works from the first column onwards and i need to have the autonumber column...Any further suggestions?

phoenix_dwarf
Junior Poster in Training
57 posts since May 2009
Reputation Points: 11
Solved Threads: 0
 
Sorry i thought it worked but i see now that i still can't have a autonumber column in my database...? the bulk insert works from the first column onwards and i need to have the autonumber column...Any further suggestions?

Seems to me you could add/insert an autonumber column after the bulk insert completes: How to: Create an Autonumber DataColumn

DdoubleD
Posting Shark
996 posts since Jul 2009
Reputation Points: 341
Solved Threads: 233
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: