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

  • 相关阅读:
    mqtt 服务器与客户端通讯
    MQTT--单片机实现即时通信
    source insight 如何建工程--以及快捷方式查找调用函数方法
    在联网时,两台linux服务器传输文件方法
    字符串截取函数-c语言
    strcpy(),strcat()的用法
    如何将红色区域数据调用解密函数直接打印到输出控制台(例如:crt控制台)
    如何把apdu[decode_len]打印出来
    SecureCRT中文乱码解决已设置UTF-8
    RobotFrameWork(五)控制流之if语句——Run Keyword If
  • 原文地址:https://www.cnblogs.com/webwang/p/4231999.html
Copyright © 2011-2022 走看看