目录 一、前期准备 二、MySQL 字符串连接 三、MySQL Windows身份验证 (不推荐使用) --不安全 四、MySQL 类构建连接 五、MySQL 外部配置文件连接 (推荐) -- connectionStrings连接 六、M
目录
三、MySQL Windows身份验证 (不推荐使用) --不安全
五、MySQL 外部配置文件连接 (推荐) -- connectionStrings连接
六、MySQL 外部配置文件连接 -- AppSettings连接
涉及知识点:AppConfig配置 -- C#连接MySQL
打开cmd命令框
win+R --> cmd
2.Mysql账号登录
mysql -uroot -p123456;
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
--结束END--
本文标题: C#与MySQL的连接的各种方法
本文链接: https://lsjlt.com/news/405696.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0