Exemple #1
0
        /// <summary>
        /// 执行sql
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void autoTextBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            ShortcutKeyHelper.AllSelect(sender, e);

            ShortcutKeyHelper.Run(sender, e, () => RunSql());
        }
Exemple #2
0
        /// <summary>
        /// 执行sql
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void autoTextBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            string sql = ((TextBox)sender).Text.Trim();

            ShortcutKeyHelper.Run(sender, e, () =>
            {
                stopwatch.Restart();

                LoadForm.ShowLoading(this);

                Task.Factory.StartNew(() =>
                {
                    WEF.DbDAL.IDbObject dbObject = null;

                    if (ConnectionModel.DbType.Equals(DatabaseType.SqlServer.ToString()))
                    {
                        dbObject = new WEF.DbDAL.SQL2000.DbObject(ConnectionModel.ConnectionString);
                    }
                    else if (ConnectionModel.DbType.Equals(DatabaseType.SqlServer9.ToString()))
                    {
                        dbObject = new WEF.DbDAL.SQL2005.DbObject(ConnectionModel.ConnectionString);
                    }
                    else if (ConnectionModel.DbType.Equals(DatabaseType.MsAccess.ToString()))
                    {
                        dbObject = new WEF.DbDAL.OleDb.DbObject(ConnectionModel.ConnectionString);
                    }
                    else if (ConnectionModel.DbType.Equals(DatabaseType.Oracle.ToString()))
                    {
                        dbObject = new WEF.DbDAL.Oracle.DbObject(ConnectionModel.ConnectionString);
                    }
                    else if (ConnectionModel.DbType.Equals(DatabaseType.Sqlite3.ToString()))
                    {
                        dbObject = new WEF.DbDAL.SQLite.DbObject(ConnectionModel.ConnectionString);
                    }
                    else if (ConnectionModel.DbType.Equals(DatabaseType.MySql.ToString()))
                    {
                        dbObject = new WEF.DbDAL.MySql.DbObject(ConnectionModel.ConnectionString);
                    }
                    else
                    {
                        LoadForm.HideLoading(1);
                        MessageBox.Show("不支持的数据库类型!");
                        return;
                    }

                    if (string.IsNullOrEmpty(sql))
                    {
                        MessageBox.Show("sql内容不能为空!");
                        return;
                    }

                    this.Invoke(new Action(() =>
                    {
                        if (!string.IsNullOrWhiteSpace(((TextBox)sender).SelectedText))
                        {
                            sql = ((TextBox)sender).SelectedText.Trim();

                            if (string.IsNullOrEmpty(sql))
                            {
                                MessageBox.Show("sql内容不能为空!");
                                return;
                            }
                        }

                        dataGridView1.DataSource = null;
                    }));

                    if (string.Compare(sql, "select", true) >= 0)
                    {
                        try
                        {
                            int max = 50;

                            var dt = dbObject.GetTabData(ConnectionModel.Database, sql, max);

                            if (dt != null && dt.Rows.Count > 0)
                            {
                                var count = dt.Rows.Count;

                                if (count > max)
                                {
                                    for (int i = max; i < count; i++)
                                    {
                                        dt.Rows.RemoveAt(max);
                                    }
                                }


                                var dList = new List <int>();

                                for (int i = 0; i < dt.Columns.Count; i++)
                                {
                                    if (dt.Columns[i].DataType == typeof(DateTime))
                                    {
                                        dList.Add(i);
                                    }
                                }


                                dataGridView1.Invoke(new Action(() =>
                                {
                                    dataGridView1.DataSource = dt;

                                    if (dList.Any())
                                    {
                                        foreach (var item in dList)
                                        {
                                            dataGridView1.Columns[item].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss.fff";
                                        }
                                    }

                                    lbl_execute.Text = $"当前显示{(max > count ? count : max)}行,影响数据行数:{count} 耗时:{stopwatch.Elapsed.TotalSeconds} 秒";
                                }));
                            }
                        }
                        catch (Exception ex)
                        {
                            this.Invoke(new Action(() =>
                            {
                                MessageBox.Show(this, $"查询发生异常,ex:" + ex.Message);
                            }));
                        }
                    }
                    else
                    {
                        try
                        {
                            var count = dbObject.ExecuteSql(ConnectionModel.Database, sql);

                            lbl_execute.Invoke(new Action(() =>
                            {
                                lbl_execute.Text = $"影响数据行数:{count} 耗时:{stopwatch.Elapsed.TotalMilliseconds} 毫秒";
                            }));
                        }
                        catch (Exception ex)
                        {
                            this.Invoke(new Action(() =>
                            {
                                MessageBox.Show(this, $"操作发生异常,ex:" + ex.Message);
                            }));
                        }
                    }
                    LoadForm.HideLoading();
                });
            });

            ShortcutKeyHelper.AllSelect(sender, e);
        }