e股脑电脑教程网
  • 首 页
  • 操作系统
  • 应用软件
  • 下载工具
  • 影音视频
  • 办公软件
  • 媒体制作
  • 网站建设
  • 平面设计
  • 数据库
  • 程序开发
  • 视频教程
编辑推荐: | 文章搜索:
您现在的位置: e股脑 >> 数据库 >> Mssql教程 >> 使用 SQL Server 2005中的 CLR 集成 >> 教程正文
 
教程搜索
 
 
相关教程
  • 你知道MySQL10条鲜为人知的技巧吗?
  • 你应该知道的10个MySQL客户启动选项
  • 用特殊的MySQL运算符获得更多数据比较
  • MySQL数据库中与 NULL值有关的几个问题
  • 教你快速了解公共MySQL的数据库服务器
  • MySQL 5.0在windows上的安装详细介绍
  • MySQL查询的性能优化
  • Linux操作系统中如何安装MySQL数据库
  • 23道安全门加铸MySQL数据库
  • MySQL 5.0 数据库新特性的存储过程
  • 从Windows命令行启动MySQL
  • 连接MySQL数据库失败频繁的原因
  • 加速动态网站 MySQL索引分析和优化
  • 轻松掌握如何保护MySQL中的重要数据
  • 快速掌握MySQL数据库中Show命令的用法
  • MySQL数据库的安全配置实用技巧
 
 

图文教程


  • Windows抢了谁的饭碗 非主流操作系统To

  • 地球还是火星 平常心看“非主流”

  • 综合运用Office 2007批量制作奖状

  • 没有系统盘如何才能修复受损系统?

  • 巧妙运用Excel中边界的附加功能!
 
 
赞 助 商
 
 
使用 SQL Server 2005中的 CLR 集成
  • 来源:e股脑
  • 点击次数:
  • 更新时间:2007-8-9

Int

非空

产品 ID

WeekDate

smalldatetime

非空

需求预测周

DemandQty

int

非空

特定产品和特定周的需求预测

给定一组产品,它们的库存和启动成本以及未来需求预测,我们创建了接受如下输入参数的存储过程:1)制订生产进度表的日期,2)按进度表生产所需要的周数。


存储过程返回带有下表中的架构的行集。

表 3:存储过程架构 列名 类型 说明

Product

nvarchar(256)

产品名称

Period

datetime

进度周

Quantity

int

在指定周内制造的产品的数量

将 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(;

上一页  1 2 3 4 5 6 7 8 下一页
  • 上一篇教程: 在 SQL Server 2005 Beta 2 中编辑 Transact-SQL 代码
  • 下一篇教程: SQL Server 2005 数据转换服务的常见设计问题
  •  

    关于本站 | 广告联系 | 版权声明 | 使用帮助

    Copyright © 2004-2008 www.egunao.com All rights reserved.