.net连接MySql数据库,简单三层模式,与大家分享
2022-04-14 02:38:46 1.14MB .net MySql
1
下载后安装 在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中,为了便于大家立马使用,下面自己写了一个真实的例子: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.Odbc; using MySQLDriverCS; public class MySQLDataAccessFactory { private const string connstr = "Data Source=数据库名称;Password=数据库密码;User ID=数据库用户名;Location=数据库服务器ip或机器名"; private MySQLConnection conn = null; private MySQLCommand command = null; private MySQLConnection GetConnection() { if (conn != null) { conn.Open(); return conn; } else { conn = new MySQLConnection(connstr); conn.Open(); return conn; } } private void CloseConnection() { try { if (conn != null) { conn.Close(); conn.Dispose(); conn = null; } } catch (Exception ex) { throw new Exception("MySQL数据库连接失败", ex); } } //防止发生中文乱码 private void KillChinese() { MySQLCommand mysql_command = new MySQLCommand("set names gb2312", GetConnection()); mysql_command.ExecuteNonQuery(); } //得到command public MySQLCommand GetCommand(string cmdText) { if (command != null) { return command; } else { command = new MySQLCommand(cmdText, GetConnection()); return command; } } //添加参数 public void AddInParameter(MySQLCommand command, string name, object value) { IDataParameter param = command.CreateParameter(); param.ParameterName = name; if (value
1
C# .net连接mysql数据库的方法大全 三种连接方法 + 所需的文件 = 全部
2021-09-13 15:26:27 9.84MB C# .net mysql
1