ruder

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
#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);
        }
posted on 2009-04-29 17:43  徐境  阅读(156)  评论(0)    收藏  举报