• ----:)欢迎访问源码网(:----
    • 首页
    • 博客
    • 学院
    • 下载
    • 论坛
    • 影视
    • 发布源码
    • RSS
    • ITPig
    • 笑话网
    • 百家姓
    • 繁體中文

源码网 - 中国第一源码门户
选择镜像:网通镜像 - 电信主站
  • 首 页
  • 新闻动态
  • 网站运营
  • 网页制作
  • WEB开发
  • 编程开发
  • 图像媒体
  • 操作系统
  • 数据库
  • 服务器
热门搜索 优化 SEO 故事 cms IIS7 MySQL 个人 AdSense 主题推广 | 文章搜索: 高级搜索
会员登录/控制面版您的位置: 学院首页 >> 数据库 >> MySQL >> 详细内容
 

推荐文章

  • 《MySQL管理员指南》之一----MySQL安全性指南
  • 实例讲解MYSQL数据库的查询优化技术
  • MySQL查询优化技术讲座
 
 

热点文章

  • 支持中文的MySQL 5.1+ 全文检索分词插件
  • MySQL数据导入导出方法与工具mysqlimport
  • 《MySQL管理员指南》之一----MySQL安全性指南
  • MySql管理的一点心得&MYSQL命令行模式
  • 修改MySQL的默认密码
  • 使用 SQL Server 2005中的 CLR 集成
  • 使用MySQL全文检索
  • Microsoft SQL Server 2005 中的 XML 支持
  • 简介Mysql中的临时表使用方法
  • MySQL查询优化系列讲座之查询优化器
  • 使用Excel分析MySQL数据
  • MySQL的常见错误的解决方法(英文)
 
 

相关文章

  • “农村包围城市”SQL Server 2005“绸缪”企业级市场
  • 修改SQL Server 2005执行环境
  • SQL Server 2005中处理表分区问题
  • SQL Server 2005中Tempdb变化分析
  • SQL Server 2005的XML支持机制和安全机制
  • SQL SERVER 2005数据库镜像
  • SQL Server 2005性能测试之CPU篇
  • 安装SQL Server 2005实例环境图解
  • SQL Server 2005与DB2 8.2之对比
  • 了解SQL Server 2005五个有用的动态管理对象
  • SQL Server 2005:你应该知道的13件事情
  • 用SQL Server 2005的三个理由
 
 

百度搜索

 
 

使用 SQL Server 2005中的 CLR 集成

  • 阅览次数:
  • 文章来源: cp整理
  • 原文作者: 不详
  • 整理日期: 2007-04-03
  • 发表评论
  • 字体大小:
  • 小
  • 中
  • 大

将 C# 版本的代码复制到下面的代码中,以说明这种可以从 CLR 集成中大大获益的情况:

 

using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlServer;
using System.Data.SqlTypes;

public class ProductionSchedule
{
//4-year limit on scheduling
   public const int MAXPRODUCTS = 101;
   public const int MAXWEEKS = 210;
   public const int MAXNAME = 256;

   public ProductionSchedule()
   {
         
   }

   public static int Schedule(SqlDateTime startDate, int numWeeks)
   {
      SqlDateTime[] week = new SqlDateTime[MAXWEEKS];
      int[] quantity;
      int[][] Cij;
      int[] Fk;
      int[] minK = new int[MAXWEEKS];
      int product_id, current_product, product_count = 0;
      int startPeriod;

      // We'll use arrays to keep state about products and forecasts 
in memory. This is only viable given that we know we have a small number 
of products and weeks. 
      // For larger data sets, we would have to consider cursors or 
temporary tables.

      // stored as CLR types since we know they can't be null
      int[] h = new int[MAXPRODUCTS];
int[] K = new int[MAXPRODUCTS];

// stored as nullable SqlChars since the table schema allows for null names
      SqlChars[] productNames = new SqlChars[MAXPRODUCTS];
      bool moreProducts = true;
      int optimal_j;
      int period;
      int sum;
      SqlPipe pipe = SqlContext.GetPipe();
      SqlDataRecord record;
      object[] values = new object[3];
      SqlMetaData[] metadata = new SqlMetaData[3];

      //Initialize algorithm arrays
      Cij = new int[MAXWEEKS][];
      for( int l=0;l<MAXWEEKS;l++)
         Cij[l] = new int[MAXWEEKS];
      Fk = new int[MAXWEEKS];

      //Look up K and h for all products
      SqlCommand cmd = SqlContext.GetCommand();
      cmd.CommandText = @"SELECT pname, InventoryCost, StartupCost from dbo.t_Products ORDER BY PID";
      SqlDataReader reader = cmd.ExecuteReader();
      while(reader.Read())
      {
         productNames[product_count] = reader.GetSqlChars(0); //product name
         h[product_count] = reader.GetInt32(1); //holding cost 
         K[product_count] = reader.GetInt32(2); //startup cost
         product_count++;

         
         // if we exceeded number of expected products then bail out with an exception
         if (product_count >= MAXPRODUCTS)
         {
            throw new Exception("Too many products");
         }
      }
      reader.Close();
      product_count = 0;

      //Get the list of product ids;
      cmd = SqlContext.GetCommand();
      cmd.CommandText = @"select PID, weekdate, DemandQty from dbo.t_SalesForecast ORDER BY PID, WeekDate";
      reader = cmd.ExecuteReader();
      moreProducts=reader.Read();
         
      //Set up the record for returning results
      metadata[0] = new SqlMetaData( "Product", 
SqlDbType.NVarChar,MAXNAME );
      metadata[1] = new SqlMetaData( "Period", SqlDbType.DateTime );
      metadata[2] = new SqlMetaData( "Quantity", SqlDbType.Int );
      record = new SqlDataRecord( metadata );


      while( moreProducts )
      {
         product_id = current_product = reader.GetInt32(0);
         int index = 1;
         quantity = new int[MAXWEEKS];
         while( current_product == product_id )
         {
            week[index] = reader.GetSqlDateTime(1);
            quantity[index] = reader.GetInt32(2);
            index++;

            moreProducts = reader.Read();
            if( !moreProducts )
               break;
            current_product = reader.GetInt32(0);
         }

         //Determine the ordinal start week
         startPeriod = 1;

         //For each product ID calculate Cij
         for( int i = startPeriod; i < (startPeriod + numWeeks); i++ )
         {
            for( int j = i+1; j <= (startPeriod + numWeeks+1); j++ )
            {
               Cij[i][j] = GetCij(quantity,i,j,K [product_count],h[product_count]);
            }
         }

         //Calculate Fk
         for( int k = startPeriod + numWeeks + 1; k >= startPeriod; k--)
         {
            minK[k] = GetFk_SO(k,startPeriod + numWeeks,Cij,Fk);
         }

         //Send the results
         record.SetSqlChars(0,productNames[product_count]);
         pipe.SendResultsStart(record,false);

         for( int k = startPeriod; k < startPeriod + numWeeks; )
         {
            period = k;
            optimal_j = minK[k];
            sum = 0;
            while( k < optimal_j )
            {
               sum = sum + quantity[k++];
            }
            values[1] = week[period];
            record.SetValue(1,values[1]);
            values[2] = sum;
            record.SetValue(2,values[2]);
            pipe.SendResultsRow(record);
         }
         pipe.SendResultsEnd();
         product_count++;
      }
      reader.Close();
      return 0;
   }

   private static int GetCij(int[] quantities, int i, int j, int K, int h)
   {
      if( j == i+1 )
         return K;
      else
         return (j-1-i) * h * quantities[j-1] + GetCij(quantities, i, j-1,K,h);

   }
   private static int GetFk_SO(int k,int n,int[][] Cij, int[] Fk)
   {
      int j,min;
      j = k+1;
      min = j;

      if ( k == n+1 )
      {
         Fk[k] = 0;
         return j;
      }

      Fk[k] = Cij[k][j] + Fk[j];
      for(; k <= n ;k++)
      {
         j = k + 1;
         while( j <= n+1 )
         {
            if( Cij[k][j] + Fk[j] < Fk[k] )
            {
               min = j;
               Fk[k] = Cij[k][j] + Fk[j];
            }

            j++;
         }
      }
      return min;
   }

}


[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]

上一篇:PHP使用zlib扩展实现页面GZIP压缩输出
下一篇:构建支持Master/Slave读写分离的数据库操作类
  • 网友评论:
  • 查看所有评论
  • 我要发表评论
您的网名:
留言主题:
你要发表的内容:

 

关于本站 | 广告联系 | 版权声明 | 网站地图 | 发布软件 | 帮助中心 | 源码论坛

Copyright © 2005-2007 CodePub.Com  程序支持:木翼  滇ICP备05005971号