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

  • 相关阅读:
    【tensorflow】神经网络的一些基本概念(前向传播、反向传播、损失函数、梯度下降法、学习率)和设计过程
    【opencv+python】图像的基本操作:缩放、剪切、位移、旋转、仿射变换
    【tensorflow】利用神经网络绘制股票价格拟合曲线
    Tuple
    2020-08-17T15:35:54.525+08:00
    FTP协议协议
    centos7常规系统指标监控shell脚本
    AWK传入shell变量举例
    洛谷-P5143 攀爬者
    洛谷-P1104 生日
  • 原文地址:https://www.cnblogs.com/webwang/p/4231999.html
Copyright © 2011-2022 走看看