Skip to content

Instantly share code, notes, and snippets.

@0V
Created August 11, 2014 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0V/5d4f8c85c2513f7f5e21 to your computer and use it in GitHub Desktop.
Save 0V/5d4f8c85c2513f7f5e21 to your computer and use it in GitHub Desktop.
using MySql.Data.MySqlClient;
using System;
using System.Data;
namespace ConnectMysqlSample
{
class Program
{
static void Main(string[] args)
{
string server = "localhost";
string database = "sample_db";
string user = "root";
string pass = "";
string myConnectionString = string.Format(
"Server={0};Database={1};Uid={2};Pwd={3}",
server,
database,
user,
pass);
using (var connection = new MySqlConnection(myConnectionString))
{
try
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = "Select * From sample_table";
var dataAdapter = new MySqlDataAdapter(cmd);
var dataSet = new DataSet();
dataAdapter.Fill(dataSet);
Console.WriteLine(dataSet.GetXml());
connection.Close();
}
catch (Exception e)
{
Console.WriteLine("Error : " + e.Message);
}
Console.ReadLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment