Mysql Connector/NET Developer Guide Abstract
Total Page:16
File Type:pdf, Size:1020Kb
MySQL Connector/NET Developer Guide Abstract This manual describes how to install and configure MySQL Connector/NET, the connector that enables .NET applications to communicate with MySQL servers, and how to use it to develop database applications. For notes detailing the changes in each release of Connector/NET, see MySQL Connector/NET Release Notes. For legal information, including licensing information, see the Preface and Legal Notices. For help with using MySQL, please visit the MySQL Forums, where you can discuss your issues with other MySQL users. Document generated on: 2021-09-24 (revision: 70892) Table of Contents Preface and Legal Notices ............................................................................................................ v 1 Introduction to MySQL Connector/NET ....................................................................................... 1 2 Connector/NET Versions ............................................................................................................ 3 3 Connector/NET Installation ......................................................................................................... 7 3.1 Installing Connector/NET on Windows ............................................................................. 7 3.1.1 Installing Connector/NET Using MySQL Installer .................................................... 7 3.1.2 Installing Connector/NET Using the Standalone Installer ........................................ 7 3.1.3 Installing Connector/NET Using NuGet .................................................................. 8 3.2 Installing Connector/NET on Unix with Mono .................................................................. 10 3.3 Installing Connector/NET from Source ........................................................................... 10 4 Connector/NET Connections .................................................................................................... 13 4.1 Creating a Connector/NET Connection String ................................................................. 13 4.2 Managing a Connection Pool in Connector/NET ............................................................. 16 4.3 Handling Connection Errors ........................................................................................... 17 4.4 Connector/NET Authentication ....................................................................................... 18 4.5 Connector/NET 8.0 Connection Options Reference ......................................................... 19 5 Connector/NET Programming ................................................................................................... 35 5.1 Using GetSchema on a Connection ............................................................................... 36 5.2 Using MySqlCommand .................................................................................................. 37 5.3 Using Connector/NET with Table Caching ...................................................................... 40 5.4 Preparing Statements in Connector/NET ........................................................................ 41 5.5 Creating and Calling Stored Procedures ......................................................................... 42 5.6 Handling BLOB Data With Connector/NET ..................................................................... 46 5.6.1 Preparing the MySQL Server .............................................................................. 46 5.6.2 Writing a File to the Database ............................................................................ 47 5.6.3 Reading a BLOB from the Database to a File on Disk .......................................... 48 5.7 Working with Partial Trust / Medium Trust ...................................................................... 50 5.7.1 Evolution of Partial Trust Support Across Connector/NET Versions ....................... 50 5.7.2 Configuring Partial Trust with Connector/NET Library Installed in GAC .................. 51 5.7.3 Configuring Partial Trust with Connector/NET Library Not Installed in GAC ............. 52 5.8 Writing a Custom Authentication Plugin .......................................................................... 53 5.9 Using the Connector/NET Interceptor Classes ................................................................ 56 5.10 Handling Date and Time Information in Connector/NET ................................................. 58 5.10.1 Fractional Seconds ........................................................................................... 58 5.10.2 Problems when Using Invalid Dates .................................................................. 58 5.10.3 Restricting Invalid Dates ................................................................................... 58 5.10.4 Handling Invalid Dates ...................................................................................... 58 5.10.5 Handling NULL Dates ....................................................................................... 59 5.11 Using the MySqlBulkLoader Class ............................................................................... 59 5.12 Using the Connector/NET Trace Source Object ............................................................ 61 5.12.1 Viewing MySQL Trace Information .................................................................... 61 5.12.2 Building Custom Listeners ................................................................................ 64 5.13 Using Connector/NET with Crystal Reports ................................................................... 65 5.13.1 Creating a Data Source .................................................................................... 65 5.13.2 Creating the Report .......................................................................................... 66 5.13.3 Displaying the Report ....................................................................................... 67 5.14 Asynchronous Methods ............................................................................................... 69 5.15 Binary and Nonbinary Issues ....................................................................................... 76 5.16 Character Set Considerations for Connector/NET ......................................................... 76 6 Connector/NET Tutorials .......................................................................................................... 77 6.1 Tutorial: An Introduction to Connector/NET Programming ................................................ 77 6.1.1 The MySqlConnection Object .............................................................................. 77 6.1.2 The MySqlCommand Object ............................................................................... 78 6.1.3 Working with Decoupled Data ............................................................................. 80 6.1.4 Working with Parameters .................................................................................... 83 iii MySQL Connector/NET Developer Guide 6.1.5 Working with Stored Procedures ......................................................................... 84 6.2 ASP.NET Provider Model and Tutorials .......................................................................... 86 6.2.1 Tutorial: Connector/NET ASP.NET Membership and Role Provider ....................... 88 6.2.2 Tutorial: Connector/NET ASP.NET Profile Provider .............................................. 91 6.2.3 Tutorial: Web Parts Personalization Provider ....................................................... 93 6.2.4 Tutorial: Simple Membership Web Provider ......................................................... 96 6.3 Tutorial: Using an Entity Framework Entity as a Windows Forms Data Source ................ 101 6.4 Tutorial: Data Binding in ASP.NET Using LINQ on Entities ............................................ 108 6.5 Tutorial: Generating MySQL DDL from an Entity Framework Model ................................ 111 6.6 Tutorial: Basic CRUD Operations with Connector/NET .................................................. 112 6.7 Tutorial: Configuring SSL with Connector/NET .............................................................. 115 6.7.1 Using PEM Certificates in Connector/NET ......................................................... 116 6.7.2 Using PFX Certificates in Connector/NET .......................................................... 116 6.8 Tutorial: Using MySqlScript .......................................................................................... 118 7 Connector/NET for Entity Framework ...................................................................................... 123 7.1 Entity Framework 6 Support ........................................................................................ 123 7.2 Entity Framework Core Support ................................................................................... 129 7.2.1 Creating a Database with Code First in EF Core ................................................ 130 7.2.2 Scaffolding an Existing Database in EF Core ..................................................... 134 7.2.3 Configuring Character Sets and Collations in EF Core ........................................ 136 8 Connector/NET API Reference ............................................................................................... 139 8.1 Microsoft.EntityFrameworkCore Namespace ................................................................