예제 #1
0
        protected void btnExec_Click(object sender, EventArgs e)
        {
            if (txtMuSql.Text.Trim().Length > 0)
            {
                if (Session["eConn"] != null || txtMuSql.Text.Trim().StartsWith("use", true, CultureInfo.CurrentCulture) || txtMuSql.Text.Trim().StartsWith("create database", true, CultureInfo.CurrentCulture))
                {
                    string[] sqls    = txtMuSql.Text.Trim().Split(SQL_GO, StringSplitOptions.RemoveEmptyEntries);
                    string   results = "";
                    bool     conti   = true;
                    tables = new List <DataTable>();
                    foreach (string _sql in sqls)
                    {
                        if (!conti)
                        {
                            results += "<span class='err'>已终止执行</span><br/>";
                            break;
                        }
                        string sql = _sql.Trim();
                        if (sql.StartsWith("use", true, CultureInfo.CurrentCulture))
                        {
                            string[] data = sql.Split(' ');
                            if (data[0].ToLower().Equals("use") && data.Length > 1)
                            {
                                bool db = true;
                                foreach (ListItem item in ddlDatabase.Items)
                                {
                                    if (item.Text.ToLower().Equals(data[1].ToLower()))
                                    {
                                        ddlDatabase.SelectedValue = item.Value;
                                        Session["eConn"]          = Session["mConn"].ToString().Replace("master", ddlDatabase.SelectedValue);
                                        results += "<span>切换成功</span><br/>";
                                        db       = false;
                                        break;
                                    }
                                }
                                if (db)
                                {
                                    results += "<span class='err'>未找到数据库</span><br/>";
                                    conti    = false;
                                }
                            }
                        }
                        else
                        {
                            try
                            {
                                ToolBll bll;
                                switch (getSQLType(sql))
                                {
                                case SQL_TYPE.SELECT:
                                    bll = new ToolBll(Session["eConn"].ToString());
                                    tables.Add(bll.executeQueryToSelect(sql, tables.Count));
                                    results += "<span>查询成功</span><br/>";
                                    break;

                                case SQL_TYPE.UPDATE:
                                    bll      = new ToolBll(Session["eConn"].ToString());
                                    results += "<span class='blue'>" + bll.executeQueryToUdate(sql) + "行受影响</span><br/>";
                                    break;

                                case SQL_TYPE.OBJECT:
                                    bll      = new ToolBll(Session["eConn"].ToString());
                                    results += bll.executeQueryToObj(sql);
                                    break;

                                case SQL_TYPE.NEWDB:
                                    bll = new ToolBll(Session["mConn"].ToString());
                                    bll.executeQueryToObj(sql);
                                    results = "<span>新增成功</span><br/>";
                                    showDatabase();
                                    break;
                                }
                            }
                            catch (Exception er)
                            {
                                results += "<span class='err'>" + er.Message + "</span><br/>";
                                conti    = false;
                            }
                        }
                    }
                    txtResoult.InnerHtml = results;
                    if (tables.Count > 0)
                    {
                        divData.Visible         = true;
                        ddlDataTable.DataSource = tables;
                        ddlDataTable.DataBind();
                        gdDataTable.DataSource = tables[0];
                        gdDataTable.DataBind();
                    }
                    else
                    {
                        divData.Visible = false;
                    }
                }
                else
                {
                    Common.regesterAlertScript(this, "err", "请选择一个数据库");
                }
            }
            else
            {
                Common.regesterAlertScript(this, "err", "请输入SQL脚本");
            }
        }