inserting data to text file from database and inserting data back to database from text file

Hello friends....

I am looking for 2 things(using c#.net or vb.net and sql svr 2000)

1.convert data from sql server 2000 database (say customers table from northwinds database) to a text file(separated by commas or just plain space)


2.Insert the data from text file back to database.


Can someone pls give me the detailed code to achieve this....really need this on urgent basis.......Thank You.


Gurjeet,England
0
gurjeetsaini
8/7/2007 6:04:48 PM
asp.net.sql-datasource 29906 articles. 0 followers. Follow

10 Replies
2061 Views

Similar Articles

[PageSpeed] 43

As for detailed code, I won't give you :P however I can tell you one way of doing this (there are multiple ways).

The way I'd do it would be simple:

  1. Retrieve the information from the DataBase as a DataTable.
  2. loop through each row in the DataTable.Rows collection and write out the values as CSV.

So it will look like this:

...Do all your code to Fill the DataTable.

DataTable t = new DataTable();
            string s = string.Empty;
            foreach (DataRow r in t.Rows)
            {
                s += r["MyRow1"].ToString() + ", ";
                s += r["MyRow2"].ToString() + ", ";
                s += r["MyRow3"].ToString() + ", ";
                s += r["MyRow4"].ToString() + ", ";
                s += r["MyRow5"].ToString()
                s += "\n";

            }

 

then at the end, remove the last "," from the string.

 once you have that, do a simple Insert into the Database.
 


The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

My Site | My Examples | My Blog


0
RTernier
8/7/2007 6:25:24 PM

Hi RTernier,Many thanks for the code BUT I need ur further help.pls dont mind...

1.how to write the value of string "s" to csv file.

2.also can u pls tell me how to write the data from csv file back to database(pls give as many details as possible mate)

 

Your help is  much appreciated...........


Gurjeet,England
0
gurjeetsaini
8/7/2007 7:02:15 PM

Look up how to Save a TextFile to the harddisk. What you will do is take that string, and save it as a regular file using the System.IO namespace.



You want to write it to the DataBase. Do you mean save the contents of the file to one specific cell in the database? Or create a new table based on the values of the CSV? 


The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

My Site | My Examples | My Blog


0
RTernier
8/7/2007 7:13:35 PM

Hi RTernier. again many thanks for da help.....my qn is that how to save the contents of csv file to a empty table in database that pre-exists in database . say csv file has five columns and that empty table in databse also has 5 columns....i need to save all the data of the 5 columns in csv file to the corresponding 5 columns in table in database....pls help


Gurjeet,England
0
gurjeetsaini
8/7/2007 7:22:15 PM

 Check this link out:

http://www.codeproject.com/cs/database/FinalCSVReader.asp 


The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

My Site | My Examples | My Blog


0
RTernier
8/7/2007 7:29:59 PM

Hi Rternier that was gr8...

again 1 qn remains and it is pls give me more details on how to save that string "s" data to csv file...pls write some code lines to demonstrate.....thank you.


Gurjeet,England
0
gurjeetsaini
8/7/2007 7:41:17 PM

 

FileStream fsOutput = 
new FileStream (txtCSVFolderPath.Text+"\\test.csv",
FileMode.Create, FileAccess.Write);
StreamWriter srOutput = new StreamWriter (fsOutput);
srOutput.WriteLine("This is some text");
srOutput.Close ();
fsOutput.Close ();
 


The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

My Site | My Examples | My Blog


0
RTernier
8/7/2007 7:53:21 PM

Hi Rternier.....many  many thanks for your kind help....i relally appreciate it.

my query is fully resolved....

Can u clear my doubts in future if i send u email? and if possible pls give me ur email id.....my email id is gurjeetsaini@gmail.com        waiiting 4ur response......

 

again many thanks mate...keep it up.....ta


Gurjeet,England
0
gurjeetsaini
8/7/2007 8:04:14 PM

I don't like sticking my email on forums due to readers, you can find it on my website, I'll also send you an email so you have it. 


The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

My Site | My Examples | My Blog


0
RTernier
8/7/2007 8:09:03 PM

you are a star.....Many thanks.....


Gurjeet,England
0
gurjeetsaini
8/7/2007 8:13:42 PM
Reply:

Similar Artilces:

trying to insert data to the database by reading it from a text file?
Hello everyone, I have a text file that have on each line a record and I already did the reading from this file and assigned it to a value this is a part of my code : so now i'm trying to insert these values to the database ...please any hint or example of code to help through this   private void AssignValues(string PrtRrd) { if (PrtRrd.Length != 400) { Initialize(); } else { _TESTFILL = PrtRrd.Substring(0, 6); _Zip05 = PrtRrd.Substring(6, 5); _Zip04 = PrtRrd.Substring(11...

Calling data from database and declaring variable to insert the data back into another table in database
Hello, I have called data from a database which is displayed on the screen (displays a particular item - then if the product is liked they click a buy button) then on the postback I declare the variables (as below) to be added back to a different table in my database, I have a problem in that it doesn't like the way I have declared the variable in bold (below is some of the code).  The variable is initially displayed from the database as decPrice and added to another table as decCartPrice on postback.  I get this error BC30469: Reference to a non-shared member requir...

Insert data from XML file into SQL Server database
Please this is very urgent!I need to import the data from an XML file into a database in Sql Server. This has to be programmatically. I really need some help Thanks a lot,Michael. http://www.dotnetjunkies.com/quickstart/aspplus/doc/webdataaccess.aspx#xmldata Use the dataset to read the xml, then update the dataset into a data adapter. http://support.microsoft.com/default.aspx?scid=kb;en-us;308055Brian"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4). Thank yo...

java script that prompts the user to Insert a name(Text) and Inserts Data to Database
Hi Like to know the javaScript method() that prompts user to Enter a name, after entering the Text(name) clicking Ok button in prompt method() will call (execute) my method I have written to insert data into Database. Technologies used: Visual Studio 2005 with ASP.NET 2.0, Oracle 9i, C# (ofcourse it doesn't matter with Technologies)  Regards,-- "Mark As Answer" if my reply helped you --  var username = prompt("Please Enter your Name:", "defaultValue");  I never lose, some people are just better than me at winning. Than...

Uploading data to sql server 2005 express database from a text file
Hi, (1)  Is there any free tool to Upload data to sql server 2005 express database from a text file? (2) Is there any free export / import data tool for sql server 2005 express? Regards,Sandyhttp://www.thequinn.infohttp://www.sksdataservices.comhttp://www.infobasket.info...

Inserting data to database with text box
 Hi i have a database table named countrycapital having two columns country and capital I want to insert new countries and their capitals using two textboxes named as txtcountry and txtcapital when i click the update button it will insert in the table the new country and capitalCode C# and asp.net 1 protected UpdateButton_Click(object sender, eventargs e) 2 { 3 string strCountry = txtcountry.Text; 4 string strCapital = txtcapital.Text; 5 //Pseudo code 6 //Create a database connection 7 //Create a command object with your insert statement...

Insert text file content into database
Hello everyone!I'm having a problem with inserting the content of a text file into a Sql Server 2005 database.I'm reading the text file into a dataset, and works fine. What I can't do is what I suspect is the simple part: Insert all the data into a table that has exactly the same configuration that the file. I've never worked with dataset's before, and I can't seem to find the answer to this!This is what I have done so far:  Dim i2 As Integer Dim j As Integer Dim File As String = Server.MapPath("..\Docs\Facts\FORM_MAN_V3_1.txt") ...

How to export database data to text file?
Dear all, I am new in PowerBuilder. I am using PB 7.03. I don't know how to export database data to text file with pre-defined format? What kind of function I have to use? Regards, newuser Have you examined dwcontrol.SaveAsAscii() and/or dwcontrol.SaveAs()? ---------------------------------- John Strano [TeamSybase] In article <AyO6UsFEBHA.203@forums.sybase.com>, mukwood@hongkong.com says... > Dear all, > > I am new in PowerBuilder. I am using PB 7.03. I don't know how to export > database data to text file with pre-defined format?...

INSERT CONTENTS OF TEXT FILE INTO DATABASE
HI ALL, AM NEWBIE TO ASP.NET,  I WANT TO ALLOW THE USER TO UPLOAD A TEXT FILE FROM ANY CLIENT IN THE NETWORK TO THE SERVER AND THEN INSERT THE CONTENTS OF THIS FILE INTO A TABLE IN ORACLE DATABASE, CONTENTS OF THE TEXT FILE LOOKS LIKE (EMPLOYEE ID , NAME , MOBILE NO)  258,JOHN TERRY,5869742 124,JUN MORIS,478596 301,OSK MARY,475 ........  AND IS THERE ANY WAY TO INSERT THE CONTENTS OF THE USER TEXT FILE DIRECTLY INTO THE DATABASE WITHOUT UPLOADING IT TO THE SERVER , PLEASE SEND ME WITH FULL CODE AM A NEWBIE TO ASP.NET Hi, In order to upload the file you can u...

Inserting data from a .txt file to Database
I want to know the code for reading a .txt file present in my system and then insert the text into the database.Eg:The text in .txt file is "FromDate|Todate|Reason for leave"Now i want this text to insert into databasetables with respect to thier fields Srinu Always say which database you are working with, and the language you use.  In the meantime, here are a couple of links:Reading Text files into Access with ASP.NETHow do I load text or csv file data into SQL Server?  Regards Mike [MVP - ASP/ASP.NET]My site reading from file. http://aspnet.4guysfromrolla.com/a...

Insert Data from database into Text Box
I am trying to pull data from a database and have it displayed in a text box. I'm doing this so that the text in the Text Box can be edited and pushed back into the database when it is submitted. I'm a novice and if there is a better way to do this I'm open to suggestions too. Thanks check out this code sample: Editing Data Using a FormDavid WierMCP/ASPInsiderASPNet101.com - where to look first!Control Grouper - easily control properties for multiple controls with one control!Calendar Express - The Best HTML Calendar Generator on the web!(Please 'Mark as Answer' when it appli...

Reading data from text file database
 Hi everyone I have a directory that contains a lot of text files that have data I need to draw from.  I want to know if it is possible to write a program that will read all of the text files in the directory and pull out data and save it to a new textfile. For example: Each text file is formatted this wayColumn1, Column2, Column3"1","xxxx","yyyy""2", "xxxx", "yyyy""3", "XXXX", "yyyy" I want to put all lines that begin with 1 in one text file, all the lines that begin with two i...

insert data from txt file to database
hi i am using sql server 2000 and i want to insert txt file data into sql server table i am using vstr = "BULK INSERT ITEM_MASTER FROM 'c:\ITEM_MASTER.TXT' WITH (FIELDTERMINATOR = '^')"cmd = New SqlCommand(vstr, con) cmd.ExecuteNonQuery() error is ITEM_MASTER.txt does not exists .but it is present in c directory.  Hi   Try this   BULK INSERT tablename        FROM ‘c:\csvtest.txt’            WITH    (     &...

Inserting data from a .txt file to Database
Hello All, I want insert data from text file to oracle database from code behind using c# . Where each line contain data like 0000000128488070046458301149560000010145681000000001100282NBCU HV INTERCO UNIVERSAL EXEC Plz help me !!!!!!!!!!!!Thanks Abhishek Jain You can read the text file using a Streamreader (here is an example below) and then insert the line to one DB table. Further you can extract the fields by their positions to other table fields in other tables.                 ...  ...

Web resources about - inserting data to text file from database and inserting data back to database from text file - asp.net.sql-datasource

Inserting Kate
jurvetson posted a photo: the speaking GPS avatar that we came to love.. more prep photos below... You can hear her voice in the video compilation ...

Inserting the Galaxy Note 5’s S Pen backwards can permanently damage the device
One of the features Samsung highlighted when it unveiled the Galaxy Note 5 was the device's new S Pen slot design. The Note 5 features a spring-loaded ...


Inserting Slavery Into The Climate Debate
Chris Hayes compares the fight against fossil fuels to the abolitionist movement. He states plainly that “there is absolutely no conceivable ...

Samsung smart TVs inserting ads into third-party apps
... playback have pissed off a number of consumers. People with smart TVs from Samsung have been complaining that the electronics maker is inserting ...

Inserting images into Gmail could be so much better
When Google first launched the new compact compose window in Gmail, many of you might have noticed something annoying about inserting images. ...

Gawker tells Steven Crowder to "stop whining, take your licks, and accept that getting hit in the face ...
"... in the middle of an argument between billionaire-funded know-nothing ideologues and people whose livelihoods and stability are being threatened ...

Gmail's Shortcut for Inserting Hyperlinks
... but that doesn't mean Google shouldn't improve them. Many people complained that Gmail's rich text editor doesn't have a shortcut for inserting ...

Why Hollywood Is Inserting These Two Huge Chinese Actresses Into Its Summer Blockbusters
... It shows the enormous potential in China for distributors. We've previously discussed how Hollywood is starting to cater to China by inserting ...

Samsung SmartTVs are inserting ads into movies - Business Insider
Random Pepsi pop-up ads have been appearing when SmartTV users watch content through the device's third-party apps.

Resources last updated: 12/17/2015 11:12:48 AM