[已解决问题] 关于写SQL Server 聚集函数的问题
提问时间: 2008-07-21 22:18
悬赏分:20 浏览:185 次

用CLR写个聚集函数,代码如下.

执行结束都提示啥溢出的.

我检查了序列化代码 没有啥溢出的问题.请哪个牛人告诉我怎么回事.

 A first chance exception of type 'System.OverflowException' occurred in System.Data.dll

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections.Generic;

[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedAggregate
    (Format.UserDefined, 
//use custom serialization to serialize the intermediate result
    IsInvariantToNulls = true//optimizer property
    IsInvariantToDuplicates = false//optimizer property
    IsInvariantToOrder = false//optimizer property
    MaxByteSize = 8000//maximum size in bytes of persisted value
]
public struct AVG2 : IBinarySerialize 
{
    
public void Init()
    
{
        
// Put your code here
        re = new List<double>();
        count 
= 0;
    }


    
public void Accumulate(SqlDouble Value)
    
{
        
// Put your code here
        if (!Value.IsNull)
        
{
            re.Add(Value.Value);
            count
++;
        }

    }


    
public void Merge(AVG2 Group)
    
{
        
// Put your code here
        re = Group.re;
        count 
= Group.count;

    }


    
public SqlDouble Terminate()
    
{
        
double sum = 0;
        
foreach (double item in re)
        
{
            sum 
+= item;
        }

        
return sum / re.Count;
    }


    
// This is a place-holder member field

    
private int count;
    
private List<double> re;
    
IBinarySerialize Members
}

提问者:zeus2 - 初学一级
所有回答(2)
int count = r.ReadInt32();//?? for (int i = 0; i < count; i++) { double b = r.ReadDouble();//? re.Add(b); } 看出问题了吗?Double字长不一样的~~这样当然溢出了.
2个月前   回答者:沙加 - 老鸟四级
'System.OverflowException' 有点经验的同志,都知道这个是你读数据的时候,数据时出的问题!
2个月前   回答者:文杰 - 初学一级
评论
2个月前   zeus2 :
w.Write(count);
for (int i = 0; i < count; i++)
{
w.Write(re[i]);
}
序列化就是这样写的。。
   您需要登录以后才能回答!
 

我要提问

我的问题


快到期问题

> 问题排行榜

相关内容

相关链接