如何把图片直接存入sql server2000数据库,然后读取,asp.net c#
如何把图片直接存入sqlserver2000数据库,然后读取,asp.netc#不是存入图片路径,然后进行读取,显示等操作,这样能实现吗?...
如何把图片直接存入sql server2000数据库,然后读取,asp.net c#
不是存入图片路径,然后进行读取,显示等操作,这样能实现吗? 展开
不是存入图片路径,然后进行读取,显示等操作,这样能实现吗? 展开
4个回答
展开全部
上传图片前台页代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadFile.aspx.cs" Inherits="UploadFile" %>
<!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>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
<asp:Image ID="pic" runat="server" ImageUrl="~/ImageReader.aspx?id=1" />
</div>
</form>
</body>
</html>
上传图片后台页代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.SqlClient;
namespace WebKB.Manager.Module.PictureGroup
{
public partial class UploadFile: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Upload_bt_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
HttpPostedFile hpf = FileUpload1.PostedFile;
int filelenth = (int)hpf.ContentLength;
string filename = hpf.FileName;
byte[] filebyte = new byte[4096];
Stream FileStream = hpf.InputStream;
while (filelenth > 0)
{
filelenth -= FileStream.Read(filebyte, 0, Math.Min(filelenth, filebyte.Length));
}
SqlConnection con = new SqlConnection(FrameWork.Common.GetConnString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add("@ID", SqlDbType.UniqueIdentifier);
cmd.Parameters["@ID"].Value = Guid.NewGuid();
cmd.Parameters.Add("@Title", SqlDbType.VarChar);
if (Title_txt.Text != "")
cmd.Parameters["@Title"].Value = Title_txt.Text;
else
cmd.Parameters["@Title"].Value = String.Empty;
cmd.Parameters.Add("@Content", SqlDbType.VarBinary);
cmd.Parameters["@Content"].Value = filebyte;
cmd.Parameters.Add("@Lenth", SqlDbType.BigInt);
cmd.Parameters["@Lenth"].Value = FileUpload1.FileContent.Length/1024;
cmd.Parameters.Add("@De", SqlDbType.VarChar);
if (Description_txt.Text != "")
cmd.Parameters["@De"].Value = Description_txt.Text;
else
cmd.Parameters["@De"].Value = String.Empty;
cmd.Parameters.Add("@Time", SqlDbType.DateTime);
cmd.Parameters["@Time"].Value = DateTime.Now;
cmd.CommandText = "INSERT INTO Pictures values(@ID,@Title,@Content,@Lenth,@De,@Time)";
cmd.Connection = con;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
Response.Write("<script>javascript:alert('上传成功!');location.href='PictureList.aspx';</script>");
}
else
{
Response.Write("<script>alert('请选择文件!')</script>");
}
}
}
}
读取图片的页面:
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageReader.aspx.cs" Inherits="ImageReader" %>
<!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>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class ImageReader : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["id"].ToString();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
Guid gid = new Guid(id);
cmd.CommandText = "SELECT Picture_Content From Pictures WHERE PictureID='" + gid + "'";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Response.Clear();
Response.BinaryWrite((byte[])dr["Picture_Content"]);
}
Response.End();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadFile.aspx.cs" Inherits="UploadFile" %>
<!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>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
<asp:Image ID="pic" runat="server" ImageUrl="~/ImageReader.aspx?id=1" />
</div>
</form>
</body>
</html>
上传图片后台页代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.SqlClient;
namespace WebKB.Manager.Module.PictureGroup
{
public partial class UploadFile: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Upload_bt_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
HttpPostedFile hpf = FileUpload1.PostedFile;
int filelenth = (int)hpf.ContentLength;
string filename = hpf.FileName;
byte[] filebyte = new byte[4096];
Stream FileStream = hpf.InputStream;
while (filelenth > 0)
{
filelenth -= FileStream.Read(filebyte, 0, Math.Min(filelenth, filebyte.Length));
}
SqlConnection con = new SqlConnection(FrameWork.Common.GetConnString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add("@ID", SqlDbType.UniqueIdentifier);
cmd.Parameters["@ID"].Value = Guid.NewGuid();
cmd.Parameters.Add("@Title", SqlDbType.VarChar);
if (Title_txt.Text != "")
cmd.Parameters["@Title"].Value = Title_txt.Text;
else
cmd.Parameters["@Title"].Value = String.Empty;
cmd.Parameters.Add("@Content", SqlDbType.VarBinary);
cmd.Parameters["@Content"].Value = filebyte;
cmd.Parameters.Add("@Lenth", SqlDbType.BigInt);
cmd.Parameters["@Lenth"].Value = FileUpload1.FileContent.Length/1024;
cmd.Parameters.Add("@De", SqlDbType.VarChar);
if (Description_txt.Text != "")
cmd.Parameters["@De"].Value = Description_txt.Text;
else
cmd.Parameters["@De"].Value = String.Empty;
cmd.Parameters.Add("@Time", SqlDbType.DateTime);
cmd.Parameters["@Time"].Value = DateTime.Now;
cmd.CommandText = "INSERT INTO Pictures values(@ID,@Title,@Content,@Lenth,@De,@Time)";
cmd.Connection = con;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
Response.Write("<script>javascript:alert('上传成功!');location.href='PictureList.aspx';</script>");
}
else
{
Response.Write("<script>alert('请选择文件!')</script>");
}
}
}
}
读取图片的页面:
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageReader.aspx.cs" Inherits="ImageReader" %>
<!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>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class ImageReader : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["id"].ToString();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
Guid gid = new Guid(id);
cmd.CommandText = "SELECT Picture_Content From Pictures WHERE PictureID='" + gid + "'";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Response.Clear();
Response.BinaryWrite((byte[])dr["Picture_Content"]);
}
Response.End();
}
}
展开全部
当然可以实现。
把图片路径存在数据库中,~/images/a.jpg
用<asp:Image>控件读取!
把图片路径存在数据库中,~/images/a.jpg
用<asp:Image>控件读取!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
实现是可以实现的,但是一般不会把图片存入数据库。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以的 数据库有个image数据类型 先把图片转换成二进制数据存入 读取的时候再转回来
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询