WPF_MySQL基本操作

时间:2021-12-24 05:09:13

1.在管理NuGet程序包中,选择浏览,下载MySql.Data,

WPF_MySQL基本操作

 2.在命名空间中引用

using MySql.Data.MySqlClient;

WPF_MySQL基本操作

 3.配置Configuration文件连接Mysql

WPF_MySQL基本操作

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>

    <add name="connStr" connectionString="server=255.255.255.255;port=3306;database=dbase;user=root;password=psd" providerName="System.Data.SqlClient"/>

  </connectionStrings>
</configuration>

用法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MySql.Data.MySqlClient;

namespace W数组
{
    class Sqlhelper
    {
        /// <summary>
        /// 服务器地址;端口号;数据库;用户名;密码
        /// </summary>
        public static string connectStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; //用户名和密码在MySQL定义的
        public static MySqlConnection conn = new MySqlConnection(connectStr);                 //using MySql.Data.MySqlClient;

    }
}