日期:2014-05-16 浏览次数:20497 次
private void ExecuteSql(string sql) { MySqlConnection conn = null; MySqlCommand command = null; MySqlDataReader reader = null; try { conn = new MySqlConnection(Server=localhost;User Id=root;Password=root;Persist Security Info=True;Database=DataBaseName); command = conn.CreateCommand(); command.CommandText = sql; conn.Open(); reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader[0]); Console.WriteLine(reader[1]); Console.WriteLine(reader[2]); } } catch (MySqlException se) { Console.WriteLine(Database operation errors : + se.StackTrace); } finally { CloseAll(reader,conn); } } public void CloseAll(MySqlDataReader reader, MySqlConnection conn) { try { if (reader != null&&!reader.IsClosed) { reader.Close(); } if (conn != null && conn.State == ConnectionState.Open) { conn.Close(); } } catch (Exception) { throw; } }