MySQL Connector/NET 发行说明
在 Crystal Reports 中创建报表时,在设计报表时,可以通过两种方式访问 MySQL 数据。
第一种方式是在设计报表时使用 Connector/ODBC 作为 ADO 数据源。您将能够浏览数据库并选择表和字段,使用拖放操作来构建报表。这种方法的缺点是必须在应用程序中执行额外的操作,才能生成与报表预期的数据集相匹配的数据集。
第二种方式是在 VB.NET 中创建数据集并将其保存为 XML。然后,此 XML 文件可用于设计报表。在应用程序中显示报表时,这种方式非常有效,但在设计时不太灵活,因为您必须在创建数据集时选择所有相关列。如果您忘记了某个列,则必须重新创建数据集,才能将该列添加到报表中。
以下代码可用于从查询创建数据集并将其写入磁盘
C# 代码示例
DataSet myData = new DataSet();
MySql.Data.MySqlClient.MySqlConnection conn;
MySql.Data.MySqlClient.MySqlCommand cmd;
MySql.Data.MySqlClient.MySqlDataAdapter myAdapter;
conn = new MySql.Data.MySqlClient.MySqlConnection();
cmd = new MySql.Data.MySqlClient.MySqlCommand();
myAdapter = new MySql.Data.MySqlClient.MySqlDataAdapter();
conn.ConnectionString = "server=127.0.0.1;uid=root;" +
"pwd=12345;database=test";
try
{
cmd.CommandText = "SELECT city.name AS cityName, city.population AS CityPopulation, " +
"country.name, country.population, country.continent " +
"FROM country, city ORDER BY country.continent, country.name";
cmd.Connection = conn;
myAdapter.SelectCommand = cmd;
myAdapter.Fill(myData);
myData.WriteXml(@"C:\dataset.xml", XmlWriteMode.WriteSchema);
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message, "Report could not be created",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Visual Basic 代码示例
Dim myData As New DataSet
Dim conn As New MySqlConnection
Dim cmd As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
conn.ConnectionString = "server=127.0.0.1;" _
& "uid=root;" _
& "pwd=12345;" _
& "database=world"
Try
conn.Open()
cmd.CommandText = "SELECT city.name AS cityName, city.population AS CityPopulation, " _
& "country.name, country.population, country.continent " _
& "FROM country, city ORDER BY country.continent, country.name"
cmd.Connection = conn
myAdapter.SelectCommand = cmd
myAdapter.Fill(myData)
myData.WriteXml("C:\dataset.xml", XmlWriteMode.WriteSchema)
Catch ex As Exception
MessageBox.Show(ex.Message, "Report could not be created", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
生成的 XML 文件在设计报表时可用作 ADO.NET XML 数据源。
如果您选择使用 Connector/ODBC 设计报表,则可以从 dev.mysql.com 下载。