Created
February 4, 2020 22:05
-
-
Save lambdageek/5b45f76e40c433647350aec9836ef4c7 to your computer and use it in GitHub Desktop.
MySqlConnector sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net472</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="MySqlConnector" Version="0.61.0" /> | |
</ItemGroup> | |
<ItemGroup> | |
<None Update="client-bundle.pfx"> | |
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |
</None> | |
</ItemGroup> | |
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MySql.Data.MySqlClient; | |
// Setup openssl keys for mysql with openssl. | |
// I followed https://www.cyberciti.biz/faq/how-to-setup-mariadb-ssl-and-secure-connections-from-clients/ | |
// Setup a database test with the following table t1: | |
// | |
// create table t1(a int, b int, c varchar(128)) default charset utf8; | |
// insert into t1 (a, b, c) values (1, 1, "abc), (2, 2, "xyz"); | |
// | |
// Connect to the database and create a user dumdum: | |
// create user [email protected]; | |
// grant all on test.t1 to [email protected]; | |
// flush privileges; | |
// | |
// build and run this project: | |
// msbuild | |
// mono bin/Debug/net472/FoofSql.exe | |
// | |
// Expected output: | |
// | |
// Connection: MySql.Data.MySqlClient.MySqlConnection | |
// opened | |
// row: (a = 1, b = 1, c = <<abc>>) | |
namespace FoofSql | |
{ | |
class MainClass | |
{ | |
const string USER="dumdum"; | |
/* created by: openssl pkcs12 -in client-cert.pem -inkey client-key.pem -export -out client-bundle.pfx */ | |
const string PkcsBundleFile = "client-bundle.pfx"; | |
public static void Main(string[] args) | |
{ | |
var connectionString = $"Host=127.0.0.1;Port=3306;User={USER};Database=test;SslMode=Required;Protocol=Socket;CertificateFile={PkcsBundleFile}"; | |
using (var conn = new MySqlConnection(connectionString)) | |
{ | |
Console.WriteLine($"Connection: {conn}"); | |
conn.Open (); | |
Console.WriteLine ($"opened"); | |
var query = "select * from t1 where b=@b"; | |
var cmd = conn.CreateCommand (); | |
cmd.CommandText = query; | |
cmd.Parameters.AddWithValue("@b", 1); | |
using (var cursor = cmd.ExecuteReader ()) { | |
while (cursor.Read ()) { | |
var a = cursor["a"]; | |
var b = cursor["b"]; | |
var c = cursor["c"]; | |
Console.WriteLine ($"row: (a = {a}, b = {b}, c = <<{c}>>)"); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment