且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将图像文件作为二进制数据存储到mysql数据库中

更新时间:2023-10-10 21:27:22

^ ]

http://***.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://***.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);