/// <summary>
/// 建立数据库连接.
/// </summary>
/// <returns>返回MySqlConnection对象</returns>
public MySqlConnection getmysqlcon()
{
//http://sosoft.cnblogs.com/
string M_str_sqlcon = "server=localhostuser id=rootpassword=rootdatabase=abc" //根据自己的设置
MySqlConnection myCon = new MySqlConnection(M_str_sqlcon)
return myCon
}
#endregion
#region 执行MySqlCommand命令
/// <summary>
/// 执行MySqlCommand
/// </summary>
/// <param name="M_str_sqlstr">SQL语句</param>
public void getmysqlcom(string M_str_sqlstr)
{
MySqlConnection mysqlcon = this.getmysqlcon()
mysqlcon.Open()
MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon)
mysqlcom.ExecuteNonQuery()
mysqlcom.Dispose()
mysqlcon.Close()
mysqlcon.Dispose()
}
#endregion
#region 创建MySqlDataReader对象
/// <summary>
/// 创建一个MySqlDataReader对象
/// </summary>
/// <param name="M_str_sqlstr">SQL语句</param>
/// <returns>返回MySqlDataReader对象</returns>
public MySqlDataReader getmysqlread(string M_str_sqlstr)
{
MySqlConnection mysqlcon = this.getmysqlcon()
MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon)
mysqlcon.Open()
MySqlDataReader mysqlread = mysqlcom.ExecuteReader(CommandBehavior.CloseConnection)
return mysqlread
}
#endregion
using MySQLDriverCS// 建立数据库连接MySQLConnection DBConnDBConn = new MySQLConnection(new MySQLConnectionString("localhost","mysql","root","",3306).AsString)DBConn.Open()
// 执行查询语句MySQLCommand DBCommDBComm = new MySQLCommand("select Host,User from user",DBConn)
// 读取数据MySQLDataReader DBReader = DBComm.ExecuteReaderEx()
// 显示数据try{while (DBReader.Read()){Console.WriteLine("Host = {0} and User = {1}", DBReader.GetString(0),DBReader.GetString(1))}}finally{DBReader.Close()DBConn.Close()}
//关闭数据库连接DBConn.Close()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)