Oracle 5.0 Reference Manual page 1879

Table of Contents

Advertisement

string sql = "INSERT INTO Country (Name, HeadOfState, Continent) VALUES ('Disneyland','Mick
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Console.WriteLine("Done.");
}
}
The query is constructed, the command object created and the
the command object. You can access your MySQL database with the
verify that the update was carried out correctly.
Finally, you will see how the
is straightforward, as a
do. The following code illustrates how to use ExecuteScalar:
using System;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
public class Tutorial4
{
public static void Main()
{
string connStr = "server=localhost;user=root;database=world;port=3306;password=******;";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
string sql = "SELECT COUNT(*) FROM Country";
MySqlCommand cmd = new MySqlCommand(sql, conn);
object result = cmd.ExecuteScalar();
if (result != null)
{
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Console.WriteLine("Done.");
}
}
This example uses a simple query to count the rows in the
calling
ExecuteScalar
20.2.4.1.3. Working with Decoupled Data
Previously, when using MySqlDataReader, the connection to the database was continually
maintained, unless explicitly closed. It is also possible to work in a manner where a connection is only
established when needed. For example, in this mode, a connection could be established to read a
chunk of data, the data could then be modified by the application as required. A connection could then
Connector/Net Tutorials
ExecuteScalar
MySqlDataReader
int r = Convert.ToInt32(result);
Console.WriteLine("Number of countries in the World database is: " + r);
on the command object.
ExecuteNonQuery
method can be used to return a single value. Again, this
object is not required to store results, a simple variable will
table. The result is obtained by
Country
1859
method called on
command interpreter and
mysql

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the 5.0 and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Mysql 5.0

Table of Contents