47 lines
1.4 KiB
C#
47 lines
1.4 KiB
C#
using MySql.Data.MySqlClient;
|
|
using System;
|
|
using System.Data;
|
|
|
|
namespace DatabaseTest
|
|
{
|
|
class Program
|
|
{
|
|
private const string SERVER_ADDRESS = "127.0.0.1";
|
|
private const string SERVER_PORT = "3306";
|
|
private const string DATABASE_NAME = "testdb";
|
|
private const string USERNAME = "testuser";
|
|
private const string PASSWORD = "test123";
|
|
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
string connectionStr = $"Server={SERVER_ADDRESS};Port={SERVER_PORT};Database={DATABASE_NAME};Uid={USERNAME};Pwd={PASSWORD}";
|
|
|
|
MySqlConnection con = new MySqlConnection();
|
|
con.ConnectionString = connectionStr;
|
|
|
|
try
|
|
{
|
|
con.Open();
|
|
|
|
MySqlCommand cmd = con.CreateCommand();
|
|
cmd.CommandText = "SELECT * FROM testtable";
|
|
var reader = cmd.ExecuteReader();
|
|
DataTable dt = new DataTable();
|
|
dt.Load(reader);
|
|
|
|
con.Close();
|
|
}
|
|
catch (MySqlException sqlEx)
|
|
{
|
|
Console.WriteLine(sqlEx.Message);
|
|
Console.WriteLine();
|
|
Console.WriteLine(sqlEx.InnerException?.Message);
|
|
}
|
|
|
|
Console.WriteLine("Press any key to close.");
|
|
Console.ReadKey();
|
|
}
|
|
}
|
|
}
|