|
using System.Text;
using System.ComponentModel;
namespace ObjectHelper.DBObjectType
{
public class Column : BaseDbObject
{
public int ColumnId { get; set; }
public bool IsNullable { get; set; }
public bool IsComputed { get; set; }
public bool IsIdentity { get; set; }
public bool IsFileStream { get; set; }
public bool IsReplicated { get; set; }
public bool IsSparse { get; set; }
public bool IsRowGuidCol { get; set; }
public bool IsPersisted { get; set; }
public bool IsUnique { get; set; }
public bool IsColumnSet { get; set; }
public string Definition { get; set; }
[DisplayName("Data type")]
public string DataType { get; set; }
public int DataTypeId { get; set; }
public int Precision { get; set; }
public int Scale { get; set; }
public int IdentitySeed { get; set; }
public int IdentityIncrement { get; set; }
public string Collation { get; set; }
public bool IsUserDefinedDataType { get; set; }
public int SystemDataTypeId { get; set; }
public string Script(ScriptingOptions so)
{
var sbScript = new StringBuilder();
sbScript.Append("[" + Name + "] ");
if (IsComputed)
{
sbScript.Append("AS " + Definition + " ");
if (IsPersisted)
{
sbScript.Append("PERSISTED ");
}
}
else
{
switch (DataType.ToUpper())
{
case "IMAGE":
sbScript.Append("[" + DataType + "] ");
break;
case "TEXT":
sbScript.Append("[" + DataType + "] ");
break;
case "UNIQUEIDENTIFIER":
sbScript.Append("[" + DataType + "] ");
break;
case "DATE":
sbScript.Append("[" + DataType + "] ");
break;
case "TIME":
if (Precision == -1)
{
sbScript.Append("[" + DataType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + DataType + "]" + "(" + Scale + ") ");
}
break;
case "DATETIME2":
if (Precision == -1)
{
sbScript.Append("[" + DataType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + DataType + "]" + "(" + Scale + ") ");
}
break;
case "DATETIMEOFFSET":
if (Precision == -1)
{
sbScript.Append("[" + DataType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + DataType + "]" + "(" + Scale + ") ");
}
break;
case "TINYINT":
sbScript.Append("[" + DataType + "] ");
break;
case "SMALLINT":
sbScript.Append("[" + DataType + "] ");
break;
case "REAL":
sbScript.Append("[" + DataType + "] ");
break;
case "MONEY":
sbScript.Append("[" + DataType + "] ");
break;
case "DATETIME":
sbScript.Append("[" + DataType + "] ");
break;
case "SMALLDATETIME":
sbScript.Append("[" + DataType + "] ");
break;
case "INT":
sbScript.Append("[" + DataType + "] ");
break;
case "FLOAT":
sbScript.Append("[" + DataType + "] ");
break;
case "SQL_VARIANT":
sbScript.Append("[" + DataType + "] ");
break;
case "NTEXT":
sbScript.Append("[" + DataType + "] ");
break;
case "BIT":
sbScript.Append("[" + DataType + "] ");
break;
case "DECIMAL":
sbScript.Append("[" + DataType + "]" + " (" + Precision + "," + Scale + ") ");
break;
case "NUMERIC":
sbScript.Append("[" + DataType + "]" + " (" + Precision + "," + Scale + ") ");
break;
case "SMALLMONEY":
sbScript.Append("[" + DataType + "] ");
break;
case "BIGINT":
sbScript.Append("[" + DataType + "] ");
break;
case "HIERARCHYID":
sbScript.Append("[" + DataType + "] ");
break;
case "GEOMETRY":
sbScript.Append("[" + DataType + "] ");
break;
case "GEOGRAPHY":
sbScript.Append("[" + DataType + "] ");
break;
case "VARBINARY":
if (Precision == -1)
{
sbScript.Append("[" + DataType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + DataType + "]" + "(" + Precision + ") ");
}
break;
case "VARCHAR":
if (Precision == -1)
{
sbScript.Append("[" + DataType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + DataType + "]" + "(" + Precision + ") ");
}
break;
case "BINARY":
if (Precision == -1)
{
sbScript.Append("[" + DataType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + DataType + "]" + "(" + Precision + ") ");
}
break;
case "CHAR":
if (Precision == -1)
{
sbScript.Append("[" + DataType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + DataType + "]" + "(" + Precision + ") ");
}
break;
case "TIMESTAMP":
sbScript.Append("[" + DataType + "]" + " ");
break;
case "NVARCHAR":
if (Precision == -1)
{
sbScript.Append("[" + DataType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + DataType + "]" + "(" + Precision + ") ");
}
break;
case "NCHAR":
if (Precision == -1)
{
sbScript.Append("[" + DataType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + DataType + "]" + "(" + Precision + ") ");
}
break;
case "XML":
sbScript.Append("[" + DataType + "] ");
if (IsColumnSet)
{
sbScript.Append(" COLUMN_SET FOR ALL_SPARSE_COLUMNS ");
}
break;
case "SYSNAME":
sbScript.Append("[" + DataType + "] ");
break;
default:
sbScript.Append("[" + DataType + "] ");
break;
}
if (so.Collation)
{
if (Collation != "")
{
if (!IsUserDefinedDataType)
{
sbScript.Append("COLLATE " + Collation + " ");
}
}
}
if (!so.NoIdentities)
{
if (IsIdentity)
{
sbScript.Append("IDENTITY(" + IdentitySeed + "," + IdentityIncrement + ") ");
}
}
if (IsSparse)
{
sbScript.Append("SPARSE ");
}
if (!so.NoFileStream)
{
if (IsFileStream)
{
sbScript.Append("FILESTREAM ");
}
}
sbScript.Append(IsNullable ? "NULL " : "NOT NULL ");
if (!IsReplicated)
{
//sbScript.Append("NOT FOR REPLICATION ");
}
if (IsRowGuidCol)
{
sbScript.Append("ROWGUIDCOL ");
}
if (IsUnique)
{
sbScript.Append("UNIQUE ");
}
}
return sbScript.ToString().Trim();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
My name is Robert Kanasz and I have been working with ASP.NET, WinForms and C# for several years.
MCSD - Web Applications
MCSE - Data Platform
MCPD - ASP.NET Developer 3.5
- Web Developer 4
MCITP - Database Administrator 2008
- Database Developer 2008
MCSA - SQL Server 2012
MCTS - .NET Framework 3.5, ASP.NET Applications
- SQL Server 2008, Database Development
- SQL Server 2008, Implementation and Maintenance
- .NET Framework 4, Data Access
- .NET Framework 4, Service Communication Applications
- .NET Framework 4, Web Applications
MS - Programming in HTML5 with JavaScript and CSS3 Specialist
Open source projects:
DBScripter - Library for scripting SQL Server database objects
Please, do not forget vote