Example #1
0
        public AreaFacadeTest()
        {
            string dbTypeId = ConfigurationManager.AppSettings["DbType"];

            TOP.Common.DbHelper.DbType dbType = (TOP.Common.DbHelper.DbType) short.Parse(dbTypeId);
            dbSqlCreater = DbSqlCreater.GetDbSqlCreater(dbType);

            connString = ConfigurationManager.AppSettings["ConnString"];
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string value = tvSchema.SelectedNode.Value;

            if (value != "table" && value != "view")
            {
                Type   type     = Type.GetType(value);
                string dbTypeId = ConfigurationManager.AppSettings["DbType"];
                TOP.Common.DbHelper.DbType dbType = (TOP.Common.DbHelper.DbType) short.Parse(dbTypeId);
                string connString = ConfigurationManager.AppSettings["ConnString"];

                string sqlDrop   = string.Empty;
                string sqlCreate = string.Empty;

                string parent = tvSchema.SelectedNode.Parent.Value;
                if (parent.Equals("Table", StringComparison.OrdinalIgnoreCase))
                {
                    DbSqlCreater dbSqlCreater = DbSqlCreater.GetDbSqlCreater(dbType);
                    sqlDrop   = dbSqlCreater.GenerateDropTableSql(type);
                    sqlCreate = dbSqlCreater.GenerateCreateTableSql(type);
                }
                else if (parent.Equals("View", StringComparison.OrdinalIgnoreCase))
                {
                    string baseDir = Server.MapPath("~");
                    baseDir += @"\..\..\Database\Scripts";
                    DbQuery dbQuery = DbQuery.GetDbQuery(dbType);
                    sqlDrop   = dbQuery.GenerateDropViewSql(type, baseDir);
                    sqlCreate = dbQuery.GenerateCreateViewSql(type, baseDir);
                }
                else
                {
                    throw new ArgumentException("不能更新的类型 - " + parent + " - " + value);
                }

                using (DbOperator dbOperator = new DbOperator(connString))
                {
                    try
                    {
                        dbOperator.BeginTran();
                        dbOperator.ExecSql(sqlDrop);
                        dbOperator.ExecSql(sqlCreate);
                        dbOperator.CommintTran();

                        lblMessage.Text = "更新成功 - " + parent + " - " + value;
                    }
                    catch
                    {
                        dbOperator.RollbackTran();
                        throw new Exception("更新数据库对象错误 - " + parent + " - " + value);
                    }
                }
            }
            else
            {
                lblMessage.Text = "所选择对象无效,请选择要更新的数据库对象。";
            }
        }