在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1. 添加 NuGet 包 在解决方案管理器视图中的目标项目上右键 -> 管理 NuGet 程序包;
2. 通过代码访问 MySQL 数据库
using MySql.Data.MySqlClient; namespace MySqlDatabaseHelper { public class MySqlDatabaseHelper { public string dbConnStr; private MySqlConnection dbConn = null; private MySqlCommand dbCmd = null; private MySqlDataReader dbDataReader = null; public MySqlDatabaseHelper(string connectionString) { dbConnStr = connectionString; dbConn = new MySqlConnection(dbConnStr); dbCmd = new MySqlCommand(); dbCmd.Connection = dbConn; } ~MySqlDatabaseHelper() { Close(); } public void Open() { dbConn.Open(); } public void Close() { dbConn.Close(); } public MySqlDataReader ExecuteReader(string sqlStr_Read) { dbCmd.CommandText = sqlStr_Read; dbDataReader = dbCmd.ExecuteReader(); return dbDataReader; } public int ExecuteNonQuery(string sqlStr_Insert) { dbCmd.CommandText = sqlStr_Insert; int result = dbCmd.ExecuteNonQuery(); return result; } } }
using MySql.Data.MySqlClient; namespace MySqlDatabaseHelper { class Program { static void Main(string[] args) { string ConnectionString = "server = 127.0.0.1; userid = root; password = 123456; database = test_db; persistsecurityinfo = True;"; MySqlDatabaseHelper db_Test = new MySqlDatabaseHelper(ConnectionString); // 打开数据库 db_Test.Open(); // sql 查询语句 string sqlCmdStr = null; // 新增记录 sqlCmdStr = "insert into chipinfo_20200209(chipid, chiptype) values('5','S10')"; db_Test.ExecuteNonQuery(sqlCmdStr); // 删除记录 sqlCmdStr = "delete from chipinfo_20200209 where chipid = 4"; db_Test.ExecuteNonQuery(sqlCmdStr); // 修改数据 sqlCmdStr = "update chipinfo_20200209 set boardid = '666' where chipid = '5'"; db_Test.ExecuteNonQuery(sqlCmdStr); // 查询数据 sqlCmdStr = "select * from chipinfo_20200209"; MySqlDataReader dataReader = db_Test.ExecuteReader(sqlCmdStr); while (dataReader.Read()) { string str = dataReader["chiptype"].ToString(); } dataReader.Close(); // 关闭数据库 db_Test.Close(); } } }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论