Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8588697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:52:00+00:00 2026-06-11T22:52:00+00:00

I want to write a MySql statement that will connect to the database, select

  • 0

I want to write a MySql statement that will connect to the database, select a column from the table, then output that data to a text file to a specific location on my computer. I have searched the internet for a couple days now and don’t seem to find the answer I am looking for. I am fairly new to c#, MySql, and Visual Studio. I am just trying to learn how to write the correct statements and get the desired result. Any help would be greatly appreciated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using MySql.Data;
using System.Windows.Forms;
using System.IO;

namespace NewPractice
{
    public class Connect
    {
        static void Main()
        {
            string results = @"server=111.111.11.111; userid=anyone;
                password=anypassword; database=anydatabase";    
            MySqlConnection conn = null;

            try
            {
                conn = new MySqlConnection(results);
                conn.Open();
                //Console.WriteLine(
                File.WriteAllLines(
                    @"C:\Documents and Settings\anyone\My Documents\Tests\testoutput.txt", 
                    results.ToArray());
            }
            catch (MySqlException ex)
            {
                Console.WriteLine("Error: (0)", ex.ToString());
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }      
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:52:02+00:00Added an answer on June 11, 2026 at 10:52 pm

    You’re writing the contents of the result string to the file, not the data you’re attempting to select. You need to run a sql command and get a SqlDataReader object to write your data to the file.

    string results = @"server=111.111.11.111; userid=anyone; 
        password=anypassword; database=anydatabase";
    
    MySqlConnection connection = new MySqlConnection(results);
    MySqlCommand command = connection.CreateCommand();
    MySqlDataReader reader;
    command.CommandText = "select * from mycustomers";
    connection.Open();
    reader = command.ExecuteReader();
    using(var sw = new StreamWriter("C:\MyPath\MyFile.txt"))
    {
        while (reader.Read())
        {
            var row = (IDataRecord)reader;
            sw.WriteLine(row["myColumn"]);
        }
    }
    connection.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a Mysql statement that selects all from a table(posting) where
I want write a simple query which will fetch data from a table (which
I want to write a mysql statement which will return a list of results
I'm trying to write a sql statement that will insert data given a few
I have MySQL database server I need to update and retrieve data to/from MySQL
I want to write a new application that will use a DBMS. In the
I want to write mysql when statement and i can't do it. explanation =>
I have a method that transfers data from one database to another and in
Since I want to include a session variable in MySql select statement, I want
I'd like to write the following as a MySQL SELECT statement to cut down

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.