问题描述:

I want to know is there any method to retrieve image from SqlServer without using handlers in web services[Asp.net]?

I am currently using this code

In my webservice.cs file

 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 [email protected]";

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;

}

}


and in my sample website where i am using/consuming web services i am using this code i.e. in default.aspx.cs file

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;

}

}

According to me this line [string st = Server.MapPath("ar");] in protected void Button2_Click(object sender, EventArgs e) should be changed but i don't know which command i have to use


Below code is of design of default.aspx file**

 <%@ 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>


Database table name is tbimages

having two columns

  1. imageid [datatype- Int]

2. imagename [datatype -- Image]

This is my first post so if i have any mistakes, please mention it also.

网友答案:

You can always save the image path/url in the database and retrieve it later when you need it. Place the image url in the html source tag. Please refer to example below :

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

Hope this helps!

相关阅读:
Top