private void copyAsCDefinitionToClipbrdToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var node = tvMain.SelectedNode;

            if (node == null || node.Tag == null || node.Tag.GetType() != typeof(SqlDataTable))
            {
                return;
            }
            var table = node.Tag as SqlDataTable;

            using (var tw = new StreamWriter(new MemoryStream()))
            {
                tw.WriteLine(string.Format("[Table(\"{0}\")]", table.Name));
                tw.WriteLine("public class " + table.Name);
                tw.WriteLine("{");
                tw.WriteLine("");

                foreach (var c in table.Columns)
                {
                    tw.WriteLine("\t/// <summary>");
                    tw.WriteLine("\t/// Gets or sets the " + c.Name);
                    tw.WriteLine("\t/// </summary>");

                    if (c.IsPKey)
                    {
                        tw.WriteLine("\t[Key]");
                    }

                    if (!c.Nullable)
                    {
                        tw.WriteLine("\t[Required]");
                    }

                    var tName = SqlDataColumn.SqlTypeToType(c.Type).Name;
                    if (tName == "Int32")
                    {
                        tName = "int";
                    }

                    tw.WriteLine(string.Format("\t[Column(\"{0}\")]", c.Name));
                    tw.WriteLine(string.Format("\tpublic {0} {1} {{ get; set; }}", tName, c.Name));
                    tw.WriteLine("");
                }

                tw.WriteLine("}");
                tw.Flush();

                using (var sr = new StreamReader(tw.BaseStream))
                {
                    sr.BaseStream.Seek(0, 0);
                    var txt = sr.ReadToEnd();
                    var frm = new FormJustText {
                        Text = "C# Class " + table.Name, Value = txt.Split('\n')
                    };
                    frm.MdiParent = this;
                    frm.Visible   = true;
                }
            }
        }
Пример #2
0
        private void EnsureColumns()
        {
            if (sourceTable != null && (sourceTable.Columns == null || sourceTable.Columns.Count == 0))
            {
                using (var con = new SqlDatabaseConnection(sourceTable.ConnectionString))
                {
                    try
                    {
                        con.Open();
                        sourceTable.GetColumns(con);
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }

            if (this.table.Columns.Count == 0)
            {
                this.table.BeginInit();
                // happens when there is no data in the table
                foreach (var c in sourceTable.Columns)
                {
                    var dt  = SqlDataColumn.SqlTypeToType(c.Type);
                    var col = new DataColumn
                    {
                        AllowDBNull   = c.Nullable,
                        AutoIncrement = c.AutoInc,
                        Caption       = c.Name,
                        DataType      = dt,
                        ColumnName    = c.Name
                                        // DefaultValue = c.DefaultValue
                    };

                    var defaultString = Convert.ToString(c.DefaultValue);
                    if (defaultString.StartsWith("'") && defaultString.EndsWith("'"))
                    {
                        defaultString = defaultString.Substring(1, defaultString.Length - 2);
                    }

                    if (!string.IsNullOrEmpty(defaultString))
                    {
                        var defVal = Convert.ChangeType(defaultString, dt);
                        col.DefaultValue = defVal;
                    }

                    table.Columns.Add(col);
                }

                this.table.AcceptChanges();
                this.table.EndInit();
            }
        }
Пример #3
0
        private bool validateRow(DataGridViewRow row)
        {
            if (row == null)
            {
                return(false);
            }
            var rowError = string.Empty;

            for (var i = 0; i < sourceTable.Columns.Count; i++)
            {
                var c    = sourceTable.Columns[i];
                var cell = row.Cells[i];
                var typ  = SqlDataColumn.SqlTypeToType(c.Type);
                var val  = Convert.ToString(cell.Value);
                if (!c.Nullable && string.IsNullOrEmpty(val))
                {
                    cell.ErrorText = string.Format("Cell \"{0}\" needs a value", c.Name);
                    rowError      += cell.ErrorText + "\n";
                }
                else
                {
                    cell.ErrorText = null;
                }
            }

            rowError = rowError.Trim();
            if (!string.IsNullOrEmpty(rowError))
            {
                row.ErrorText = rowError;
                return(true);
            }
            else
            {
                row.ErrorText = null;
            }

            return(false);
        }