C# and Mysql: Mis 21 - Introduction To Applications Development

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

C# and MySQL

MIS 21 – Introduction to Applications Development


Overwiew
 C# and Databases
 Setting up a MYSQL database Connection
 Viewing data from a MYSQL database
 Manipulating data in the MYSQL database
 Tips
C# and Databases
 Justlike XML files, a relational database
may be used as a data source for your
application

 Justlike XML, the classes that will be used


to access the DB are derived from
ADO.NET
Setting up a connection
 Reference the DLL file, MySql.Data.dll
 Can be downloaded from the MySQL website
(MySQL Connector/.NET)
 Alternatively, you can download it from our
course website
 After adding the DLL, call the namespace:
 using MySql.Data.MySqlClient;
Setting up a connection
 Instantiate
the class MySqlConnection and input the
connection string
MySqlConnection conn = new
MySqlConnection("
Server=localhost;Database=MyDatabase;Uid
=root;Pwd=root;");
 This is equivalent to:
 mysql -u root -p
 use MyDatabase;
Connection String
 Specifies how C# will connect to MySQL:
 new MySqlConnection(conn_string)
 Server – IP address of the MySQL installation
(use localhost for your computer)
 Database – name of the database used
 Uid – MySQL username (root)
 Pwd – password of the username used (root)
Setting up a connection
 Create a connection string
 Create and open the connection
String conn_string = new MySqlConnection("
Server=localhost;Database=MyDatabase;Uid=root;Pwd
=root;");

MySqlConnection conn = new


MySqlConnection(conn_string);
conn.Open();
Viewing Data
 Involves the use of “SELECT” queries
 Two ways:
 Viewing chunks of data (ExecuteReader())
 Viewing a single piece of data
(ExecuteScalar())
Viewing Data
 Create a query using a string
 Createa MySqlCommand and pass the
query & MySqlConnection
string query = "SELECT * FROM table;";
MySqlCommand cmd = new MySqlCommand(query, conn);
Viewing Data (chunks)
 ExecuteReader() will return a MySqlDataReader
 Use reader.Read() to loop through each row of
the SELECT query's result
 Similar to how we read files
 Use reader.GetValue(i) to get one of the data in
the row
 Then, close the reader and the connection
Viewing Data (chunks)
MySqlConnection conn = new MySqlConnection(conn_string);
conn.Open();

string query = "SELECT * FROM table;";


MySqlCommand cmd = new MySqlCommand(query, conn);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string x = reader.GetValue(0).ToString();
int y = int.Parse(reader.GetValue(1).ToString());
double z = double.Parse(reader.GetValue(2).ToString());
// Use x, y, and z here
}

reader.Close();
conn.Close();
Viewing Data (single)
 ExecuteScalar() will return a generic object
 Use ToString() and then convert to the type
that you need
 Close the reader and the connection
Viewing Data (single)
MySqlConnection conn = new MySqlConnection(conn_string);
conn.Open();

string query = "SELECT COUNT(*) FROM table";


MySqlCommand cmd = new MySqlCommand(query, conn);
int count = int.Parse(cmd.ExecuteScalar().ToString());
Console.WriteLine("count {0}", count);

conn.Close();
Manipulating Data
 Involves
the use of “INSERT”, “UPDATE”,
and “DELETE” queries
 Two ways:
 Formatted Strings
 Parameterized Commands
Formatted Strings
 Create the query
 Create the MySQLCommand object and
pass the query
 Call ExecuteNonQuery()
 Then, close the connection
Formatted Strings
MySqlConnection conn = new MySqlConnection(conn_string);
conn.Open();

string query = "DELETE FROM table WHERE id = 5;";

MySqlCommand cmd = new MySqlCommand(query, conn);


cmd.ExecuteNonQuery();
conn.Close();
Formatted Strings
MySqlConnection conn = new MySqlConnection(conn_string);
conn.Open();

string query = “INSERT INTO table (f1, f2, f3) VALUES (d1, d2, d3)";

MySqlCommand cmd = new MySqlCommand(query, conn);


cmd.ExecuteNonQuery();
conn.Close();
Parameterized Commands
 Create the query, use “?”+varname for the data
 Create the MySQLCommand object and pass the
query
 Usecommand.Parameters.AddWithValue( “?
varname”, data) to add the data
 Call ExecuteNonQuery()
 Then, close the connection
Parameterized Commands
MySqlConnection conn = new MySqlConnection(conn_string);
conn.Open();

string query = "DELETE FROM table WHERE id = ?a;";

MySqlCommand cmd = new MySqlCommand(query, conn);


cmd.Parameters.AddWithValue(“?a”, 5);
cmd.ExecuteNonQuery();
conn.Close();
Parameterized Commands
MySqlConnection conn = new MySqlConnection(conn_string);
conn.Open();

string query = “INSERT INTO table (f1, f2, f3) VALUES (?a, ?b, ?c)";

MySqlCommand cmd = new MySqlCommand(query, conn);

insertcommand.Parameters.AddWithValue("?a", “d1");
insertcommand.Parameters.AddWithValue("?b", d2);
insertcommand.Parameters.AddWithValue("?c", “d3");

cmd.ExecuteNonQuery();
conn.Close();
Tips
 Sometimes there is a need to get the last inserted
ID, usually if the DB field is auto incremented
 Use int.Parse(command.LastInsertedId.ToString())
 Use ExecuteReader() for getting chunks of data
 Use ExecuteScalar() for getting single data
 Use ExecuteNonQuery() for manipulating data
 Use Parameterized commands

You might also like