.net连接MYSQL数据库方法一

时间:2021-07-08 04:47:33

1、添加引用MySql.Data.dll
2、在配置文件设置如下,其中5.2.1.0对应dll文件的版本号。

<connectionStrings>  
    <add name="MySqlServer" connectionString="Data Source=192.168.0.2;Initial Catalog=test;Persist Security Info=True;User ID=user1;Password=pass1" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
 
  <system.data>
    <DbProviderFactories>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.2.1.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>

3、程序中引用命名空间MySql.Data.MySqlClient;