
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(textBox1.Text, FileMode.Open)
BinaryReader br = new BinaryReader(fs)
Byte[] byData = br.ReadBytes((int)fs.Length)
fs.Close()
string conn = "server=.database=testDBUid=saPwd=sa "
SqlConnection myconn = new SqlConnection(conn)
myconn.Open()
string str = "insert into pro_table (pro_name,pro_file) values('测试文件',@file)"
SqlCommand mycomm = new SqlCommand(str, myconn)
mycomm.Parameters.Add("@file", SqlDbType.Binary, byData.Length)
mycomm.Parameters["@file"].Value = byData
mycomm.ExecuteNonQuery()
myconn.Close()
}
//从数据库中把二进制流读出写入还原成文件
private void button4_Click(object sender, EventArgs e)
{
string conn = "server=.database=testDBUid=saPwd=sa "
string str = "select pro_file from pro_table where pro_name='测试文件' "
SqlConnection myconn = new SqlConnection(conn)
SqlDataAdapter sda = new SqlDataAdapter(str, conn)
DataSet myds = new DataSet()
myconn.Open()
sda.Fill(myds)
myconn.Close()
Byte[] Files = (Byte[])myds.Tables[0].Rows[0]["pro_file"]
BinaryWriter bw = new BinaryWriter(File.Open("D:\\2.rdlc",FileMode.OpenOrCreate))
bw.Write(Files)
bw.Close()
}
图片就是文件嘛,把文件数据全部读入到内存然后插入到sql数据库
中就可以了,但不建议这样做,因为图片数据比较大,存入数据库会很慢,检索也会很慢,建议只存入图片的路径,比如你要存入的图片为c:\\test.bmp,那么你存入数据的信息就为c:\\test.bmp,当有地方要使用该图片时,只需取出c:\\test.bmp这个路径就可以 *** 作该图片了。
比较复杂,看我的笔记。根本方法是定义图片VARIANT变量,其类型为VT_ARRAY|VT_UI1。此时VARIANT的值为SAFEARRAY.将图片载入SAFEARRAY中。便可以存入图片了。可以存储任何格式 的图片
void CDataBase16Dlg::OnBnClickedAddNew()
{
CString filepath
CFileDialog filedialog(TRUE)
if(filedialog.DoModal()==IDOK)
{
filepath=filedialog.GetPathName()
}
CFile file
file.Open(filepath,CFile::modeRead)
SAFEARRAY *psa //SAFEARRAY是variant变量的参数之一。
SAFEARRAYBOUND bound[1]//SAFEARRAYBOUND为SAFEARRAY结构成员之一。
bound[0].lLbound=0 //代表数组起始索引,一般为 0
bound[0].cElements=file.GetLength() //代表元素个数,即图片的大小。以字节为单位
psa=SafeArrayCreate(VT_UI1,1,bound) //创建SAFEARRAY,中间参数代表创建的是一个一维数组
BYTE *filebuffer=new BYTE[file.GetLength()+1]
file.Read(filebuffer,file.GetLength()) //将图片文件读入内存。
for(long index=0index<file.GetLength()index++)
SafeArrayPutElement(psa,&index,&filebuffer[index]) //将图片的每一字节放入SAFEAWWAY中。
VARIANT vt
vt.vt = VT_ARRAY|VT_UI1
vt.parray=psa
m_recordset->GetFields()->GetItem(_variant_t("肖像"))->AppendChunk(vt)//添加图片。
m_recordset->Update()
}
至于读取,你没问我也就不答了,比较长。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)