c++ 写进数据库的图片 c# 读取不出来 (同一张图片) (有难度)
c++ 写进数据库的图片 c# 读取不出来
一 C++ 写进数据库的图片 C++ 可以读取
二 C# 写进数据库的图片 C#可以读取
但是c++ 写进数据库的图片 c# 读取不出来
c++ 写进数据库的图片
if(strCoverPic != "")
{
BYTE * pBuf;
//读取文件中的图像数据
CFile file;
if( file.Open(strCoverPic, CFile::modeRead) )
{
m_nFileLen = file.GetLength();
pBuf = new BYTE[m_nFileLen + 2];
if(pBuf)
{
BYTE *pBufEx;
pBufEx = pBuf;
VARIANT varBLOB;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
if(file.Read(pBuf,m_nFileLen) == m_nFileLen)
{
if(pBuf)
{
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = m_nFileLen;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound); ///创建SAFEARRAY对象
for (long i = 0; i < (long)m_nFileLen; i++)
SafeArrayPutElement (psa, &i, pBufEx++); ///将pBuf指向的二进制数据保存到SAFEARRAY对象psa中
varBLOB.vt = VT_ARRAY | VT_UI1;///将varBLOB的类型设置为BYTE类型的数组
varBLOB.parray = psa; ///为varBLOB变量赋值
m_pRecSet->GetFields()->GetItem("book_image")->AppendChunk(varBLOB); ///加入BLOB类型的数据
}
}
}
}
else
{
AfxMessageBox("封面图片文件打开错误,请确认输入正确文件名!");
return FALSE;
}
}
c# 读写文件
string ImageName=DateTime.Now.ToString("yyyyMMddhhmmssfff")+"."+exName;//图片名称设置为保存的时间
Byte[] FileByte = new Byte[FileLength]; //图象文件储存到数组
Stream ObjectStream = UpFile.InputStream;//建立数据流对像,获取一个 Stream 对象,该对象指向一个上载文件,以准备读取该文件的内容。
ObjectStream.Read(FileByte,0,FileLength); //读取图象文件数据
string StrSql="Insert Into t_b_libinfo(book_name,book_image) Values(@book_name,@book_image)";
SqlCommand Cmd=new SqlCommand(StrSql,conn);
Cmd.Parameters.Add("@book_image",SqlDbType.Binary,FileLength).Value=FileByte;
Cmd.Parameters.Add("@book_name",SqlDbType.VarChar,100).Value=ImageName;
conn.Open();
Cmd.ExecuteNonQuery();
conn.Close();
Response.Write("<script>alert('图片保存到数据库成功')</script>");
读取
string strlib_no = Request["lib_no"].ToString();
DJPSoft.Enterprise.Manager.LibManager Lib= new DJPSoft.Enterprise.Manager.LibManager();
DJPSoft.Enterprise.Interface.ILib_info ILib=Lib.GetLibinfo(strlib_no);
string str=ILib.book_name.ToString();
this.Response.BinaryWrite((byte[])ILib.book_image);