目录
三、MySQL Windows身份验证 (不推荐使用) --不安全
五、MySQL 外部配置文件连接 (推荐) -- connectionStrings连接
六、MySQL 外部配置文件连接 -- AppSettings连接
涉及知识点:AppConfig配置 -- C#连接MySQL
打开cmd命令框
win+R --> cmd
2.Mysql账号登录
Mysql -uroot -p123456;
mysql_student数据库创建
create database mysql_student;
mysql_student数据库使用
use mysql_student;
导入NuGet包
管理解决方案的NuGet程序包 --> 搜索MySql.Data 并下载
using MySql.Data.MySqlClient;using System.Configuration;
本地: localhost 表示 远程:IP,端口号 **注: Sqlsever用 local和. 表示本地, MySQL不可以**
Data Source == server //服务器名字
Initial Catalog == database //数据库名字
User Id == uid //账号
Password == pwd //密码
string connStr = "Data Source = localhost; Initial Catalog = mysql_student; User Id = root; Password = 123456";
或者
string connStr1 = "server=localhost;database=mysql_student;uid=root;pwd=123456;";
internal class Program { static void Main(string[] args) { //本地: localhost 表示 远程:IP,端口号 //Data Source == server //服务器名字 //Initial Catalog == database //数据库名字 //User Id == uid //账号 //Password == pwd //密码 string connStr = "Data Source = localhost; Initial Catalog = mysql_student; User Id = root; Password = 123456"; string connStr1 = "server=localhost;database=mysql_student;uid=root;pwd=123456;"; MySqlConnection connection = null; try { connection = new MySqlConnection(connStr1); connection.Open(); string cmd = "show databases;"; //显示数据库 MySqlDataReader reader = new MySqlCommand(cmd, connection).ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader[0]); } reader.Close(); } catch (Exception ex) { Console.WriteLine("错误:" + ex.Message); } finally { connection?.Close(); } Console.WriteLine("Hello World!"); } }
string connStr = "Data Source=localhost;Initial Catalog = mysql_student; Integrated Security = SSPI";
internal class Program{ static void Main(string[] args) { string connStr = "Data Source=localhost;Initial Catalog = mysql_student; Integrated Security = SSPI"; MySqlConnection connection = null; try { connection = new MySqlConnection(connStr); connection.Open(); string cmd = "show databases;"; //显示数据库 MySqlDataReader reader = new MySqlCommand(cmd, connection).ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader[0]); } reader.Close(); } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); } finally { connection?.Close(); } Console.WriteLine("Hello World!"); }}
MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
builder.Server = "localhost";
builder.UserID = "root";
builder.Password = "123456";
builder.Database = "mysql_student";
internal class Program{ static void Main(string[] args) { MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder(); builder.Server = "localhost"; builder.UserID = "root"; builder.Password = "123456"; builder.Database = "mysql_student"; using (MySqlConnection connect = new MySqlConnection(builder.ConnectionString)) { connect.Open(); string cmd = "show databases;"; MySqlDataReader reader = new MySqlCommand(cmd, connect).ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader[0]); } reader.Close(); //connect.Close(); //自动关闭 } }}
添加 --> 新建项--> 应用程序配置文件(默认名称:App.config)
//获取外部文件字符串
string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
internal class Program { static void Main(string[] args) { //获取外部文件字符串 string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; using (MySqlConnection connect = new MySqlConnection(connStr)) { connect.Open(); string cmd = "show databases;"; MySqlDataReader reader = new MySqlCommand(cmd, connect).ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader[0]); } reader.Close(); //connect.Close(); //自动关闭 Console.WriteLine("Hello World!"); } } }
添加 --> 新建项--> 应用程序配置文件(默认名称:App.config)
//获取外部文件字符串
string connStr = ConfigurationManager.AppSettings["connStr"].ToString();
internal class Program{ static void Main(string[] args) { //获取外部文件字符串 string connStr = ConfigurationManager.AppSettings["connStr"].ToString(); using (MySqlConnection connect = new MySqlConnection(connStr)) { connect.Open(); string cmd = "show databases;"; MySqlDataReader reader = new MySqlCommand(cmd, connect).ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader[0]); } reader.Close(); //connect.Close(); //自动关闭 } Console.WriteLine("Hello World!"); }}
来源地址:https://blog.csdn.net/againknow/article/details/128496805