从oracle中取图片,并显示!如果是.net2002那么需要另外安转专门的oracle的类库![]()
---------------------------------------------------------------------------------------------------------
Imports System.Drawing
Dim conn As New OracleClient.OracleConnection()
Dim cmd As New OracleClient.OracleCommand()
Dim myReader As OracleClient.OracleDataReader
Dim sql As String
Dim fl As File
sql = "select jt from sd_gtzp" '从数据库中取出图片数据 blob
conn.ConnectionString = "Password=jlsbgis;User ID=jlsbgis;Data Source=sj"
cmd.CommandText = sql
cmd.Connection = conn
conn.Open()
myReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
myReader.Read()
Dim fs As FileStream ' Writes the BLOB to a file (*.bmp).
Dim bw As BinaryWriter ' Streams the binary data to the FileStream object.
Dim bufferSize As Integer = 1000 ' The size of the BLOB buffer.
Dim outbyte(bufferSize - 1) As Byte ' The BLOB byte() buffer to be filled by GetBytes.
Dim retval As Long ' The bytes returned from GetBytes.
Dim startIndex As Long = 0 ' The starting position in the BLOB output.
Dim fpath As String
fpath = Server.MapPath(Request.ApplicationPath) & "/Image/photo.bmp" '将图片数据存成本地文件
fs = New FileStream(fpath, FileMode.OpenOrCreate, FileAccess.Write)
bw = New BinaryWriter(fs)
' Reset the starting byte for a new BLOB.
startIndex = 0
' Read bytes into outbyte() and retain the number of bytes returned.
retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize)
' Continue reading and writing while there are bytes beyond the size of the buffer.
Do While retval = bufferSize
bw.Write(outbyte)
bw.Flush()
' Reposition the start index to the end of the last buffer and fill the buffer.
startIndex = startIndex + bufferSize
retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize)
Loop
' Write the remaining buffer.
bw.Write(outbyte)
bw.Flush()
' Close the output file.
bw.Close()
fs.Close()
' Close the reader and the connection.
myReader.Close()
conn.Close()
Dim logo As Image '文件格式转换
logo = Image.FromFile(fpath)
fpath = Server.MapPath(Request.ApplicationPath) & "/Image/zkjhy.jpeg"
logo.Save(fpath, System.Drawing.Imaging.ImageFormat.Jpeg)
logo.Dispose()
logo = Nothing
Me.Image1.Width = New Unit(300) '调整显示大小
Me.Image1.Height = New Unit(350)
Me.Image1.ImageUrl = "image/zkjhy.jpeg"
-----------------------------------------------------------------------------------------------------------------------------
//存入图片![]()
private void Button1_Click(object sender, System.EventArgs e)
{
OracleConnection con = new OracleConnection("Password=jlsbgis;User ID=jlsbgis;Data Source=sj");
OracleDataAdapter da = new OracleDataAdapter("Select * From MyImages", con);
OracleCommandBuilder MyCB = new OracleCommandBuilder(da);
DataSet ds = new DataSet("MyImages");
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
FileStream fs = new FileStream(@"d:/a.jpg", FileMode.OpenOrCreate, FileAccess.Read);
byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
da.Fill(ds,"MyImages");
DataRow myRow=ds.Tables["MyImages"].NewRow();
myRow["Description"] = "This would be description text";
myRow["imgField"] = MyData;
ds.Tables["MyImages"].Rows.Add(myRow);
da.Update(ds, "MyImages");
con.Close();
}
-----------------------------------------------------------------------------------------------------------------------------
//取出图片![]()
private void Button2_Click(object sender, System.EventArgs e)
{
OracleConnection con = new OracleConnection("Password=jlsbgis;User ID=jlsbgis;Data Source=sj");
OracleDataAdapter da = new OracleDataAdapter("Select * From MyImages",con);
OracleCommandBuilder MyCB = new OracleCommandBuilder(da);
DataSet ds = new DataSet("MyImages");
byte[] MyData= new byte[0];
da.Fill(ds,"MyImages");
DataRow myRow=ds.Tables["MyImages"].Rows[0];
MyData=(byte[])myRow["imgField"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);
FileStream fs = new FileStream(@"d:/b.jpg", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0,ArraySize);
fs.Close();
}
本文介绍了如何在Oracle数据库中存储和检索图片。通过.NET连接Oracle数据库,读取BLOB类型图片数据,写入本地文件,然后展示在网页上。同时,文章也展示了如何将图片数据存入数据库和从数据库中取出图片的代码示例。
1287

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



