使用没有处理程序的webservice从SQLSERVER数据库中检索图像?

时间:2014-05-22 21:32:06

标签: c# asp.net sql-server web-services asmx

我想知道是否有任何方法从SqlServer检索图像而不使用Web服务[Asp.net]中的处理程序?

我目前正在使用此代码

在我的webservice.cs文件中

 public class WebService : System.Web.Services.WebService {

        SqlConnection con = new SqlConnection();
        public WebService () {
            con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
            if (con.State == ConnectionState.Closed)
            {
                con.Open();

            }
            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }

        [WebMethod]
        public string HelloWorld() {
            return "Hello World";
        }
        [WebMethod]
        public void Save_Image(Int32 id, Byte[] ar)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "insert tbimages values(@imageid,@imagename)";
            cmd.Connection = con;
            cmd.Parameters.Add("@imageid", SqlDbType.Int).Value = id;
            cmd.Parameters.Add("@imagename", SqlDbType.Image).Value = ar;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }
        [WebMethod]

        public Byte[] Ret_Image(Int32 id)
        {
            SqlCommand cmd =new SqlCommand();
            cmd.CommandText="Select * from tbimages where imageid=@imageid";
            cmd.Connection = con;
            cmd.Parameters.Add("@imageid",SqlDbType.Int).Value = id;
           SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            Byte[] ar = (Byte[])(dr[1]);
            dr.Close();
            cmd.Dispose();
            return ar;

           }        
    }

在我使用/使用网络服务的示例网站中我正在使用此代码,即在default.aspx.cs文件中

public partial class _Default : System.Web.UI.Page
{
    xyz.WebService obj = new xyz.WebService();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Int32 In = FileUpload1.PostedFile.ContentLength;
        Byte[] ar = new Byte[In];
        FileStream fs = new FileStream(FileUpload1.PostedFile.FileName, FileMode.Open, FileAccess.ReadWrite);
        fs.Read(ar, 0, In - 1);
        fs.Close();
        obj.Save_Image(Convert.ToInt32(TextBox1.Text), ar);
        TextBox1.Text = string.Empty;
        TextBox1.Focus();


    }
   protected void Button2_Click(object sender, EventArgs e)
    {
        Byte[] ar = obj.Ret_Image(Convert.ToInt32(TextBox3.Text));
        string st = Server.MapPath("ar"); 
        FileStream fs = new FileStream(st, FileMode.Create, FileAccess.Write);
        fs.Write(ar, 0, ar.Length - 1);
        fs.Close();
        Image1.ImageUrl = st;
    }
}

根据我这一行[string st = Server.MapPath(" ar");]在受保护的void中Button2_Click(对象发送者,EventArgs e)应该被更改,但我不会#39;我知道我必须使用哪个命令


以下代码是default.aspx文件的设计**

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>Image Id:</td>
    <td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td>Image Path:</td>
    <td><asp:FileUpload ID="FileUpload1" runat="server" /> </td>
    </tr>
    <tr><td colspan="2" align="center"><asp:Button ID ="Button1" runat="server" 
            Text="Upload" onclick="Button1_Click" /></td></tr>
    </table>
    <table>
    <tr><td> Enter Image ID </td>
    <td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td></tr>
    <tr><td colspan="2" align="center"><asp:Button ID="Button2" runat="server" 
            onclick="Button2_Click" Text="Retrieve Image" /></td></tr>
    </table>
        <br />
        <br />
        <asp:Image ID="Image1" runat="server" />
    </div>
    </form>
</body>
</html>

数据库表名称为tbimages

有两列

  1. imageid [datatype- Int]
  2. 2。 imagename [datatype - Image]

    这是我的第一篇文章,所以如果我有任何错误,请同时提及。

1 个答案:

答案 0 :(得分:0)

您始终可以将图像路径/网址保存在数据库中,并在以后需要时进行检索。将图片网址放在html源标记中。请参考下面的示例:

<img src="<% image url %>"></img>

希望这有帮助!