C# and Mysql: Mis 21 - Introduction To Applications Development
C# and Mysql: Mis 21 - Introduction To Applications Development
C# and Mysql: Mis 21 - Introduction To Applications Development
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();
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 = “INSERT INTO table (f1, f2, f3) VALUES (d1, d2, d3)";
string query = “INSERT INTO table (f1, f2, f3) VALUES (?a, ?b, ?c)";
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