0

I am trying to get a table from my local database to download into a .txt. I've and I'm honestly at a loss.

It has to be in a txt for simplicity of other users. I've tried a few different ways, I've tried to call it as if I was writing it to a datagrid, which didn't work.

I am getting results from the Reader @

while (dbReader.Read())
                {
                    MessageBox.Show(dbReader.GetValue(0).ToString());
                }

So it is pulling and reading the information, it's just not writing it to a txt file. Here's the code.

 private void btnDLSql_Click(object sender, RoutedEventArgs e)
        {
            string dbConnection = "datasource=127.0.0.1;port=3306;username=root;password=12345";
            MySqlConnection conDataBase = new MySqlConnection(dbConnection);
            MySqlCommand SelectCommand = new MySqlCommand("select GUID,BanTime,Reason from bans.bans order by BanType asc", conDataBase);

            conDataBase.Open();

            MySqlCommand command = conDataBase.CreateCommand();
            MySqlDataReader dbReader = SelectCommand.ExecuteReader();

            if (dbReader != null)
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\mccla\Desktop\A3Bans.txt", true);

                while (dbReader.Read())
                {
                    MessageBox.Show(dbReader.GetValue(0).ToString());
                }
            }

            conDataBase.Close();

        }

Any help would be greatly appreciated. Sorry for the questions, I usually try and figure stuff out on my own. But just can't find any information for this.

Mokey
  • 215
  • 1
  • 15
  • Have you tried putting `file.Write(dbReader.GetValue(0).ToString());` instead of the message box show? (Also, are you closing the file?) – Andy M Oct 27 '16 at 16:18
  • 1
    If you want to write a file, try writing some code that actually puts things in the file. – SLaks Oct 27 '16 at 16:18
  • `dbReader` can never be `null`. – SLaks Oct 27 '16 at 16:18
  • You should use `using` statements. – SLaks Oct 27 '16 at 16:18
  • Thanks @SLaks I've only been learning c# for 3 days... and I only know bits and pieces of code from simple stuff.. so this is a big project, any pointers are always appreciated. – Mokey Oct 27 '16 at 17:36
  • Yup.. needed to close file... smh, i thought I had it too.. Not I just gotta figure out the formatting. Thanks Gents. – Mokey Oct 27 '16 at 17:48

1 Answers1

1

You currently aren't writing to your file or closing it when you are done. You should also be using using statements, as mentioned by SLaks in the comments.

You could probably simplify your code to:

        var dbConnection = "datasource=127.0.0.1;port=3306;username=root;password=12345";
        using (var conDataBase = new MySqlConnection(dbConnection))
        using (var selectCommand = new MySqlCommand("select GUID,BanTime,Reason from bans.bans order by BanType asc", conDataBase)) {
            var dbReader = selectCommand.ExecuteReader();
            using (var file = new System.IO.StreamWriter(@"C:\Users\mccla\Desktop\A3Bans.txt", true)) {
                while (dbReader.Read()) {
                    file.Write(dbReader.GetValue(0).ToString());
                }
            }
        }
Andy M
  • 596
  • 3
  • 7
  • Thanks, I will start looking at ways to simplify, as I said in the comments above, C# is the 1st language I'm really getting into besides a few small project. – Mokey Oct 27 '16 at 17:50