//创建一个新文件
string fileFullName = txtFileSaveDir.Text + "\\" + SendFileName;

FileStream MyFileStream = new FileStream(fileFullName, FileMode.Create, FileAccess.Write, FileShare.Read);
             
                //已发送包的个数
int SendedCount = 0; while (true)
{
byte[] data = TransferFiles.ReceiveVarData(client);
if (data.Length == 0)
{
break;
}
else
{
SendedCount++;
//将接收到的数据包写入到文件流对象
MyFileStream.Write(data, 0, data.Length);
 }
    }
                
//关闭文件流
MyFileStream.Close();
//关闭套接字
client.Close();                FileStream fs = new FileStream(fileFullName, FileMode.Open);
                byte[] imagebytes = new byte[fs.Length];
                BinaryReader br = new BinaryReader(fs);
                imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));                SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=Northwind;Integrated Security=True");
                con.Open();
                SqlCommand com = new SqlCommand("insert into db_05 values(@ImageList)",con);
                com.Parameters.Add("ImageList", SqlDbType.Image).Value = imagebytes;
                com.ExecuteNonQuery();
                con.Close();以上代码先接收图片,然后保存在本地,再从本地将图片保存至数据库。有没有更好的办法,直接将图片保存到数据库,不经过本地保存。还请高手赐教。图片数据库

解决方案 »

  1.   

    什么数据库? oracle:blob sqlserver:Image
      

  2.   

    http://www.cnblogs.com/tuyile006/archive/2007/01/08/614718.html
      

  3.   

    灰常奇怪的问题你都已经是byte[]接收了。。存到数据库也只能是byte[]为啥还要做一次保存操作呢???
    如果byte不是一次接收完毕,那你可以建个List<byte[]>来接收,接收完毕后,将List<byte[]>组合成一个最终完整的byte[]
      

  4.   

    是我没说清楚,定义每个包大小50K,所以才有sendedcount++,先传送完所有的数据包,然后写入filestream,我原本是想直接从文件流保存到数据库。单步调试时,MyFileStream中有数据。代码如下:
                    byte[] imagebytes = new byte[MyFileStream.Length];
                    BinaryReader br = new BinaryReader(MyFileStream);
                    imagebytes = br.ReadBytes(Convert.ToInt32(MyFileStream.Length));                SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=Northwind;Integrated Security=True");
                    con.Open();
                    SqlCommand com = new SqlCommand("insert into db_05 values(@ImageList)",con);
                    com.Parameters.Add("ImageList", SqlDbType.Image).Value = imagebytes;
                    com.ExecuteNonQuery();
                    con.Close(); 
    //关闭文件流
    MyFileStream.Close();
    //关闭套接字
    client.Close();
    但是在, BinaryReader br = new BinaryReader(MyFileStream);这步出错。
      

  5.   

    数据库中字段设置为image格式,可以存储的
      

  6.   

    long  totalLength=0;
    List<byte[]> list = new List<byte[]>();
    while (true)
                    {
                        byte[] data = TransferFiles.ReceiveVarData(client);
    totalLength +=data.Length ;
                        if (data.Length == 0)
                        {
                            break;
                        }
                        else
                        {
    list.Add(data);
                        }
                    }
    if(totalLength>0)
    {
     byte[] imagebytes = new byte[totalLength];
    //这里将list中的数据存放到imagebytes中
    }
      

  7.   

    上面2了。
    List<byte> list = new List<byte>();
    list.AddRange(data);
    if(list.Count>0)
    {
        byte[] imagebytes = list.ToArray();
    }通过linq直接组成byte[]就行了额