v9.6.2017.0808   重构正向工程,基于映射表查找数据库字段类型到实体类型的映射
大石头 编写于 2017-08-08 21:38:06
X
using System;
using System.Data;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NewLife.Threading;
using XCode.DataAccessLayer;

namespace XCoder
{
    public partial class FrmQuery : Form
    {
        #region 属性
        private DAL _Dal;
        /// <summary>数据层</summary>
        public DAL Dal { get { return _Dal; } set { _Dal = value; } }
        #endregion

        #region 初始化界面
        public FrmQuery()
        {
            InitializeComponent();

            Icon = Source.GetIcon();
        }

        public static FrmQuery Create(DAL db)
        {
            if (db == null) throw new ArgumentNullException("db");

            FrmQuery frm = new FrmQuery();
            frm.Dal = db;

            return frm;
        }

        private void FrmQuery_Load(Object sender, EventArgs e)
        {
        }
        #endregion

        private void btnQuery_Click(Object sender, EventArgs e)
        {
            var sql = txtSQL.Text;
            if (sql.IsNullOrWhiteSpace()) return;

            Task.Factory.StartNew(() =>
            {
                var sw = new Stopwatch();
                sw.Start();

                String msg = null;
                DataTable dt = null;
                try
                {
                    DataSet ds = Dal.Session.Query(sql);
                    if (ds != null && ds.Tables != null && ds.Tables.Count > 0) dt = ds.Tables[0];

                    msg = "查询完成!";
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                }
                finally
                {
                    sw.Stop();

                    msg += String.Format(" 耗时{0}", sw.Elapsed);
                }

                this.Invoke(() => lbStatus.Text = msg);
                if (dt != null) this.Invoke(() => gv.DataSource = dt);
            }).LogException();
        }

        private void btnExecute_Click(Object sender, EventArgs e)
        {
            var sql = txtSQL.Text;
            if (sql.IsNullOrWhiteSpace()) return;

            Task.Factory.StartNew(() =>
            {
                var sw = new Stopwatch();
                sw.Start();

                String msg = null;
                try
                {
                    Int32 n = Dal.Session.Execute(sql);

                    msg = String.Format("执行完成!共影响{0}行!", n);
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                }
                finally
                {
                    sw.Stop();

                    msg += String.Format(" 耗时{0}", sw.Elapsed);
                }

                this.Invoke(() => lbStatus.Text = msg);
            }).LogException();
        }
    }
}