Это код консультации:
protected void btnbuscar_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source = MEGATRON; Initial Catalog = RentCar; Integrated Security = True");
try
{
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Autos WHERE Marca like '" + DropDownListMarcas.SelectedItem.Text + "%'", conn);
DataTable dt = new DataTable();
da.Fill(dt);
ListView1.DataSource = dt;
ListView1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
}
Это код сохраняемый:
SqlCommand insert = new SqlCommand("INSERT INTO Autos(codigo, Marca, Modelo,Año, NoPuertas, TipoCombustible, Tipotransmision, Capacidadasientos, Color, Precioxdia,img1) values(@Codigo, @Marca, @Modelo,@Año,@NoPuertas,@TipoCombustible,@Tipotransmision,@Capacidadasientos,@Color,@Precioxdia,@img1)", conn);
insert.Parameters.AddWithValue("@Codigo", txtcodigo.Text);
insert.Parameters.AddWithValue("@Marca", idmarca.SelectedItem.Text);
insert.Parameters.AddWithValue("@Modelo", DropDownListModelo.Text);
insert.Parameters.AddWithValue("@Año", DropDownano.Text);
insert.Parameters.AddWithValue("@NoPuertas", NoPuertas.Text);
insert.Parameters.AddWithValue("@TipoCombustible", txtCombustible.Text);
insert.Parameters.AddWithValue("@Tipotransmision", DropDownListtransmision.Text);
insert.Parameters.AddWithValue("@Capacidadasientos", txtAsientos.Text);
insert.Parameters.AddWithValue("@Color", DropDownListColor.Text);
insert.Parameters.AddWithValue("@Precioxdia", txtPrecioxdia.Text);
insert.Parameters.AddWithValue("@img1", SqlDbType.VarBinary).Value = FileUpload1.FileBytes;
У меня есть этот ListView, но это фотографии или изображение я не функционирует. Фотография, когда я сохраняю ее в базе данных, хранила ее в поле VarBinary (MAX).
<asp:ListView ID="ListView1" runat="server" >
<ItemTemplate>
<div class="hola">
<table>
<tr><td>
<asp:Image ID="Image1" runat="server" src='<%#Eval("img1") %>' /></td></tr>
<tr><td><h3><%# Eval("Marca") %></h3></td></tr>
<tr><td><h4><%# Eval("Modelo") %></h4></td></tr>
<tr><td><h4><%# Eval("Color") %></h4></td></tr>
</table>
</div>
</ItemTemplate>
</asp:ListView>
Если ты анализируешь как инструмент gridview в этом примере
[ASP.NET] GridView †“EdiciГіn Персонал
, который применяется равно для listview, потому что тема происходит, в котором ты будешь должен создавать handler, который возвратит байт array изображения в Response
<asp:TemplateField HeaderText="Imagen">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# Eval("IdEmpleado", "imagen.ashx?id={0}") %>' Width="100px" Height="100px" />
</ItemTemplate>
</asp:TemplateField>
, тогда проанализируй, как это .ashx
, которые возвращает изображение, как id или код реестра
public class HttpImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
byte[] imagenEmpleado = ... //aqui recuperas la imagen de la tabla
// Arma el contexto que enviara la imagen en el response
// se usa el nombre del empleado para el nombre del archivo que se envia
//
context.Response.Clear();
context.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", "nombreimagen"));
context.Response.ContentType = "image/jpg";
// Se escribe en el response la imagen asociada al empleado
//
context.Response.BinaryWrite(imagenEmpleado);
context.Response.End();
}
public bool IsReusable
{
get { return false; }
}
}
в web.config
помнит что регистрирует handler такой
<httpHandlers>
<add verb="*" path="*.ashx" type="WebAdminEmpleados.HttpImageHandler"/>
</httpHandlers>
это расширение, решает с кодом, где осуществляется
Все я функционирует хорошо, asГ - что я оставляю то, что у меня есть.
Здесь код файла aspx.cs
protected void btnbuscar_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source = MEGATRON; Initial Catalog = RentCar; Integrated Security = True");
try
{
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Autos WHERE Marca like '" + DropDownListMarcas.SelectedItem.Text + "%'" +
"AND Tipo like '" + DropDownListTipo.SelectedItem.Text + "%' ", conn);
DataTable dt = new DataTable();
da.Fill(dt);
ListView1.DataSource = dt;
ListView1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
}
Здесь код aspx:
<asp:Image ID="imgPhoto" runat="server"
ImageUrl='<%# "HttpImageHandler.ashx?ImID="+ Eval("Codigo") %>'
Height="150px" Width="150px"/>
Здесь код handdler (ashx.cs)
public void ProcessRequest(HttpContext context)
{
string imageid = context.Request.QueryString["ImID"];
SqlConnection con = new SqlConnection("Data Source = MEGATRON; Initial Catalog = RentCar; Integrated Security = True");
con.Open();
SqlCommand cmd = new SqlCommand("select img1 from Autos where Codigo=" + imageid, con);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((byte[])dr[0]);
con.Close();
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}