1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
   |  public void BackupCSV()
        {
            string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
            string LogFolder = @"C:\Log\";
            try
            {
 
                //Declare Variables and provide values
                string FileNamePart = "DATAFromDGV";//Datetime will be added to it
                string DestinationFolder = @"D:\bdd";
                string TableName = "Dbo.DatafromDGV, Dbo.Calculation";
                string FileDelimiter = ","; //You can provide comma or pipe or whatever you like
                string FileExtension = ".Csv"; //Provide the extension you like such as .txt or .csv
 
 
                //Create Connection to SQL Server in which you like to load files
                SqlConnection SQLConnection = new SqlConnection(TheConnectionString);
               // SQLConnection.ConnectionString = "Data Source = (local); Initial Catalog =TechBrothersIT; "
                   //+ "Integrated Security=true;";
 
                //Read data from table or view to data table
                string query = "Select * From " + TableName;
                SqlCommand cmd = new SqlCommand(query, SQLConnection);
                SQLConnection.Open();
                DataTable d_table = new DataTable();
                d_table.Load(cmd.ExecuteReader());
                SQLConnection.Close();
 
                //Prepare the file path 
                string FileFullPath = DestinationFolder + "\\" + FileNamePart + "_" + datetime + FileExtension;
 
                StreamWriter sw = null;
                sw = new StreamWriter(FileFullPath, false);
 
                // Write the Header Row to File
                int ColumnCount = d_table.Columns.Count;
                for (int ic = 0; ic < ColumnCount; ic++)
                {
                    sw.Write(d_table.Columns[ic]);
                    if (ic < ColumnCount - 1)
                    {
                        sw.Write(FileDelimiter);
                    }
                }
                sw.Write(sw.NewLine);
 
                // Write All Rows to the File
                foreach (DataRow dr in d_table.Rows)
                {
                    for (int ir = 0; ir < ColumnCount; ir++)
                    {
                        if (!Convert.IsDBNull(dr[ir]))
                        {
                            sw.Write(dr[ir].ToString());
                        }
                        if (ir < ColumnCount - 1)
                        {
                            sw.Write(FileDelimiter);
                        }
                    }
                    sw.Write(sw.NewLine);
 
                }
 
                sw.Close();
 
            }
            catch (Exception exception)
            {
                // Create Log File for Errors
                using (StreamWriter sw = File.CreateText(LogFolder
                    + "\\" + "ErrorLog_" + datetime + ".log"))
                {
                    sw.WriteLine(exception.ToString());
 
                }
 
            }
        } |