首先,我们要明白图片是以二进制的形式保存在数据库中的,那么把图片保存到数据库中的步骤大体上有这几步
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;
}
接下来我们要做的就是要把已经得到的byte[]作为参数传递给Command对象
1.SqlServer数据库。SqlServer有Image字段类型,最大可以存储2G的数据。
byte[] fileData = this.FileUpload1.FileBytes;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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;//为参数赋值
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
sqlConn.Open();
sqlComm.ExecuteNonQuery();
sqlConn.Close();
2.Oracle数据库。在Oracle数据库中我们可以使用BLOB字段类型,最大可以存储4G的数据。
byte[] fileData = this.FileUpload1.FileBytes;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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;//为参数赋值
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
oraConn.Open();
oraComm.ExecuteNonQuery();
oraConn.Close();
byte[] fileData = this.FileUpload1.FileBytes;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
string sql = "insert into t_img(IMGDATA) values(?)";
string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringForAccess"].ToString();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
OleDbConnection oleConn = new OleDbConnection(strconn);
OleDbCommand oleComm = new OleDbCommand(sql, oleConn);
oleComm.Parameters.Add("imgdata", OleDbType.Binary);
oleComm.Parameters["imgdata"].Value = fileData;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oraConn.Open();
byte[] fileData = (byte[])oraComm.ExecuteScalar();
oraConn.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return fileData;
}
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;
}
如果你在开发WinForm应用的话你可以直接把返回结果保存或者显示到PictureBox里,如果你在使用ASP.Net那么你可以在单独的一个页面把图片输出,在另外一个页面把Image控件的ImageUrl属性指向图片输出页面。
比如输出页面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);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oraConn.Open();
byte[] fileData = (byte[])oraComm.ExecuteScalar();
oraConn.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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);
}
1.将图片转换为二进制数组(byte[]);
2.把转换后的二进制数组(byte[])作为参数传递给要执行的Command;
3.执行Command;
首先,如何把图片转换成byte[],如果你使用的是ASP.Net2.0,那么你可以使用FileUpLoad控件来实现
byte[] fileData = this.FileUpload1.FileBytes;
如果你用的是ASP.Net1.1或者你在创建WinForm那么你可以使用下面的方法来把图片转换为byte[]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1.SqlServer数据库。SqlServer有Image字段类型,最大可以存储2G的数据。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2.Oracle数据库。在Oracle数据库中我们可以使用BLOB字段类型,最大可以存储4G的数据。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
注意:这里我需要说明一下,用Oracle的专用连接传递参数的时候你要小心一点,看看上面的SQL语句你就会知道,要在参数名前加个“:”否则就会出现
下面的错误“OracleException: orA-01036:
非法的变量名/编号”。这里需要我们注意一下。另外还有一个地方,当我引用System.Data.OracleClient命名空间的时候默认是没有
的,必须添加对System.Data.OracleClient的引用,我记得在VS2003下如果安装了OracleClient是不用添加引用就可
以引入的。这里也要留意一下。
3.Access数据库。在Access中我们使用OLE对象字段类型,最大支持1G的数据。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
好了,到这里我们就把图片保存到数据库中全部说完了,接下来要说的是如何从数据库中把图片读取出来。实际上这是与插入操做相反的一个过程:先报把从数据库
中获取的图片数据转换为数组,然后把数组转换为图片。不同数据之间没有特别大的差异,我这里只列出从Oracle数据库中把数据读取出来以供参考。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
我们获取到了数据,那么把byte[]转换为图片的过程都是一样的。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
比如输出页面getImg.aspx的代码
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)