A very common issue reported by our customers is assigning MySQL database as ComponentOne Studio control’s DataSource. Simply assigning a data source is far more irritating than creating an awesome multi-featured application in such scenarios. On one hand, using Access and SQL databases is quick and easy due to the presence of OLEDB connectors. On the other, connecting to MySQL datasource seems a hard nut to crack, due to the absence of any such connector. This blog will help in solving such problems. The approach mentioned in this blog can be used to connect any ComponentOne Studio control to MySQL database. For the demonstration, we'll look at Report for WinForms.
Programming languages need connectors/interfaces like ODBC, OLEDB or .NET to connect with databases like MySQL. These are the database APIs, based on Call Level Interface (CLI) API definition published by various standard organizations. They're basically used to access data in different databases by using their corresponding drivers. The application calls the connector’s API functions using a standard SQL syntax. Then the driver makes any necessary translations to send the statement to DBMS and present the result back to the application.
MySQL Connector/ODBC provides both driver-manager-based and native interfaces to the MySQL database, with full support for MySQL functionality, including stored procedures, transactions and, with Connector/ODBC 5.1 and higher, full Unicode compliance. For connecting MySQL database:
Data Provider: Auto
Connection String: Provider=MySQL Provider; Data Source=MySQLServerIP; User ID =MyID; Password=MyPassword;
Please modify the connection string according to your server credentials.MySQL .Net connect provides secure, high-performance data connectivity with MySQL databases. It implements the required ADO.NET interfaces and integrates into ADO.NET aware tools. For using this:
After installing, you need to add "MySql.Data" assembly in project's references which contains definition of classes like MySqlDataAdapter and MySqlDataReader. This assembly can be found at following location:
C:\\Program Files (x86)\\MySQL\\MySQL Connector Net 6.9.6\\Assemblies\\v4.0
Now, you can use following code to fill the MySQL data in a DataTable which can be further assigned to "RecordSet" property of C1Report's DataSource.
MySqlConnection conn = null;
MySqlCommand cmd = null;
DataTable dataTable = new DataTable();
conn = new MySqlConnection("SERVER=in-ggs-praveen;UID=root;PWD=root;DATABASE=test;");
cmd = new MySqlCommand(sql, conn);
string sql = "SELECT * FROM stuff_master";
conn.Open();
using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
{
da.Fill(dataTable);
}
c1Report1.DataSource.Recordset = dataTable;
c1Report1.Render();
Refer to the link below for more information on MySQL connection string with different connectors. http://www.connectionstrings.com/mysql/#p27
This approach can be used to connect any ComponentOne Studio Control, including FlexGrid, List, TrueDBGrid, etc., to MySQL database. You simply need to assign the DataSource property of respective control to the corresponding table. Please let me know (through the comments) if you face any problem while connecting to MySQL database with any other control.
Attached sample project shows connecting C1Report to MySQL database using .NET provider.
Net Connector CS Sample
Net Connector VB Sample