//创建一个新文件
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();以上代码先接收图片,然后保存在本地,再从本地将图片保存至数据库。有没有更好的办法,直接将图片保存到数据库,不经过本地保存。还请高手赐教。图片数据库
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();以上代码先接收图片,然后保存在本地,再从本地将图片保存至数据库。有没有更好的办法,直接将图片保存到数据库,不经过本地保存。还请高手赐教。图片数据库
如果byte不是一次接收完毕,那你可以建个List<byte[]>来接收,接收完毕后,将List<byte[]>组合成一个最终完整的byte[]
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);这步出错。
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中
}
List<byte> list = new List<byte>();
list.AddRange(data);
if(list.Count>0)
{
byte[] imagebytes = list.ToArray();
}通过linq直接组成byte[]就行了额