我现在开发一个程序,需要将一个ArrayList存储到字段类型为binary/image的数据库字段中。

在存储(序列化)的时候,将ArrayList转成byte[] 序列化到内存流中,然后再存到数据库中。

取出(反序列化)的时候,将字段类型为binary/image的值取出,转换成byte[],写到内存流中,再反序列化到arrayList中,返回。

本来程序的执行很顺利,可是过一会儿就会出现异常,并且频率非常高,有时候一小时不会异常,有时候一分钟就异常了,非常郁闷。

异常发生在取出的时候(反序列化),说是“在分析完成之前就遇到流结尾”

异常信息是:
---------------------------------------------------------------------------------------------------------------------
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------

在分析完成之前就遇到流结尾。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及

代码中导致错误的出处的详细信息。
---------------------------------------------------------------------------------------------------------------------

下边贴出我的程序,如果有谁知道是为什么请解释一下。

附一:取出(反序列化)方法,错误就在这里:

 1public static ArrayList GetChatList()
 2        {
 3            ArrayList myArrayList = new ArrayList();
 4
 5            DataTable myDataTable = new DataTable();
 6
 7            myDataTable = GetChatConfigDetial();            
 8
 9            byte[] b;
10
11            if(Convert.ToString(myDataTable.Rows[0]["Memberlist"]) != "")
12            {
13                b = (byte[])myDataTable.Rows[0]["Memberlist"];
14                MemoryStream ms    = new MemoryStream(); 
15                try
16                {                    
17                    BinaryFormatter    binaryFormatter    = new BinaryFormatter(); 
18     
19                    // 将 byte 数组到内存流 
20                    // 
21                    ms.Write(b, 0, b.Length); 
22                    
23                    //ms.Flush();
24                    // 将内存流的位置到最开始位置 
25                    //                     
26                    //ms.Position    = 0;    
27                    ms.Seek(0,SeekOrigin.Begin);
28                    // 反序列化成对象,创建出与原对象完全相同的副本 
29                    // 
30                    myArrayList = (ArrayList)binaryFormatter.Deserialize(ms); 
31                }

32                catch(SerializationException e)
33                {
34                                    throw e;
35
36                }

37                finally
38                {                    
39                    ms.Close(); 
40                }
    
41
42            }

43            myDataTable.Dispose();
44            return myArrayList;
45        }


附二:存储(序列化)方法:
public static void UpdateChatList(ArrayList myArrayList)
        
{    
            
// 序列化对象 
            BinaryFormatter    binaryFormatter    = new BinaryFormatter(); 
            MemoryStream ms 
= new MemoryStream();
            
byte[] b; 
            binaryFormatter.Serialize(ms, myArrayList); 
            
// 设置内存流的起始位置 
            
// 
            
//ms.Position    = 0; 
            ms.Seek(0,SeekOrigin.Begin); 
            
// 读入到 byte 数组 
            
// 
            b =    new    Byte[ms.Length]; 
            ms.Read(b, 
0, b.Length); 
            ms.Close(); 


            SqlConnection myConnection 
= new SqlConnection(F1Security.GetConnectionString("ConnectionStringChat"));
            SqlCommand myCommand 
= new SqlCommand("", myConnection);

            
try
            
{
                myConnection.Open();
                myCommand.CommandType 
= CommandType.StoredProcedure;
                myCommand.CommandText 
= "sp_F1_ChatUpdateChatList";


                SqlParameter paramMembers 
= myCommand.Parameters.Add("@ChatList",  SqlDbType.Image,5000);
                paramMembers.Value 
= b;

                myCommand.ExecuteNonQuery();
            }

            
catch(Exception e)
            
{
                
throw e;
            }

            
finally
            
{
                myConnection.Close();
            }

        }


问了很多人,都说程序没问题,找不到原因。
posted on 2005-09-02 18:13  xmen  阅读(134)  评论(0)    收藏  举报