如何将图像文件作为二进制数据存储到mysql数据库中 [英] how to store image file as a binary data into the mysql database
本文介绍了如何将图像文件作为二进制数据存储到mysql数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友,
在我的项目中,我需要将图像文件作为二进制数据存储到数据库中.我在mysql数据库中创建了一个列",并声明为"varbinary".
我已经尝试过以下代码.
Hi Friends,
In my project i need to store the image file as a binary data into the database.I have create one "column" in mysql database and declared as "varbinary".
And i have tried this following code.
HttpPostedFile file = FileUpload1.PostedFile;
Stream stream = file.InputStream;
BinaryReader br = new BinaryReader(stream);
byte[] byt = br.ReadBytes((int)stream.Length);
cmd = new MySqlCommand("Insert into db_image (Image) values ('" + byt + "')", con);
cmd.ExecuteNonQuery();
然后我看到我的数据库数据存储为"System.byte".如何检索.
是正确的方法吗?请帮帮我.
Then i seen my database the data is storing as "System.byte" . How to Retrieve it.
Is it Correct method .Please help me.
推荐答案
^ ]
http://stackoverflow.com/questions/11030952/unable-to-retrieve-or-store-image-in-database-sql-server-2005 [ http://techiecocktail.blogspot.in/2009/05/store- and-retrieve-image-from-sql.html [ ^ ]
http://www.sqlhub.com/2009/03/image-store-in-sql-server-2005-database.html[^]
http://stackoverflow.com/questions/11030952/unable-to-retrieve-or-store-image-in-database-sql-server-2005[^]
http://techiecocktail.blogspot.in/2009/05/store-and-retrieve-image-from-sql.html[^]
// Read the file and convert it to Byte Array
string filePath = Server.MapPath("APP_DATA/TestDoc.docx");
string filename = Path.GetFileName(filePath);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
//insert the file into database
string strQuery = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = "application/vnd.ms-word";
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
InsertUpdateData(cmd);
这篇关于如何将图像文件作为二进制数据存储到mysql数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文