Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I'm calling a Stored Procedure say 'GetUserDetails' which retrieves say 3 rows with 3 columns [3X3] with SqlCommand and this is stored in to a DataSet.

Now I wanted to loop through each row from the DataSet and write each row to a separate text file, I mean the final outcome should contain 3 text file which contains only a row of data from the dataset and save it in some location.

Can anyone help at it? Thanks.
Posted
Comments
Maciej Los 29-Apr-15 1:32am    
What have you tried? Where are you stuck?

1 solution

HI Sathish

basically you are not supposed to ask the complete code directly, you shall try something and if u face any difficulty in that approach you can post the same here.
asking the code is not encouraged in this forum. :)
for now you can try the below .

C#
string folderLocation = @"D:\CodeProject\temp"; // your path to save the files
           // code to fetch the data from DB and store it in dataset/datatable
           // assume the datatable as below , which has data in 3X3

           DataTable dt = new DataTable();
           dt.Columns.AddRange(new DataColumn[] { new DataColumn("Column1"), new DataColumn("Column2"), new DataColumn("Column3") });
           dt.Rows.Add("Row-1-Column-1", "Row-1-Column-2", "Row-1-Column-3");
           dt.Rows.Add("Row-2-Column-1", "Row-2-Column-2", "Row-2-Column-3");
           dt.Rows.Add("Row-3-Column-1", "Row-3-Column-2", "Row-3-Column-3");


           for (int i = 0; i < dt.Rows.Count; i++)  // iterate each rows to get the value
           {
               string fileName = folderLocation + "\\" +  "File-" + i + ".txt";  // file name for saving each row
               string contents = "";
               foreach (DataColumn col in dt.Columns)  // iteate each column to get its corresponding value
                   contents += dt.Rows[i][col].ToString() + "    ";  // concatenating the contents

               System.IO.File.WriteAllText(fileName, contents);  // create a text file and copy the contents
           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900