#region 确定栏位存在
text.AppendLine("if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[" + CurrentType.Name + @"]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)");
foreach (PropertyInfo info in Attributes)
{
string dbType = GetSqlType(info);
object[] customerAttributes = info.GetCustomAttributes(true);
if (customerAttributes != null)
{
foreach (object obj in customerAttributes)
{
if (obj is TextAttribute && info.PropertyType.Name == "String")
{
dbType = ((TextAttribute)obj).SqlText;
}
}
}
text.AppendLine("if not exists (SELECT * FROM dbo.syscolumns WHERE (id = OBJECT_ID(N'[dbo].[" + CurrentType.Name + "]')) AND (name = '" + info.Name + "') )");
text.AppendLine("ALTER TABLE [dbo].[" + CurrentType.Name + "] Add " + info.Name + " " + dbType + " null ");
//文本
if (dbType.ToLower().IndexOf("ntext") != -1)
{
text.AppendLine("if not exists (SELECT * FROM dbo.syscolumns WHERE (id = OBJECT_ID(N'[dbo].[" + CurrentType.Name + "]')) AND (name = '" + info.Name + "') and xtype in( select xtype from dbo.sysTypes where name='ntext' ))");
text.AppendLine("ALTER TABLE [dbo].[" + CurrentType.Name + "] alter column " + info.Name + " ntext null ");
}
//非文本
else if (dbType.ToLower().IndexOf("varchar") == -1)
{
text.AppendLine("if not exists (SELECT * FROM dbo.syscolumns WHERE (id = OBJECT_ID(N'[dbo].[" + CurrentType.Name + "]')) AND (name = '" + info.Name + "') and xtype in( select xtype from dbo.sysTypes where name='" + dbType + "' ))");
text.AppendLine("ALTER TABLE [dbo].[" + CurrentType.Name + "] alter column " + info.Name + " " + dbType + " null ");
}
}
#endregion
SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, text.ToString(), null);
}
text.AppendLine("if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[" + CurrentType.Name + @"]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)");
foreach (PropertyInfo info in Attributes)
{
string dbType = GetSqlType(info);
object[] customerAttributes = info.GetCustomAttributes(true);
if (customerAttributes != null)
{
foreach (object obj in customerAttributes)
{
if (obj is TextAttribute && info.PropertyType.Name == "String")
{
dbType = ((TextAttribute)obj).SqlText;
}
}
}
text.AppendLine("if not exists (SELECT * FROM dbo.syscolumns WHERE (id = OBJECT_ID(N'[dbo].[" + CurrentType.Name + "]')) AND (name = '" + info.Name + "') )");
text.AppendLine("ALTER TABLE [dbo].[" + CurrentType.Name + "] Add " + info.Name + " " + dbType + " null ");
//文本
if (dbType.ToLower().IndexOf("ntext") != -1)
{
text.AppendLine("if not exists (SELECT * FROM dbo.syscolumns WHERE (id = OBJECT_ID(N'[dbo].[" + CurrentType.Name + "]')) AND (name = '" + info.Name + "') and xtype in( select xtype from dbo.sysTypes where name='ntext' ))");
text.AppendLine("ALTER TABLE [dbo].[" + CurrentType.Name + "] alter column " + info.Name + " ntext null ");
}
//非文本
else if (dbType.ToLower().IndexOf("varchar") == -1)
{
text.AppendLine("if not exists (SELECT * FROM dbo.syscolumns WHERE (id = OBJECT_ID(N'[dbo].[" + CurrentType.Name + "]')) AND (name = '" + info.Name + "') and xtype in( select xtype from dbo.sysTypes where name='" + dbType + "' ))");
text.AppendLine("ALTER TABLE [dbo].[" + CurrentType.Name + "] alter column " + info.Name + " " + dbType + " null ");
}
}
#endregion
SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, text.ToString(), null);
}

浙公网安备 33010602011771号