예제 #1
0
        private string ChangeIdentity(string tablesDdl)
        {
            List <string> ddl = new List <string>(Regex.Split(tablesDdl, @";\s*").Where(s => !string.IsNullOrWhiteSpace(s)));

            for (int i = 0; i < ddl.Count; i++)
            {
                string identity = null;
                Match  match    = Regex.Match(ddl[i], @"(?<=^CREATE TABLE \[)[^\]]*(?=\])", RegexOptions.Singleline);
                if (match.Success)
                {
                    identity = db.GetAutoincNext(match.Value);
                }
                if (identity != null)
                {
                    ddl[i] = Regex.Replace(ddl[i], @"(?<=\r\n\ *\[.*\].* IDENTITY \()\d+(?=,)", identity, RegexOptions.Singleline);
                }
            }

            StringBuilder sb = new StringBuilder();

            foreach (string s in ddl)
            {
                sb.AppendLine(s + ";" + Environment.NewLine);
            }
            return(sb.ToString());
        }
예제 #2
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());
        }