Exemplo n.º 1
0
        private void GetRoutines(ref MySqlCommand cmd, string routine)
        {
            try
            {
                if (ServerMajorVersion >= 5)
                {
                    DataTable dtR = new DataTable();

                    // Edited by Niemand
                    //cmd.CommandText = "SHOW " + routine + " STATUS WHERE Db LIKE '" + DatabaseName + "';";
                    cmd.CommandText = "SHOW " + routine + " STATUS WHERE UPPER(TRIM(Db))=UPPER(TRIM('" + DatabaseName + "'));";

                    MySqlDataAdapter daR = new MySqlDataAdapter(cmd);
                    daR.Fill(dtR);
                    daR = null;

                    foreach (DataRow dr in dtR.Rows)
                    {
                        cmd.CommandText = "SHOW CREATE " + routine + " `" + dr[1] + "`;";
                        DataTable        dtP = new DataTable();
                        MySqlDataAdapter daP = new MySqlDataAdapter(cmd);
                        daP.Fill(dtP);
                        daP = null;
                        if (dtP.Rows.Count != 0)
                        {
                            string createSql = "";
                            object ob        = dtP.Rows[0][2];

                            if (ob is System.String)
                            {
                                createSql = ob + "";
                            }
                            // It has been reported that, in some unknown cases, the query will return
                            // byte array.
                            // Report: http://www.codeproject.com/Messages/4450086/Small-changes-in-Code.aspx
                            else if (ob is System.Byte[])
                            {
                                UTF8Encoding enc = new UTF8Encoding();
                                createSql = enc.GetString((byte[])ob);
                            }

                            string cr = createSql.Replace("\r\n", "^^^^").Replace("\n", "\r\n").Replace("^^^^", "\r\n");
                            if (routine == "PROCEDURE")
                            {
                                StoredProcedure.Add(dr[1].ToString(), cr);
                            }
                            else if (routine == "FUNCTION")
                            {
                                StoredFunction.Add(dr[1].ToString(), cr);
                            }
                        }
                    }
                }
            }
            catch
            {
                // Purposely do nothing.
                // Trap exception that raised due to restriction of user privilege
                // The MySQL User used in this connection is not allowed to access Functions or Stored Procedures

                // Clear and Reset Collection
                if (routine == "PROCEDURE")
                {
                    StoredProcedure = new Dictionary <string, string>();
                }
                else if (routine == "FUNCTION")
                {
                    StoredFunction = new Dictionary <string, string>();
                }
            }
        }