zoukankan      html  css  js  c++  java
  • linq .dbml转化成sql脚本

    public String ConvertDBMLToSqlScript(System.Data.Linq.DataContext DBContext) 
    {  
          String DBContextNamespace = DBContext.GetType().Namespace;  
          StringBuilder sqlScriptCollection = new StringBuilder();  

          String[] DotNeedArr = new String[]   
         {   
           "Connection","Transaction","CommandTimeout",  
           "Log", "ObjectTrackingEnabled","DeferredLoadingEnabled",  
           "Mapping","LoadOptions","ChangeConflicts" 
         };  
         foreach (PropertyInfo p in DBContext.GetType().GetProperties())  
         {  
             if (DotNeedArr.Contains(p.Name)) continue;  
             string sqlScript = "create table "+p.Name+" (";     
             Type type = Type.GetType(DBContextNamespace+"." + p.Name);       
             foreach (System.Reflection.PropertyInfo mInfo in type.GetProperties())  
             {  
                  foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))  
                  {  
                       if (attr.GetType() == typeof(ColumnAttribute))  
                       {  
                           ColumnAttribute ColumnAttr = attr as ColumnAttribute;                
                           sqlScript += "[" + mInfo.Name + "] ";  
                           if (ColumnAttr.DbType.Contains("NULL"))

                               sqlScript+=ColumnAttr.DbType;             

                           else

                               sqlScript+=ColumnAttr.DbType+" null ";            
                           if (ColumnAttr.IsPrimaryKey)

                           {

                                sqlScript += " primary key ";

                           }                      
                         sqlScript += ",";  
                      }  
                 }  
           }                  
           sqlScript = sqlScript.Substring(0, sqlScript.Length - 1);  
           sqlScript += ")";  
           sqlScriptCollection.AppendLine(sqlScript);  
        }  
        return sqlScriptCollection.ToString();  
    }

    DataClasses1DataContext DBContext = new DataClasses1DataContext();  

    String resultSql = ConvertDBMLToSqlScript(DBContext);

  • 相关阅读:
    lnmp下如何建立svn版本库
    解决更新本地svn版本库,提示:工作副本已锁定 问题
    请不要在意
    ecshop在lbi库文件中添加广告位的方法(转载,试过了确实可以添加成功)
    Jquery AjaxUpload实现文件上传
    js提交表单错误:document.form.submit() is not a function
    kindeditor的使用方法
    phpcmsv9整合ucenter经验分享
    替换字符串sql语句
    初生牛犊之spring(二)
  • 原文地址:https://www.cnblogs.com/webwang/p/4231999.html
Copyright © 2011-2022 走看看