static string connstr = "server=127.0.0.1;port=3306;database=newim;user=admin;password=admin;";
MySqlConnection conn = new MySqlConnection(connstr);
读取:
public MySqlDataReader MYSQLReader(string sql)
{
conn.Open();
MySqlCommand cmd = new MySqlCommand(sql, conn);
try
{
MySqlDataReader reader = cmd.ExecuteReader();
return reader;
}
catch
{
return null;
}
}
执行写入:
public int MysqlExecuteNonQuery(string sql)
{
try
{
MySqlCommand cmd = new MySqlCommand(sql, conn);
int result = cmd.ExecuteNonQuery();
return result;
}
catch (Exception e)
{
return -1;
}
}
连接状态判断:
if (conn.State == ConnectionState.Open)