Esempio n. 1
0
        private void Question1()
        {
            try
            {
                SqlParameter[] parameters = new SqlParameter[1];
                parameters[0] = new SqlParameter("@FileName", txtFileName.Text);

                DataSet ds = SQLHelper.GetDataFromStoredProcedure("spQuestion1", parameters);

                if (ds.Tables.Count == 0)
                {
                    MessageBox.Show("There is no data available to be written to the resulting text file", "No Data", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                    return;
                }

                StringBuilder sb = new StringBuilder();

                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    sb.AppendLine(String.Format("{0}\t{1}", row[0], row[1]));
                }

                string fileName = String.Format("{0}\\Question1 {1}.txt", FileIOHelper.ExtractPathFromFullFileName(txtFileName.Text), DateTime.Now.ToString("yyyy-MM-dd HHmmss"));

                string result = sb.ToString();

                File.WriteAllText(fileName, result, ASCIIEncoding.ASCII);

                if (MessageBox.Show(String.Format("A new text file has been created at {0}. Would you like to view it now?", FileIOHelper.ExtractPathFromFullFileName(txtFileName.Text)), "New File Created", MessageBoxButton.YesNo, MessageBoxImage.Asterisk) == MessageBoxResult.Yes)
                {
                    System.Diagnostics.Process.Start(fileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }