C#与MySQL的连接的各种方法
目录
三、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;
二、MySQL 字符串连接
本地: 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!"); } }
三、MySQL Windows身份验证 (不推荐使用) --不安全
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!"); }}
四、MySQL 类构建连接
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(); //自动关闭 } }}
五、MySQL 外部配置文件连接 (推荐) -- connectionStrings连接
添加 --> 新建项--> 应用程序配置文件(默认名称: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!"); } } }
六、MySQL 外部配置文件连接 -- AppSettings连接
添加 --> 新建项--> 应用程序配置文件(默认名称: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
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341