在现代网络应用中,远程通信是实现数据共享和交互的关键技术之一。本文将探讨如何通过XML Web服务实现图像数据在远程Oracle数据库中的存储与检索。
随着网络技术的发展,越来越多的应用需要处理远程数据库的数据交互。本文以一个实际案例为基础,介绍如何将图像数据存储到远程Oracle数据库中,并从数据库中检索这些图像。
要实现图像数据的存储与检索,首先需要在Oracle数据库中创建相应的数据表和存储过程。
在Oracle数据库中创建一个名为tblImageService的表,用于存储图像数据。
CREATE TABLE tblImageService (
Image_id TIMESTAMP DEFAULT SYSDATE,
image_data BLOB,
CONSTRAINT pk_image_id PRIMARY KEY (image_id)
);
创建一个名为InsertImage的存储过程,用于将图像数据插入到数据库中。
CREATE OR REPLACE PROCEDURE InsertImage (image_data IN BLOB) AS
BEGIN
INSERT INTO tblImageService (image_data) VALUES (image_data);
END;
创建一个Web服务,并暴露一个名为FireImage的方法,用于上传图像。
[WebMethod]
public int FireImage(string user_name, string password, byte[] news_image)
{
// 初始化与Oracle的连接
OracleConnection Conxn1 = new OracleConnection("user id=test;password=test;data source=jamb");
try
{
// 验证用户
if (user_name == "user_name" && password == "password")
{
try
{
// 打开连接
Conxn1.Open();
byte[] tempBuff = news_image;
OracleTransaction tx;
tx = Conxn1.BeginTransaction();
OracleCommand cmd;
cmd = Conxn1.CreateCommand();
cmd.CommandText = "declare xx BLOB; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;";
cmd.Parameters.Add(new OracleParameter("tempblob", OracleDbType.Blob)).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
OracleBlob tempLob = (OracleBlob)cmd.Parameters[0].Value;
tempLob.BeginChunkWrite();
tempLob.Write(tempBuff, 0, tempBuff.Length);
tempLob.EndChunkWrite();
cmd.Parameters.Clear();
cmd.CommandText = "InsertImage";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("image_data", OracleDbType.Blob)).Value = tempLob;
cmd.ExecuteNonQuery();
tx.Commit();
}
catch
{
}
finally
{
Conxn1.Close();
}
}
}
catch (Exception ex)
{
string showError = "Error: " + ex.Message;
}
finally
{
}
return 1;
}
创建一个名为FetchNewsImage的方法,用于从数据库中检索图像。
[WebMethod]
public string FetchNewsImage(string image_id)
{
OracleConnection Conxn1 = new OracleConnection("user id=test;password=test;data source=jamb");
OracleDataReader dtr = null;
string imageString = "";
try
{
Conxn1.Open();
string id = image_id;
OracleCommand cmd = new OracleCommand("select image_data from tblImageService where to_char(image_id,'DD-MON:YY-HH:MI:SS')='" + id + "'", Conxn1);
dtr = cmd.ExecuteReader();
byte[] arrpicture = new byte[0];
while (dtr.Read())
{
arrpicture = (byte[])dtr["image_data"];
}
imageString = Convert.ToBase64String(arrpicture);
}
catch (Exception ex)
{
string showError = "Error: " + ex.Message;
}
finally
{
dtr.Close();
Conxn1.Close();
}
return imageString;
}
创建一个名为AddImage.aspx的页面,使用FileUpload服务器控件浏览图像,然后调用FireImage方法。
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
byte[] tempBuff = new byte[Upload1.PostedFile.InputStream.Length];
Upload1.PostedFile.InputStream.Read(tempBuff, 0, Convert.ToInt32(Upload1.PostedFile.InputStream.Length));
Upload1.PostedFile.InputStream.Close();
newsImageDataService.ImageDataService fireImage = new newsImageDataService.ImageDataService();
fireImage.FireImage("user_name", "password", tempBuff);
Response.Write("Article Successfully saved!!");
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
finally
{
}
}
创建一个名为viewImage.aspx的页面,并在页面加载时添加以下代码。
protected void Page_Load(object sender, EventArgs e)
{
try
{
Conxn1.Open();
OracleCommand cmd = new OracleCommand("select '' as news_url from tblImageService ", Conxn1);
OracleDataReader dtr = cmd.ExecuteReader();
while (dtr.Read())
{
this.lblImage.Text = "- " + dtr["news_url"].ToString() + "
";
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
finally
{
Conxn1.Close();
}
}
创建一个名为LoadImage.aspx的页面,该页面负责实际生成图像。
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["image_id"].ToString();
try
{
newsImageDataService.ImageDataService getImage = new newsImageDataService.ImageDataService();
byte[] arrpicture = System.Convert.FromBase64String(getImage.FetchNewsImage(id));
Response.ContentType = "Image/Jpeg";
Response.BinaryWrite(arrpicture);
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
finally
{
}
}