Code Snippets

MySql Connection C#

[sourcecode language=“csharp“ wraplines=“true“ collapse=“true“]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MySql.Data.MySqlClient;

namespace OctoSearch
{
class MySql
{
public void Write(string mpn, string tech, int pin)
{
try
{
string connStr = „server=yourServerHere;user=yourUserHere;database=yourDatabaseHere;port=3306;password=yourPasswordHere“;
MySqlConnection conn = new MySqlConnection(connStr);

//verbindung aufbauen
conn.Open();

//—schreiben
string sqlWrite = „INSERT INTO yourTableHere (mpn, tech, pin) VALUES (“ + „‚“+ mpn + „‚“ + „,“ + „‚“ + tech + „‚“ + „,“ + „‚“ + pin + „‚“ + „)“;
MySqlCommand cmdWrite = new MySqlCommand(sqlWrite, conn);
cmdWrite.ExecuteNonQuery();

//—lesen
string sqlRead = „SELECT * FROM yourTableHere „;
MySqlCommand cmdRead = new MySqlCommand(sqlRead, conn);
MySqlDataReader rdr = cmdRead.ExecuteReader();

while (rdr.Read())
{
Console.WriteLine(rdr[0] + “ “ + rdr[1] + “ “ + rdr[2]);
}
rdr.Close();
}
catch (Exception)
{

throw;
}
}
}
}

[/sourcecode]