j3ssi3ftw j3ssi3ftw - 3 months ago 24
C# Question

build mySQL database using c# console app

Alright so I'm trying to make a web app, and I'm trying to use mySQL as my database. I want to use c# (visual studio 2008 SP1) to create functions for interfacing with the database. The syntax of how to do this is no problem but I'm not sure how to get the two to interact whatsoever; I don't understand the relationship. I have downloaded and configured a mySQL server, and through using the "mySQL Command Line Client" I have no problem composing a database using SQL commands. I have also downloaded mySQL for visual studio, and I have no problem using it's interface to add tables and so on. What I'm trying to do is write c# code and SQL statements in the same environment.

For reference my objective is to create a small app so that my friend and I can duel with our custom, physical mtg decks that we have composed. So for example I would like to set up a small c# interface to add, remove, and replace cards based on the changes we'd like to make to our decks out of the cards we already have. So in summation I would like to build a mySQL database through a c# console app.

Answer

There are docs for this on mysql.com: https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-open.html

MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;

myConnectionString = "server=127.0.0.1;uid=root;" +
    "pwd=12345;database=test;";

try
{
    conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
    conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
    MessageBox.Show(ex.Message);
}

https://dev.mysql.com/doc/connector-net/en/connector-net-programming-mysqlcommand.html

MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "mytable";
cmd.Connection = someConnection;
cmd.CommandType = CommandType.TableDirect;
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
   Console.WriteLn(reader[0], reader[1]...);
}
Comments