|
using System.Text;
namespace ObjectHelper.DBObjectType
{
public class UserDefinedDataType:BaseDbObject
{
public string Schema { get; set; }
public string SystemType { get; set; }
public int Length { get; set; }
public int Precision { get; set; }
public int Scale { get; set; }
public int MaxLength { get; set; }
public bool IsNullable { get; set; }
public string Script()
{
var sbScript = new StringBuilder();
sbScript.Append("CREATE TYPE ["+Schema+"].["+Name+"] FROM ");
switch (SystemType.ToUpper())
{
case "IMAGE":
sbScript.Append("[" + SystemType + "] ");
break;
case "TEXT":
sbScript.Append("[" + SystemType + "] ");
break;
case "UNIQUEIDENTIFIER":
sbScript.Append("[" + SystemType + "] ");
break;
case "DATE":
sbScript.Append("[" + SystemType + "] ");
break;
case "TIME":
if (Precision == -1)
{
sbScript.Append("[" + SystemType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + SystemType + "]" + "(" + Scale + ") ");
}
break;
case "DATETIME2":
if (Precision == -1)
{
sbScript.Append("[" + SystemType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + SystemType + "]" + "(" + Scale + ") ");
}
break;
case "DATETIMEOFFSET":
if (Precision == -1)
{
sbScript.Append("[" + SystemType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + SystemType + "]" + "(" + Scale + ") ");
}
break;
case "TINYINT":
sbScript.Append("[" + SystemType + "] ");
break;
case "SMALLINT":
sbScript.Append("[" + SystemType + "] ");
break;
case "REAL":
sbScript.Append("[" + SystemType + "] ");
break;
case "MONEY":
sbScript.Append("[" + SystemType + "] ");
break;
case "DATETIME":
sbScript.Append("[" + SystemType + "] ");
break;
case "SMALLDATETIME":
sbScript.Append("[" + SystemType + "] ");
break;
case "INT":
sbScript.Append("[" + SystemType + "] ");
break;
case "FLOAT":
sbScript.Append("[" + SystemType + "] ");
break;
case "SQL_VARIANT":
sbScript.Append("[" + SystemType + "] ");
break;
case "NTEXT":
sbScript.Append("[" + SystemType + "] ");
break;
case "BIT":
sbScript.Append("[" + SystemType + "] ");
break;
case "DECIMAL":
sbScript.Append("[" + SystemType + "]" + " (" + Precision + "," + Scale + ") ");
break;
case "NUMERIC":
sbScript.Append("[" + SystemType + "]" + " (" + Precision + "," + Scale + ") ");
break;
case "SMALLMONEY":
sbScript.Append("[" + SystemType + "] ");
break;
case "BIGINT":
sbScript.Append("[" + SystemType + "] ");
break;
case "HIERARCHYID":
sbScript.Append("[" + SystemType + "] ");
break;
case "GEOMETRY":
sbScript.Append("[" + SystemType + "] ");
break;
case "GEOGRAPHY":
sbScript.Append("[" + SystemType + "] ");
break;
case "VARBINARY":
if (Precision == -1)
{
sbScript.Append("[" + SystemType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + SystemType + "]" + "(" + Length + ") ");
}
break;
case "VARCHAR":
if (Precision == -1)
{
sbScript.Append("[" + SystemType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + SystemType + "]" + "(" + Length + ") ");
}
break;
case "BINARY":
if (Precision == -1)
{
sbScript.Append("[" + SystemType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + SystemType + "]" + "(" + Length + ") ");
}
break;
case "CHAR":
if (Precision == -1)
{
sbScript.Append("[" + SystemType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + SystemType + "]" + "(" + Length + ") ");
}
break;
case "TIMESTAMP":
sbScript.Append("[" + SystemType + "]" + " ");
break;
case "NVARCHAR":
if (Precision == -1)
{
sbScript.Append("[" + SystemType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + SystemType + "]" + "(" + Length + ") ");
}
break;
case "NCHAR":
if (Precision == -1)
{
sbScript.Append("[" + SystemType + "]" + "(max) ");
}
else
{
sbScript.Append("[" + SystemType + "]" + "(" + Length + ") ");
}
break;
case "SYSNAME":
sbScript.Append("[" + SystemType + "] ");
break;
default:
sbScript.Append("[" + SystemType + "] ");
break;
}
sbScript.Append(IsNullable ? " NULL;" : " NOT NULL;");
return sbScript.ToString();
}
}
}
|
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