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);

  • 相关阅读:
    Android 仿iPhone ListView拖动排序 按钮联动删除显示隐藏
    START WITH CONNECT BY PRIOR 链表查询
    pdf转图片
    HttpClient支持使用代理服务器以及身份认证
    easyui 1.3.3 中combotree post传参问题
    quartz Cron表达式
    lucene分词多种方法
    secureCRT命令大全
    tbschedule
    mysql 查询表
  • 原文地址:https://www.cnblogs.com/webwang/p/4231999.html
Copyright © 2011-2022 走看看