Exemplo n.º 1
0
        public ExportForm(SqlCeDb sqlCeDb, TreeNode dbNode)
        {
            InitializeComponent();

            SetCultureTexts();

            db          = sqlCeDb;
            this.dbNode = dbNode;
            if (dbNode.ImageIndex == 1)
            {
                saveFileDialog1.Filter = saveFileDialog1.Filter.Replace("All files|*.*", "CSV files|*.csv|All files|*.*");
            }
            int nItems = FillTreeDb();

            int maxHeight = Screen.PrimaryScreen.WorkingArea.Height - 20;
            int height    = nItems * treeDb.ItemHeight + 90;

            if (height < 400)
            {
                height = 400;
            }
            if (height > maxHeight)
            {
                height = maxHeight;
            }
            this.Height = height;

            DoubleClickIntercept doubleClickIntercept = new DoubleClickIntercept(treeDb.Handle);  // Disable double click to avoid TreeView double click check bug
        }
Exemplo n.º 2
0
        public ImportForm(SqlCeDb sqlCeDb, string fileName)
        {
            InitializeComponent();

            SetCultureTexts();
            statusStrip1_SizeChanged(null, null);

            db = sqlCeDb;
            FillTreeDb(fileName);
        }
Exemplo n.º 3
0
        private string GetExportConstraintsSchema(SqlCeDb db, TreeView treeView)
        {
            List <string> ddl = new List <string>(Regex.Split(db.GetDatabaseDdl(), @";\s*").Where(s => !string.IsNullOrWhiteSpace(s)));

            for (int i = ddl.Count - 1; i >= 0; i--)
            {
                if (ddl[i].StartsWith("CREATE TABLE "))
                {
                    ddl.RemoveAt(i);
                }
            }
            return(RemoveUncheck(ddl, treeView));
        }
Exemplo n.º 4
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());
        }
Exemplo n.º 5
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());
        }