diff --git a/DbTools.csproj b/DbTools.csproj index 6b0c0c6..f9c1cad 100644 --- a/DbTools.csproj +++ b/DbTools.csproj @@ -55,6 +55,7 @@ + \ No newline at end of file diff --git a/DbToolsTester/DbToolsTester.csproj b/DbToolsTester/DbToolsTester.csproj index 91a7a98..f179189 100644 --- a/DbToolsTester/DbToolsTester.csproj +++ b/DbToolsTester/DbToolsTester.csproj @@ -43,6 +43,7 @@ + @@ -71,6 +72,12 @@ OpenForm.cs + + Form + + + TableChooserForm.cs + @@ -79,6 +86,9 @@ OpenForm.cs + + TableChooserForm.cs + ResXFileCodeGenerator diff --git a/DbToolsTester/Forms/MainForm.Designer.cs b/DbToolsTester/Forms/MainForm.Designer.cs index b7778d1..1a12456 100644 --- a/DbToolsTester/Forms/MainForm.Designer.cs +++ b/DbToolsTester/Forms/MainForm.Designer.cs @@ -36,6 +36,9 @@ this.bbiApplyBoth = new DevExpress.XtraBars.BarButtonItem(); this.bar2 = new DevExpress.XtraBars.Bar(); this.bar3 = new DevExpress.XtraBars.Bar(); + this.bar4 = new DevExpress.XtraBars.Bar(); + this.btsRemoveUnusedColumns = new DevExpress.XtraBars.BarToggleSwitchItem(); + this.btsRemoveUnusedTables = new DevExpress.XtraBars.BarToggleSwitchItem(); this.barDockControlTop = new DevExpress.XtraBars.BarDockControl(); this.barDockControlBottom = new DevExpress.XtraBars.BarDockControl(); this.barDockControlLeft = new DevExpress.XtraBars.BarDockControl(); @@ -48,8 +51,7 @@ this.xtraTabPage3 = new DevExpress.XtraTab.XtraTabPage(); this.deltaSql = new System.Windows.Forms.RichTextBox(); this.xtraTabPage4 = new DevExpress.XtraTab.XtraTabPage(); - this.btsRemoveUnusedTables = new DevExpress.XtraBars.BarToggleSwitchItem(); - this.btsRemoveUnusedColumns = new DevExpress.XtraBars.BarToggleSwitchItem(); + this.bbiBuildTableCreate = new DevExpress.XtraBars.BarButtonItem(); ((System.ComponentModel.ISupportInitialize)(this.barManager1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.XtraTabControl1)).BeginInit(); this.XtraTabControl1.SuspendLayout(); @@ -63,7 +65,8 @@ this.barManager1.Bars.AddRange(new DevExpress.XtraBars.Bar[] { this.bar1, this.bar2, - this.bar3}); + this.bar3, + this.bar4}); this.barManager1.DockControls.Add(this.barDockControlTop); this.barManager1.DockControls.Add(this.barDockControlBottom); this.barManager1.DockControls.Add(this.barDockControlLeft); @@ -74,9 +77,10 @@ this.bbiMigrations, this.bbiApplyBoth, this.btsRemoveUnusedTables, - this.btsRemoveUnusedColumns}); + this.btsRemoveUnusedColumns, + this.bbiBuildTableCreate}); this.barManager1.MainMenu = this.bar2; - this.barManager1.MaxItemId = 6; + this.barManager1.MaxItemId = 7; this.barManager1.StatusBar = this.bar3; // // bar1 @@ -89,8 +93,7 @@ new DevExpress.XtraBars.LinkPersistInfo(this.bbiDelta), new DevExpress.XtraBars.LinkPersistInfo(this.bbiMigrations), new DevExpress.XtraBars.LinkPersistInfo(this.bbiApplyBoth), - new DevExpress.XtraBars.LinkPersistInfo(this.btsRemoveUnusedTables, true), - new DevExpress.XtraBars.LinkPersistInfo(this.btsRemoveUnusedColumns)}); + new DevExpress.XtraBars.LinkPersistInfo(this.bbiBuildTableCreate)}); this.bar1.Text = "Tools"; // // bbiDelta @@ -136,13 +139,36 @@ this.bar3.OptionsBar.UseWholeRow = true; this.bar3.Text = "Status bar"; // + // bar4 + // + this.bar4.BarName = "Custom 5"; + this.bar4.DockCol = 0; + this.bar4.DockRow = 2; + this.bar4.DockStyle = DevExpress.XtraBars.BarDockStyle.Top; + this.bar4.LinksPersistInfo.AddRange(new DevExpress.XtraBars.LinkPersistInfo[] { + new DevExpress.XtraBars.LinkPersistInfo(this.btsRemoveUnusedColumns), + new DevExpress.XtraBars.LinkPersistInfo(this.btsRemoveUnusedTables)}); + this.bar4.Text = "Custom 5"; + // + // btsRemoveUnusedColumns + // + this.btsRemoveUnusedColumns.Caption = "Remove Unused Columns"; + this.btsRemoveUnusedColumns.Id = 5; + this.btsRemoveUnusedColumns.Name = "btsRemoveUnusedColumns"; + // + // btsRemoveUnusedTables + // + this.btsRemoveUnusedTables.Caption = "Remove Unused Tables"; + this.btsRemoveUnusedTables.Id = 4; + this.btsRemoveUnusedTables.Name = "btsRemoveUnusedTables"; + // // barDockControlTop // this.barDockControlTop.CausesValidation = false; this.barDockControlTop.Dock = System.Windows.Forms.DockStyle.Top; this.barDockControlTop.Location = new System.Drawing.Point(0, 0); this.barDockControlTop.Manager = this.barManager1; - this.barDockControlTop.Size = new System.Drawing.Size(840, 41); + this.barDockControlTop.Size = new System.Drawing.Size(840, 62); // // barDockControlBottom // @@ -156,17 +182,17 @@ // this.barDockControlLeft.CausesValidation = false; this.barDockControlLeft.Dock = System.Windows.Forms.DockStyle.Left; - this.barDockControlLeft.Location = new System.Drawing.Point(0, 41); + this.barDockControlLeft.Location = new System.Drawing.Point(0, 62); this.barDockControlLeft.Manager = this.barManager1; - this.barDockControlLeft.Size = new System.Drawing.Size(0, 417); + this.barDockControlLeft.Size = new System.Drawing.Size(0, 396); // // barDockControlRight // this.barDockControlRight.CausesValidation = false; this.barDockControlRight.Dock = System.Windows.Forms.DockStyle.Right; - this.barDockControlRight.Location = new System.Drawing.Point(840, 41); + this.barDockControlRight.Location = new System.Drawing.Point(840, 62); this.barDockControlRight.Manager = this.barManager1; - this.barDockControlRight.Size = new System.Drawing.Size(0, 417); + this.barDockControlRight.Size = new System.Drawing.Size(0, 396); // // XtraTabControl1 // @@ -176,10 +202,10 @@ new DevExpress.XtraTab.Buttons.CustomHeaderButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "Open Databases", -1, true, true, editorButtonImageOptions1, serializableAppearanceObject1, "", "open", null), new DevExpress.XtraTab.Buttons.CustomHeaderButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "Clear All", -1, true, true, editorButtonImageOptions2, serializableAppearanceObject2, "", "clear", null)}); this.XtraTabControl1.Dock = System.Windows.Forms.DockStyle.Fill; - this.XtraTabControl1.Location = new System.Drawing.Point(0, 41); + this.XtraTabControl1.Location = new System.Drawing.Point(0, 62); this.XtraTabControl1.Name = "XtraTabControl1"; this.XtraTabControl1.SelectedTabPage = this.xtraTabPage1; - this.XtraTabControl1.Size = new System.Drawing.Size(840, 417); + this.XtraTabControl1.Size = new System.Drawing.Size(840, 396); this.XtraTabControl1.TabIndex = 4; this.XtraTabControl1.TabPages.AddRange(new DevExpress.XtraTab.XtraTabPage[] { this.xtraTabPage1, @@ -192,7 +218,7 @@ // this.xtraTabPage1.Controls.Add(this.db1Sql); this.xtraTabPage1.Name = "xtraTabPage1"; - this.xtraTabPage1.Size = new System.Drawing.Size(838, 375); + this.xtraTabPage1.Size = new System.Drawing.Size(838, 354); this.xtraTabPage1.Text = "Live / Production DB"; // // db1Sql @@ -202,7 +228,7 @@ this.db1Sql.Location = new System.Drawing.Point(0, 0); this.db1Sql.Name = "db1Sql"; this.db1Sql.ReadOnly = true; - this.db1Sql.Size = new System.Drawing.Size(838, 375); + this.db1Sql.Size = new System.Drawing.Size(838, 354); this.db1Sql.TabIndex = 0; this.db1Sql.Text = "This database is the one that is currently in use by the application. This databa" + "se needs to be updated to fit the most recent schema."; @@ -211,7 +237,7 @@ // this.xtraTabPage2.Controls.Add(this.db2Sql); this.xtraTabPage2.Name = "xtraTabPage2"; - this.xtraTabPage2.Size = new System.Drawing.Size(838, 375); + this.xtraTabPage2.Size = new System.Drawing.Size(838, 354); this.xtraTabPage2.Text = "Schema / Updated DB"; // // db2Sql @@ -221,7 +247,7 @@ this.db2Sql.Location = new System.Drawing.Point(0, 0); this.db2Sql.Name = "db2Sql"; this.db2Sql.ReadOnly = true; - this.db2Sql.Size = new System.Drawing.Size(838, 375); + this.db2Sql.Size = new System.Drawing.Size(838, 354); this.db2Sql.TabIndex = 1; this.db2Sql.Text = resources.GetString("db2Sql.Text"); // @@ -229,7 +255,7 @@ // this.xtraTabPage3.Controls.Add(this.deltaSql); this.xtraTabPage3.Name = "xtraTabPage3"; - this.xtraTabPage3.Size = new System.Drawing.Size(838, 375); + this.xtraTabPage3.Size = new System.Drawing.Size(838, 354); this.xtraTabPage3.Text = "Delta SQL"; // // deltaSql @@ -239,7 +265,7 @@ this.deltaSql.Location = new System.Drawing.Point(0, 0); this.deltaSql.Name = "deltaSql"; this.deltaSql.ReadOnly = true; - this.deltaSql.Size = new System.Drawing.Size(838, 375); + this.deltaSql.Size = new System.Drawing.Size(838, 354); this.deltaSql.TabIndex = 1; this.deltaSql.Text = "This SQL script contains all of the SQL commands required to make the production " + "(existing) database the same as the new template database."; @@ -247,20 +273,15 @@ // xtraTabPage4 // this.xtraTabPage4.Name = "xtraTabPage4"; - this.xtraTabPage4.Size = new System.Drawing.Size(838, 375); + this.xtraTabPage4.Size = new System.Drawing.Size(838, 354); this.xtraTabPage4.Text = "Migrations"; // - // btsRemoveUnusedTables + // bbiBuildTableCreate // - this.btsRemoveUnusedTables.Caption = "Remove Unused Tables"; - this.btsRemoveUnusedTables.Id = 4; - this.btsRemoveUnusedTables.Name = "btsRemoveUnusedTables"; - // - // btsRemoveUnusedColumns - // - this.btsRemoveUnusedColumns.Caption = "Remove Unused Columns"; - this.btsRemoveUnusedColumns.Id = 5; - this.btsRemoveUnusedColumns.Name = "btsRemoveUnusedColumns"; + this.bbiBuildTableCreate.Caption = "Build Table Create Script"; + this.bbiBuildTableCreate.Id = 6; + this.bbiBuildTableCreate.Name = "bbiBuildTableCreate"; + this.bbiBuildTableCreate.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.ToolbarItemClicked); // // MainForm // @@ -309,5 +330,7 @@ private System.Windows.Forms.RichTextBox deltaSql; private DevExpress.XtraBars.BarToggleSwitchItem btsRemoveUnusedTables; private DevExpress.XtraBars.BarToggleSwitchItem btsRemoveUnusedColumns; + private DevExpress.XtraBars.Bar bar4; + private DevExpress.XtraBars.BarButtonItem bbiBuildTableCreate; } } \ No newline at end of file diff --git a/DbToolsTester/Forms/MainForm.cs b/DbToolsTester/Forms/MainForm.cs index d15d8a5..8ee03e5 100644 --- a/DbToolsTester/Forms/MainForm.cs +++ b/DbToolsTester/Forms/MainForm.cs @@ -80,6 +80,19 @@ namespace DbToolsTester.Forms { MessageBox.Show("Failed to apply delta." + (delta.LastError != null ? "\r\n\r\n" + delta.LastError.Message : ""), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + } else if (e.Item.Equals(bbiBuildTableCreate)) { + TableChooserForm frm = new TableChooserForm(); + if (frm.ShowDialog(this) == DialogResult.OK) { + using (var cn = new SQLiteConnection("Data Source=" + (frm.SelectedDatabase == Path.GetFileName(Database1File) ? Database1File : Database2File) + ";Version=3;")) { + SqlBuilder builder = new SqlBuilder(); + string sql = builder.GetTableCreateSql(cn, frm.SelectedTable); + if (!string.IsNullOrEmpty(sql)) { + deltaSql.Text = sql; + } else { + MessageBox.Show("Failed to get CREATE TABLE SQL for table " + frm.SelectedTable, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } } } diff --git a/DbToolsTester/Forms/OpenForm.cs b/DbToolsTester/Forms/OpenForm.cs index 43e0d62..930c335 100644 --- a/DbToolsTester/Forms/OpenForm.cs +++ b/DbToolsTester/Forms/OpenForm.cs @@ -11,7 +11,7 @@ namespace DbToolsTester.Forms { } private void btnOpen_Click(object sender, EventArgs e) { - MainForm.Instance.SetDatabases(Db1File.Text, db2File.Text); + //MainForm.Instance.SetDatabases(Db1File.Text, db2File.Text); } private void Db1File_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { diff --git a/DbToolsTester/Forms/TableChooserForm.Designer.cs b/DbToolsTester/Forms/TableChooserForm.Designer.cs new file mode 100644 index 0000000..7684a73 --- /dev/null +++ b/DbToolsTester/Forms/TableChooserForm.Designer.cs @@ -0,0 +1,93 @@ +namespace DbToolsTester.Forms { + partial class TableChooserForm { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) { + if (disposing && (components != null)) { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() { + this.treeList1 = new DevExpress.XtraTreeList.TreeList(); + this.btnOK = new DevExpress.XtraEditors.SimpleButton(); + this.btnCancel = new DevExpress.XtraEditors.SimpleButton(); + ((System.ComponentModel.ISupportInitialize)(this.treeList1)).BeginInit(); + this.SuspendLayout(); + // + // treeList1 + // + this.treeList1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left))); + this.treeList1.Location = new System.Drawing.Point(0, 0); + this.treeList1.Name = "treeList1"; + this.treeList1.OptionsBehavior.Editable = false; + this.treeList1.OptionsBehavior.ReadOnly = true; + this.treeList1.OptionsView.ShowColumns = false; + this.treeList1.OptionsView.ShowHorzLines = false; + this.treeList1.OptionsView.ShowIndicator = false; + this.treeList1.OptionsView.ShowVertLines = false; + this.treeList1.Size = new System.Drawing.Size(456, 303); + this.treeList1.TabIndex = 0; + this.treeList1.FocusedNodeChanged += new DevExpress.XtraTreeList.FocusedNodeChangedEventHandler(this.treeList1_FocusedNodeChanged); + // + // btnOK + // + this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK; + this.btnOK.Location = new System.Drawing.Point(222, 309); + this.btnOK.Name = "btnOK"; + this.btnOK.Size = new System.Drawing.Size(108, 36); + this.btnOK.TabIndex = 1; + this.btnOK.Text = "&OK"; + // + // btnCancel + // + this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; + this.btnCancel.Location = new System.Drawing.Point(336, 309); + this.btnCancel.Name = "btnCancel"; + this.btnCancel.Size = new System.Drawing.Size(108, 36); + this.btnCancel.TabIndex = 2; + this.btnCancel.Text = "&Cancel"; + // + // TableChooserForm + // + this.AcceptButton = this.btnOK; + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.CancelButton = this.btnCancel; + this.ClientSize = new System.Drawing.Size(456, 357); + this.Controls.Add(this.btnCancel); + this.Controls.Add(this.btnOK); + this.Controls.Add(this.treeList1); + this.Name = "TableChooserForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; + this.Text = "Choose Table ..."; + this.Load += new System.EventHandler(this.TableChooserForm_Load); + ((System.ComponentModel.ISupportInitialize)(this.treeList1)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DevExpress.XtraTreeList.TreeList treeList1; + private DevExpress.XtraEditors.SimpleButton btnOK; + private DevExpress.XtraEditors.SimpleButton btnCancel; + } +} \ No newline at end of file diff --git a/DbToolsTester/Forms/TableChooserForm.cs b/DbToolsTester/Forms/TableChooserForm.cs new file mode 100644 index 0000000..cb65898 --- /dev/null +++ b/DbToolsTester/Forms/TableChooserForm.cs @@ -0,0 +1,71 @@ +using DbTools; +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SQLite; +using System.IO; +using System.Linq; + +namespace DbToolsTester.Forms { + public partial class TableChooserForm : DevExpress.XtraEditors.XtraForm { + List TreeItems = new List(); + + public string SelectedTable { get; private set; } + public string SelectedDatabase { get; private set; } + + public TableChooserForm() { + InitializeComponent(); + } + + private int AddTreeItem(string text, int parentId = 0) { + TreeItems.Add(new TreeItem(text, TreeItems.Count + 1, parentId)); + return TreeItems.Count; + } + + private void TableChooserForm_Load(object sender, EventArgs e) { + SqlBuilder builder = new SqlBuilder(); + int db1 = AddTreeItem(Path.GetFileName(MainForm.Instance.Database1File)); + int db2 = AddTreeItem(Path.GetFileName(MainForm.Instance.Database2File)); + + using (SQLiteConnection cn = new SQLiteConnection("Data Source=" + MainForm.Instance.Database1File + "; Version=3;")) { + string[] tables = builder.GetTableNames(cn).OrderBy(f => f).ToArray(); + foreach (string table in tables) { + AddTreeItem(table, db1); + } + } + + using (SQLiteConnection cn = new SQLiteConnection("Data Source=" + MainForm.Instance.Database2File + "; Version=3;")) { + string[] tables = builder.GetTableNames(cn).OrderBy(f => f).ToArray(); + foreach (string table in tables) { + AddTreeItem(table, db2); + } + } + + treeList1.DataSource = TreeItems; + treeList1.ExpandAll(); + } + + private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e) { + if (e.Node == null || e.Node.ParentNode == null) { + SelectedTable = null; + SelectedDatabase = null; + return; + } + SelectedDatabase = e.Node.ParentNode.GetValue(0).ToString(); + SelectedTable = e.Node.GetValue(0).ToString(); + } + } + + internal class TreeItem { + public int ID { get; set; } + public int ParentID { get; set; } + public string Text { get; set; } + + public TreeItem() { } + public TreeItem(string text, int id, int parentId = 0) { + ID = id; + ParentID = parentId; + Text = text; + } + } +} \ No newline at end of file diff --git a/DbToolsTester/Forms/TableChooserForm.resx b/DbToolsTester/Forms/TableChooserForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/DbToolsTester/Forms/TableChooserForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/DbToolsTester/Properties/licenses.licx b/DbToolsTester/Properties/licenses.licx index 12db65a..dcfd26d 100644 --- a/DbToolsTester/Properties/licenses.licx +++ b/DbToolsTester/Properties/licenses.licx @@ -1,3 +1,4 @@ DevExpress.XtraEditors.ButtonEdit, DevExpress.XtraEditors.v23.2, Version=23.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a DevExpress.XtraLayout.LayoutControl, DevExpress.XtraLayout.v23.2, Version=23.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a +DevExpress.XtraTreeList.TreeList, DevExpress.XtraTreeList.v23.2, Version=23.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a DevExpress.XtraBars.BarManager, DevExpress.XtraBars.v23.2, Version=23.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a diff --git a/SqlBuilder.cs b/SqlBuilder.cs new file mode 100644 index 0000000..083619a --- /dev/null +++ b/SqlBuilder.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Text; + +namespace DbTools { + public class SqlBuilder { + + public string[] GetTableNames(IDbConnection cn) { + if (cn == null) { + throw new ArgumentNullException("cn"); + } + List tables = new List(); + if (cn.State != ConnectionState.Open) { + cn.Open(); + } + using (IDbCommand cmd = cn.CreateCommand()) { + cmd.CommandText = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name;"; + using (IDataReader reader = cmd.ExecuteReader()) { + while (reader.Read()) { + tables.Add(reader.GetString(0)); + } + } + } + return tables.ToArray(); + } + + public string GetTableCreateSql(IDbConnection cn, string tableName, string selectStatement = "select * from $table") { + if (cn == null) { + throw new ArgumentNullException("cn"); + } + if (string.IsNullOrEmpty(tableName)) { + throw new ArgumentNullException("tableName"); + } + + if (cn.State != ConnectionState.Open) { + cn.Open(); + } + selectStatement = selectStatement.Replace("$table", tableName); + using (IDbCommand cmd = cn.CreateCommand()) { + cmd.CommandText = selectStatement; + using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo)) { + DataTable schemaTable = reader.GetSchemaTable(); + if (schemaTable == null) { + throw new Exception("Could not get schema for table " + tableName); + } + StringBuilder sb = new StringBuilder(); + sb.AppendLine("CREATE TABLE " + tableName + " ("); + List pkColumns = new List(); + foreach (DataRow row in schemaTable.Rows) { + string columnName = row["ColumnName"].ToString(); + string dataType = row["DataTypeName"].ToString(); + int columnSize = Convert.ToInt32(row["ColumnSize"]); + bool allowDBNull = Convert.ToBoolean(row["AllowDBNull"]); + bool isKey = Convert.ToBoolean(row["IsKey"]); + if (isKey) { + pkColumns.Add(columnName); + } + sb.Append(" " + columnName + " " + dataType); + if (dataType.Equals("VARCHAR", StringComparison.OrdinalIgnoreCase) || + dataType.Equals("CHAR", StringComparison.OrdinalIgnoreCase) || + dataType.Equals("NVARCHAR", StringComparison.OrdinalIgnoreCase) || + dataType.Equals("NCHAR", StringComparison.OrdinalIgnoreCase)) { + sb.Append("(" + columnSize + ")"); + } + if (!allowDBNull) { + sb.Append(" NOT NULL"); + } + sb.AppendLine(","); + } + if (pkColumns.Count > 0) { + sb.AppendLine(" PRIMARY KEY (" + string.Join(", ", pkColumns) + ")"); + } else { + // Remove the last comma + sb.Length -= 3; + sb.AppendLine(); + } + sb.AppendLine(");"); + return sb.ToString(); + } + } + } + } +}