我想显示存储在数据库中的图像 [英] I want to display image that stored in database
问题描述
这是我的数据库:
This is my database:
create table images
(
ID int primary key identity,
Name nvarchar(255),
Size int,
ImgData varbinary(max)
)
CREATE PROCEDURE UploadImages
@Name nvarchar(255),
@Size int,
@ImgData varbinary(max),
@NewId int output
AS
BEGIN
INSERT INTO images
VALUES (@Name, @Size, @ImgData)
SELECT @NewId = SCOPE_IDENTITY()
END
这是我的代码:
This is my code:
HttpPostedFile PostedFile = FileUpload1.PostedFile;
string fileName = Path.GetFileName(PostedFile.FileName);
string fileExtension = Path.GetExtension(fileName);
int fileSize = PostedFile.ContentLength;
if(fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".bmp"|| fileExtension.ToLower() == ".gif" || fileExtension.ToLower() == ".png")
{
Stream stream = PostedFile.InputStream;
BinaryReader binaryReader = new BinaryReader(stream);
byte[] bytes = binaryReader.ReadBytes((int)stream.Length);
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("UploadImages", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlParameter paramName = new SqlParameter()
{
ParameterName = "@Name",
Value = fileName
};
cmd.Parameters.Add(paramName);
SqlParameter paramSize = new SqlParameter()
{
ParameterName = "@Size",
Value = fileSize
};
cmd.Parameters.Add(paramSize);
SqlParameter paramImgData = new SqlParameter()
{
ParameterName = "@ImgData",
Value = bytes
};
cmd.Parameters.Add(paramImgData);
SqlParameter paramNewId = new SqlParameter()
{
ParameterName = "@NewId",
Value =-1,
Direction = ParameterDirection.Output
};
cmd.Parameters.Add(paramNewId);
cmd.ExecuteNonQuery();
con.Close();
Lmas.Visible = true;
Lmas.Text = "done";
Lmas.ForeColor = System.Drawing.Color.Green;
HyperLink1.Visible = true;
HyperLink1.NavigateUrl = "~/ShowImage.aspx?Id=" + cmd.Parameters["@NewId"].Value.ToString();
//LoadImage();
}
}
else
{
Lmas.Visible = true;
Lmas.Text = "only images (.jpg .png .gif .bmp) can be uploaded";
Lmas.ForeColor = System.Drawing.Color.Red;
HyperLink1.Visible = false;
}
-------------------
我尝试过:
我试图在第一页的img-tag或label中显示我的图像相同的页面,但它不起作用,我试过这个代码,但不工作:
-------------------
What I have tried:
I tried to display my image in img-tag or label in first page not in the same page but it does not work , I tried this code but not work:
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetImageById", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramID = new SqlParameter()
{
ParameterName = "@ID",
Value = Request.QueryString["ID"]
};
cmd.Parameters.Add(paramID);
con.Open();
cmd.ExecuteScalar();
byte[] bytes = (byte[])cmd.ExecuteScalar();
string strBase64 = Convert.ToBase64String(bytes);
Image1.ImageUrl = "data:Image/png;base64," + strBase64;
推荐答案
看一下表定义:
Take a look at table definition:
create table images
(
ID int primary key identity, --required!
Name nvarchar(255),
Size int,
ImgData varbinary(max)
)
和SP中的INSERT语句:
and your INSERT statement in SP:
INSERT INTO images
VALUES (@Name, @Size, @ImgData)
你错过了什么? ID
!
根据具体情况,您必须:
What you miss? ID
!
Depending on situation, you have to:
- 将
ID
字段定义更改为
- change
ID
field definition to
ID int primary key identity(1,1)
提供自动增量。然后你就可以忽略 ID
。
to provide autoincrement. Then you'll be able to ignore ID
.
INSERT INTO images (Name, Size, ImgData)
VALUES (@Name, @Size, @ImgData)
或
INSERT INTO images (ID, Name, Size, ImgData)
VALUES (@ID, @Name, @Size, @ImgData)
注意,C#代码应相应更改!
有关详细信息,请参阅过去的答案:>
如何将图像插入MS Access [ ^ ]
在访问数据库中插入图像 [ ^ ]
For further details, please see past answers:
How will I insert an image to MS Access[^]
insert image in access database[^]
这篇关于我想显示存储在数据库中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!