title: Read from a SQL database and output result to a file
body: <p>Afternoon,</p>
  
  <p>I’m trying to code a form that will run a query on one of my databases and put the result in a text file. </p>
  
  <p>But am not able to accomplish the output of the files. Can someone tell me how I can output the file? I think i am missing a streamwriter but I am not sure how to do it.</p>
  
  <p>Kindly advise.</p>
  
  <p>A</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Data.SqlClient;
  using System.Diagnostics;
  using System.Drawing;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  
  namespace SwiftMSGDownload
  {
    public partial class Form1 : Form
    {
      string RdSqlMsg;
      string RdSqlRef;
      string RdSqlType;
      string RdSqlTime;
      string MSGRootPath;
  
      public Form1()
      {
          InitializeComponent();
      }
  
      private void subGetMSGs_Click()
      {                      
          string Sql = null;
  
          Sql = "SELECT [FileNumber] " + " ,[Reference] " + " ,[SenderRefID] " + " ,[Amount] " + " ,[Currency] " + " ,[Type] " + " ,[DateTransfer] " + " ,[Time] " + " ,[Msg] " + " ,[Direction] " + " ,[ProcessBy] " + " ,[SenderID] " + " ,[ReceiverID] " + " FROM  [TransferFilesIn] " + " WHERE  Time BETWEEN '" + txtLastTimeProcessed.Text + "' AND '" + DateTime.Now + "'" + " AND Type IN (199,103) " + " AND ReceiverID = 'ABCDEFXXX' ";
  
          try
          {
              string strConnectionString = null;
              strConnectionString = "Data Source=180.123.45.6;Password=thepassword;User ID=theuser;Initial Catalog=thedatabase;Persist Security Info = True";
              txtSwiftDBConnectionString.Text = strConnectionString;
  
              using (SqlConnection connObj = new SqlConnection(strConnectionString))
              {
                  using (SqlCommand cmdObj = new SqlCommand(Sql, connObj))
                  {
                      connObj.Open();
  
                      using (SqlDataReader readerObj = cmdObj.ExecuteReader())
                      {
                          //Loops through all returned records.
                          while (readerObj.Read())
                          {
                              RdSqlMsg = readerObj["Msg"].ToString();
                              RdSqlRef = readerObj["Reference"].ToString();
                              RdSqlType = readerObj["Type"].ToString();
                              RdSqlTime = readerObj["Time"].ToString();
  
                              RdSqlTime = RdSqlTime.Replace(":", "-");
                              RdSqlTime = RdSqlTime.Replace("/", "-");
  
                             string strFilePath = MSGRootPath + "\\" + RdSqlTime + "_" + RdSqlRef + "_" + RdSqlType + ".txt";
                          }
                      }
  
                      //Close the connection
                      connObj.Close();
                    }
                }
           }
           catch (Exception ex) 
           {
              MessageBox.Show("subGetMSGs_Click - ERROR: " + Environment.NewLine + ex.Message);
              return;
          }
      }
  
      private void Form1_Load(object sender, EventArgs e)
      {
         MSGRootPath = "\\\\180.987.65.43\\Public\\Test";
      }
  
      private void btnStart_Click(object sender, EventArgs e)
      {
          subGetMSGs_Click();
      }
  }
  </code></pre>