Ejemplo n.º 1
0
        public static string ExecuteScript(string script)
        {
            DemandCustomSqlReportAccess();

            StringBuilder output = new StringBuilder();

            output.Append("<?xml version='1.0' encoding='utf-8'?>");
            output.Append("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>");
            output.Append("<html xmlns='http://www.w3.org/1999/xhtml'>");
            output.Append("<head>");
            output.Append("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />");
            AppendElement(output, "title", "Custom SQL Report");
            output.Append("<style type='text/css'>");
            output.Append("table { border-collapse: collapse; border: 1px solid #000000; }");
            output.Append("th, td { vertical-align: top; border: 1px solid; padding: 4px; }");
            output.Append("span.null { font-style: italic; }");
            output.Append("p.error { color: red; }");
            output.Append("</style>");
            output.Append("</head>");

            output.Append("<body>");

            output.Append("<p>");
            AppendText(output, Configuration.Domain);
            output.Append("<br />");
            AppendText(output, DateTime.Now.ToString(CultureInfo.CurrentUICulture));
            output.Append("</p>");

            DbTransaction tran = DbTransaction.Begin();

            try
            {
                // O.R. [2009-10-13]: Set context info
                DbHelper2.RunText("DECLARE @BinaryId BINARY(128) SET @BinaryId = CAST(@UserId AS BINARY(128)) SET CONTEXT_INFO @BinaryId",
                                  DbHelper2.mp("@UserId", SqlDbType.Int, Security.CurrentUser.UserID));

                using (System.IO.StringReader reader = new System.IO.StringReader(script))
                {
                    bool          bContueRead    = true;
                    StringBuilder sqlCommandText = new StringBuilder();

                    while (bContueRead)
                    {
                        bContueRead = ReadQuery(reader, sqlCommandText);
                        if (sqlCommandText.Length > 0)
                        {
                            string query = sqlCommandText.ToString();
                            AppendElement(output, "p", query);

                            try
                            {
                                using (IDataReader rd = DbHelper2.RunTextDataReader(query))
                                {
                                    do
                                    {
                                        AppendReportTable(output, rd);
                                    }while (rd.NextResult());
                                }
                            }
                            catch (Exception e)
                            {
                                Trace.WriteLine(e.ToString());
                                AppendElement(output, "p", e.ToString(), "error");
                            }
                            sqlCommandText = new StringBuilder();
                        }
                    }
                }
            }
            catch
            {
            }
            output.Append("</body>");
            output.Append("</html>");

            tran.Rollback();

            return(output.ToString());
        }