You can read those link to see how to get data from my sql in c#
Click
Click
After get the data from the database, you can easily write records to text file.
string OutputFile = "a.txt";
string output = "1\n2\n3";
File.WriteAllText(OutputFile, output);
You can do this operations in the button click event.