private void treeDb_AfterSelect(object sender, TreeViewEventArgs e) { if (!db.IsOpen) { return; } dataGrid.SuspendLayout(); if (e.Node.ImageIndex == 0) { // Database node dataGrid.ReadOnly = true; dataGrid.DataSource = DatabaseInfoLocale(db.DatabaseInfo); rtbDdl.Text = db.GetDatabaseDdl(); sqlParserDdl.Update(); } else { // Table node dataGrid.ReadOnly = cbReadOnly.SelectedIndex == 0; dataGrid.Columns.Clear(); if (tabControl1.SelectedIndex == 0) { dataGrid.DataSource = db.GetTableData(e.Node.Text, null, SortOrder.None); rtbDdl.Text = db.GetTableDdl(e.Node.Name, true, true, true, true); } else { rtbDdl.Text = db.GetTableDdl(e.Node.Name, true, true, true, true); dataGrid.DataSource = db.GetTableData(e.Node.Text, null, SortOrder.None); } } dataGrid.AllowUserToAddRows = dataGrid.AllowUserToDeleteRows = !dataGrid.ReadOnly; dataGrid.ResumeLayout(); }
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()); }