Пример #1
0
        private void btnExecute_Click(object sender, EventArgs e)
        {
            if (rtbQuery.Text.Trim().Length == 0)
            {
                return;
            }
            bool partial = rtbQuery.SelectedText.Trim().Length > 0;

            if (partial)
            {
                DialogResult result = MessageBox.Show(GlobalText.GetValue("SelectedTextQuery"), GlobalText.GetValue("Confirm"),
                                                      MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (result == DialogResult.Cancel)
                {
                    return;
                }
                partial = result == DialogResult.Yes;
            }
            string sql = partial ? rtbQuery.SelectedText.Trim() : rtbQuery.Text.Trim();

            dataGrid.DataSource = null;

            // Initial time
            DateTime initTime = DateTime.Now;

            object resultSet = db.ExecuteSql(sql, false);

            long ms = (long)(DateTime.Now - initTime).TotalMilliseconds;

            dataGrid.DataSource = resultSet;

            if (resultSet != null || string.IsNullOrEmpty(db.LastError))
            {
                lbResult.ForeColor = Color.Black;
                lbResult.Text      = string.Format("{0} {1}, {2} {3}, {4} {5}", db.QueryCount, GlobalText.GetValue("Querys"), dataGrid.RowCount,
                                                   GlobalText.GetValue("Rows"), ms, GlobalText.GetValue("Milliseconds"));
                if (resultSet == null && regexCreateAlterDrop.IsMatch(regexDropQuotesAndBrackets.Replace(rtbQuery.Text, "")))
                {
                    db.ResetDdl();  // Update DDL
                    UpdateTreeDb();
                }
                else
                {
                    tabControl1.SelectedIndex = 0;
                    if (resultSet == null && regexInsertUpdateDelete.IsMatch(regexDropQuotesAndBrackets.Replace(rtbQuery.Text, "")))
                    {
                        TreeNode node = treeDb.SelectedNode;  // Update data view
                        treeDb.SelectedNode = null;
                        treeDb.SelectedNode = node;
                    }
                }
            }
            else
            {
                lbResult.ForeColor = Color.Red;
                lbResult.Text      = db.LastError;
            }
        }
Пример #2
0
        private string GetCsvExportData(SqlCeDb db, TreeView treeView, bool titles, bool data)
        {
            int ncount = 0;

            foreach (TreeNode node in treeView.Nodes)
            {
                if (node.Checked)
                {
                    ncount++;
                }
            }
            if (ncount != 1)
            {
                return(null);
            }

            StringBuilder csv = new StringBuilder();

            foreach (TreeNode node in treeView.Nodes)
            {
                StringBuilder sb       = new StringBuilder();
                StringBuilder sbTitles = new StringBuilder();
                int           count    = 0;
                if (node.Checked)
                {
                    foreach (TreeNode n in node.Nodes)
                    {
                        if (n.Checked)
                        {
                            sbTitles.Append("\"" + n.Text + "\",");
                            sb.Append("[" + n.Text + "], ");
                            count++;
                        }
                    }
                }
                if (count == 0)
                {
                    continue;
                }
                if (titles)
                {
                    csv.AppendLine(sbTitles.ToString().TrimEnd(','));
                }
                if (!data)
                {
                    continue;
                }

                string      fields    = sb.ToString().TrimEnd(',', ' ');
                string      sqlSelect = "SELECT " + fields + " FROM [" + node.Text + "]";
                IDataReader dr        = (IDataReader)db.ExecuteSql(sqlSelect, false);
                object[]    values    = new object[count];
                while (dr.Read())
                {
                    dr.GetValues(values);
                    for (int i = 0; i < count; i++)
                    {
                        string s;
                        if (dr.IsDBNull(i))
                        {
                            s = "";
                        }
                        else
                        {
                            CultureInfo ci = Thread.CurrentThread.CurrentCulture;
                            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                            Type t       = values[i].GetType();
                            bool numeric = t == typeof(Byte) || t == typeof(Int16) || t == typeof(Int32) ||
                                           t == typeof(Int64) || t == typeof(Decimal) || t == typeof(Double) || t == typeof(Single);
                            if (t == typeof(Byte[]))
                            {
                                s = "0x" + BitConverter.ToString(values[i] as Byte[]).Replace("-", "");
                            }
                            else if (t == typeof(DateTime))
                            {
                                s = ((DateTime)values[i]).ToString("yyyy.MM.dd HH:mm:ss.fff");
                            }
                            else
                            {
                                s = numeric ? values[i].ToString() : values[i].ToString().Replace("\"", "''");
                            }
                            Thread.CurrentThread.CurrentCulture = ci;
                        }
                        csv.Append("\"" + s + "\"");
                        if (i < count - 1)
                        {
                            csv.Append(",");
                        }
                    }
                    csv.AppendLine();
                }
                dr.Close();
            }
            return(csv.ToString());
        }
Пример #3
0
        private string GetExportData(SqlCeDb db, TreeView treeView)
        {
            StringBuilder ddl = new StringBuilder();

            foreach (TreeNode node in treeView.Nodes)
            {
                StringBuilder sb    = new StringBuilder();
                int           count = 0;
                if (node.Checked)
                {
                    foreach (TreeNode n in node.Nodes)
                    {
                        if (n.Checked)
                        {
                            sb.Append("[" + n.Text + "], ");
                            count++;
                        }
                    }
                }
                if (count == 0)
                {
                    continue;
                }

                string schema   = db.GetTableDdl(node.Text, true, false, false, false);
                string identity = db.GetAutoincNext(node.Text);
                if (identity != null)
                {
                    ddl.AppendLine("SET IDENTITY_INSERT [" + node.Text + "] ON;" + Environment.NewLine);
                }

                string      fields    = sb.ToString().TrimEnd(',', ' ');
                string      sqlSelect = "SELECT " + fields + " FROM [" + node.Text + "]";
                IDataReader dr        = (IDataReader)db.ExecuteSql(sqlSelect, false);
                object[]    values    = new object[count];
                while (dr.Read())
                {
                    ddl.Append("INSERT INTO [" + node.Text + "] (" + fields + ") VALUES (");
                    dr.GetValues(values);
                    for (int i = 0; i < count; i++)
                    {
                        string s;
                        if (dr.IsDBNull(i))
                        {
                            s = "NULL";
                        }
                        else
                        {
                            CultureInfo ci = Thread.CurrentThread.CurrentCulture;
                            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                            Type t       = values[i].GetType();
                            bool numeric = t == typeof(Byte) || t == typeof(Int16) || t == typeof(Int32) ||
                                           t == typeof(Int64) || t == typeof(Decimal) || t == typeof(Double) || t == typeof(Single);
                            if (t == typeof(Byte[]))
                            {
                                s = "0x" + BitConverter.ToString(values[i] as Byte[]).Replace("-", "");
                            }
                            else if (t == typeof(DateTime))
                            {
                                s = "'" + ((DateTime)values[i]).ToString("yyyy.MM.dd HH:mm:ss.fff") + "'";
                            }
                            else
                            {
                                s = numeric ? values[i].ToString() : "'" + values[i].ToString().Replace("'", "''") + "'";
                            }
                            Thread.CurrentThread.CurrentCulture = ci;
                        }
                        ddl.Append(s);
                        if (i < count - 1)
                        {
                            ddl.Append(", ");
                        }
                    }
                    ddl.AppendLine(");" + Environment.NewLine);
                }
                dr.Close();

                if (identity != null)
                {
                    ddl.AppendLine("SET IDENTITY_INSERT [" + node.Text + "] OFF;" + Environment.NewLine);
                }
            }
            return(ddl.ToString());
        }