Represents an open connection to MySQL.
The following example creates a
MySqlCommand and a
MySqlConnection. The
MySqlConnection is opened and set as the
Connection property. The example then calls
ExecuteNonQuery method, and closes the connection. To accomplish this, the
ExecuteNonQuery is passed a connection string and a query string that is SQL INSERT statement.
public void InsertRow(string myConnectionString)
{
// If the connection string is empty, use default.
if(myConnectionString == "")
{
myConnectionString =
"User Id=root;Host=localhost;Database=Test;";
}
MySqlConnection myConn = new MySqlConnection(myConnectionString);
string myInsertQuery = "INSERT INTO DEPT VALUES (50,'Development','Philadelphia')";
MySqlCommand myCommand = new MySqlCommand(myInsertQuery);
myCommand.Connection = myConn;
myConn.Open();
try
{
myCommand.ExecuteNonQuery();
}
finally
{
myConn.Close();
}
}
Public Sub InsertRow(myConnectionString As String)
' If the connection string is empty, use default.
If myConnectionString = "" Then
myConnectionString = _
"User Id=root;Host=localhost;Database=Test;"
End If
Dim myConn As New MySqlConnection(myConnectionString)
Dim myInsertQuery As String = "INSERT INTO DEPT VALUES (50,'Development','Philadelphia')"
Dim myCommand As New MySqlCommand(myInsertQuery)
myCommand.Connection = myConn
myConn.Open()
Try
myCommand.ExecuteNonQuery()
Finally
myConn.Close()
End Try
End Sub