准备工作
在Visual Studio中新建一个C#的控制台应用程序。
确保后台已经启用了MySQL57服务。
在MySQL的安装路径下找到DLL,默认位置是 C:\Program Files (x86)\MySQL\Connector.NET 6.9\Assemblies\v4.5\MySql.Data.dll 。
更多写代码连接MySQL的方法(通过MySQLConnector连接器),参考文档 C:\Program Files (x86)\MySQL\Connector.NET 6.9\Documentation\ConnectorNET.chm 。在文档的Users Guide-Programming中有代码案例。
把MySql.Data.dll引入项目中。v4.5表示.NetFramework框架的版本号,一定要保证使用的DLL与项目的.NetFramework版本一致,否则运行时报错找不到DLL,报错如下:
1>F:\Learning\CSharpConnectMySQL\CSharpConnectMySQL\Program.cs(1,7,1,12): error CS0246: The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference?)
测试增删改查
usingMySql.Data.MySqlClient;usingSystem;usingSystem.Col