|
转眼间在博客园里待了快一年了,我还能记得去年的这个时候老师有个项目,在这个项目里需要把图片保存到数据库里,以前我做的就是把路径保存到数据库,后来为了实现这个功能我可是费了不少劲啊,问东问西的最后还是在优快云网友的帮助下完成了。今天晚上又有优快云网友提问有关图片保存到数据库的问题。下面我来汇总一下如何将图片保存到SqlServer、Oracle、Access数据库中。
首先,我们要明白图片是以二进制的形式保存在数据库中的,那么把图片保存到数据库中的步骤大体上有这几步 1.将图片转换为二进制数组(byte[]); 2.把转换后的二进制数组(byte[])作为参数传递给要执行的Command; 3.执行Command; 首先,如何把图片转换成byte[],如果你使用的是ASP.Net2.0,那么你可以使用FileUpLoad控件来实现 byte[] fileData = this.FileUpload1.FileBytes; 如果你用的是ASP.Net1.1或者你在创建WinForm那么你可以使用下面的方法来把图片转换为byte[] public byte[] getBytes(string filePath)![]() ![]() { System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open); byte[] imgData = new byte[fs.Length]; fs.Read(imgData, 0, (int)fs.Length); return imgData; }1.SqlServer数据库。SqlServer有Image字段类型,最大可以存储2G的数据。 byte[] fileData = this.FileUpload1.FileBytes;![]() string sql = "insert into t_img(img) values (@img)"; string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["fengdongDB"].ToString(); SqlConnection sqlConn = new SqlConnection(strconn); SqlCommand sqlComm = new SqlCommand(sql, sqlConn); sqlComm.Parameters.Add("@img", SqlDbType.Image);//添加参数 sqlComm.Parameters["@img"].Value = fileData;//为参数赋值![]() sqlConn.Open(); sqlComm.ExecuteNonQuery(); sqlConn.Close();2.Oracle数据库。在Oracle数据库中我们可以使用BLOB字段类型,最大可以存储4G的数据。 byte[] fileData = this.FileUpload1.FileBytes;![]() string sql = "insert into t_img(imgid,IMGDATA) values(100,:IMGDATA)"; string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringForOracle"].ToString(); OracleConnection oraConn = new OracleConnection(strconn); OracleCommand oraComm = new OracleCommand(sql, oraConn); oraComm.Parameters.Add(":IMGDATA", OracleType.Blob);//添加参数 oraComm.Parameters[":IMGDATA"].Value = fileData;//为参数赋值![]() oraConn.Open(); oraComm.ExecuteNonQuery(); oraConn.Close(); 注意:这里我需要说明一下,用Oracle的专用连接传递参数的时候你要小心一点,看看上面的SQL语句你就会知道,要在参数名前加个“:”否则就会出现下面的错误“OracleException: ORA-01036: 非法的变量名/编号”。这里需要我们注意一下。另外还有一个地方,当我引用System.Data.OracleClient命名空间的时候默认是没有的,必须添加对System.Data.OracleClient的引用,我记得在VS2003下如果安装了OracleClient是不用添加引用就可以引入的。这里也要留意一下。 byte[] fileData = this.FileUpload1.FileBytes;![]() string sql = "insert into t_img(IMGDATA) values(?)"; string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringForAccess"].ToString();![]() OleDbConnection oleConn = new OleDbConnection(strconn); OleDbCommand oleComm = new OleDbCommand(sql, oleConn); oleComm.Parameters.Add("imgdata", OleDbType.Binary); oleComm.Parameters["imgdata"].Value = fileData;![]() oleConn.Open(); oleComm.ExecuteNonQuery(); oleConn.Close();
private byte[] getImageDataFromOracle()![]() ![]() { string sql = "select IMGDATA from t_img where imgID=100"; string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringForOracle"].ToString(); OracleConnection oraConn = new OracleConnection(strconn); OracleCommand oraComm = new OracleCommand(sql, oraConn);![]() oraConn.Open(); byte[] fileData = (byte[])oraComm.ExecuteScalar(); oraConn.Close();![]() return fileData; }我们获取到了数据,那么把byte[]转换为图片的过程都是一样的。 private System.Drawing.Image convertByteToImg(byte[] imgData)![]() ![]() { System.IO.MemoryStream ms = new System.IO.MemoryStream(imgData); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); return img; }比如输出页面getImg.aspx的代码 protected void Page_Load(object sender, EventArgs e)![]() ![]() { string sql = "select IMGDATA from t_img where imgID=100"; string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringForOracle"].ToString(); OracleConnection oraConn = new OracleConnection(strconn); OracleCommand oraComm = new OracleCommand(sql, oraConn);![]() oraConn.Open(); byte[] fileData = (byte[])oraComm.ExecuteScalar(); oraConn.Close();![]() System.IO.MemoryStream ms = new System.IO.MemoryStream(fileData); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } |




System.IO.FileStream fs
}
1万+

被折叠的 条评论
为什么被折叠?



