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

  • 相关阅读:
    C#根据当前日期获取星期和阴历日期
    C#常用实例
    使用Open Live Writer在博客园发表博文随笔
    Excel公式中双引号和单引号输入和显示以及函数的选择确认
    outlook新邮件到达提醒设置以及outlook最小化到托盘设置
    Windows任务计划
    酷狗音乐盒缓存文件夹KuGouCache的设置方法
    Android SDK生成时,自定义文件名称,而非系统第一分配的app-release.apk
    android button text属性中英文大小写问题
    Diskpart使用说明
  • 原文地址:https://www.cnblogs.com/webwang/p/4231999.html
Copyright © 2011-2022 走看看