Skip to content

Instantly share code, notes, and snippets.

@tps2015gh
Last active August 6, 2019 19:21
Show Gist options
  • Save tps2015gh/98a386580427221a2623a851e3de169c to your computer and use it in GitHub Desktop.
Save tps2015gh/98a386580427221a2623a851e3de169c to your computer and use it in GitHub Desktop.
connect mysql with C# , compile in csc.exe cli
// A Hello World! program in C#.
using System;
using System.Threading.Tasks;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello World!");
// Keep the console window open in debug mode.
Task a = Task.Run(() =>{
Hello.ConnectDb();
});
a.ContinueWith((data)=>{
Console.WriteLine("Continue");
});
// NOTE : THIS ERROR IN ASYNC AWAIT , STILL NOT UNDERSTAND
// LIKE NODE.JS PROMISE . THEN
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
static async void ConnectDb(){
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=127.0.0.1;uid=root;" +
"pwd=mypassword;database=mydb";
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection();
//MySqlCommand cmd = new MySqlCommand();
conn.ConnectionString = myConnectionString;
await Task.Run( () =>
{
Console.WriteLine("Connect DB START ");
conn.Open();
Console.WriteLine("Connect DB OK ");
String queryString = "SELECT * from prov LIMIT 0,5";
MySqlCommand command = new MySqlCommand(queryString, conn);
command.CommandTimeout = 60;
// Setting command timeout to 1 second
command.CommandTimeout = 1;
// try {
//command.ExecuteNonQuery();
MySqlDataReader rd = command.ExecuteReader();
while( rd.Read() ){
String msg = String.Format("prov_code={0} prov_name={1}" ,rd[0],rd[1]);
Console.WriteLine(msg );
}
// }
// catch (SqlException e) {
// Console.WriteLine("Got expected SqlException due to command timeout ");
// Console.WriteLine(e);
// }
return true ;
}
);
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
Console.WriteLine(ex.Message);
//MessageBox.Show(ex.Message);
}
}
}
}
D:\tu_dev_jsc>csc_mysql.bat a2.cs
D:\tu_dev_jsc>c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /r:"C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.17\Assemblies\v4.5.2\MySql.Data.dll","C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.17\Assemblies\v4.5.2\Renci.SshNet.dll" a2.cs
Microsoft (R) Visual C# Compiler version 4.8.3752.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.
This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240
D:\tu_dev_jsc>
REM compile with reference to Library DLL
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /r:"C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.17\Assemblies\v4.5.2\MySql.Data.dll","C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.17\Assemblies\v4.5.2\Renci.SshNet.dll" %1
REM == NOTE in source code folder must have file Renci.SshNet.dll
(copy from .NET PATH )
REM Must install MySQL.NET Connection in PC
D:\tu_dev_jsc>a2.exe
Hello World!
Press any key to exit.
Connect DB START
Continue
Connect DB OK
prov_code=10 prov_name=กรุงเทพมหานคร
prov_code=11 prov_name=สมุทรปราการ
prov_code=12 prov_name=นนทบุรี
prov_code=13 prov_name=ปทุมธานี
prov_code=14 prov_name=พระนครศรีอยุธยา
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment