Click here to Skip to main content
15,888,148 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SqlConnection conn = new SqlConnection("ConnectionString"))
            {

                SqlCommand sqlComm = new SqlCommand("Test", conn);

                sqlComm.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(sqlComm);
                DataSet ds = new DataSet();
                da.Fill(ds);
            }
        }

    }
}
Posted
Updated 2-Aug-15 21:24pm
v8

1. Try yourself first, if you have proper connection string and stored procedure which returning multiple tables, you will get DataSet with multiple tables.

2. DataSet can have multiple DataTables, if your stored procedure returning multiple tables, then you can access each table by using your dataset Tables property. You can save DataTable to CSV file, just google for it, you will find many solutions and related code samples.

3. you can iterate all the Datatables in the dataset and call each datatable with export to CSV code which you find on step 2.

4. when you save csv file, give the name with date time stamp, check Append TimeStamp to a File Name[^]
 
Share this answer
 
Comments
aarif moh shaikh 3-Aug-15 6:20am    
Agree with you..5
To answer if you can create a CSV with the code above, no. You need a lot more coding. Have a look at C# - CSV Import Export[^]. It's a very nice example how to create a CSV from data in SQL Server. Pay attention to exportToCSVfile method.
 
Share this answer
 
Comments
Member 11878313 1-Aug-15 9:29am    
Sorry, I updated my questions. I wasn't clear with my questions previously.
Wendelius 1-Aug-15 10:25am    
1. if the execution returns several result sets, the dataset will contain several datatables
2. the data table is created by the adapter
3. loop through the tables, columns and rows and write them to file/files
4. string filename = "log" + System.DateTime.Now.Ticks.ToString();

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