如何使用.net将图像上传到sqlserver数据库 [英] how to upload image to sqlserver database using .net
问题描述
大家好我屏幕上有四个字段如下
id
ename
date
image使用fileupload控件
所以当我点击保存按钮时,所有字段都应输入数据库
之后在第二个屏幕中
i想要图像幻灯片放映我们在数据库中插入的与系统日期相关的图像
下面的图像选框滚动日期与sysdare相关的数据库的ename应该滚动
i想要完整的代码
假设这是我们的表格:
CREATE TABLE dbo.MyPictures
(
Id INT IDENTITY ( 1 , 1 ) NOT NULL ,
图片 IMAGE NOT NULL ,
CONSTRAINT PK_MyPictures PRIMARY KEY CLUSTERED (Id)
);
此代码会将图像文件插入数据库。
string commandText = INSERT INTO dbo.MyPictures(Picture)VALUES(@picture);;
byte [] fileBuffer = File.ReadAllBytes( MyPicture.jpg跨度>);
使用(SqlConnection sqlConnection = new SqlConnection( data source = localhost; initial catalog = Scratch; integrated security = True;))
using (SqlCommand sqlCommand = new SqlCommand(commandText,sqlConnection))
{
sqlCommand.Parameters.AddWithValue ( @ picture,fileBuffer);
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
请务必阅读有关使用此数据类型的MSDN文档。这是一个固定长度的字段,这意味着如果图像是一个字节或100个字节,两者将使用相同的存储量。此外,我无法证实这一点,但有传言称微软计划放弃对此数据类型的支持,转而支持BINARY
和VARBINARY
。
ntext,text和image (Transact-SQL) [ ^ ]
按钮上的代码点击插入
string _path,_fname;
if (FileUpload1.HasFile)
{
_fname = FileUpload1.FileName;
_path =〜/ uploads /+ _ fname.ToString() ;
FileUpload1.SaveAs(Server.MapPath(_path));
string ins =插入到tblname(图片)值('+ _path +') ;
SqlCommand cmd = new SqlCommand(ins,conn);
conn.Open();
cmd.ExecuteNo nQuery();
conn.Close();
}
for select
string sel =从tblname中选择图片;
SqlDataAdapter da = new SqlDataAdapter(sel,conn);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count> 0)
{
Image1.ImageUrl = dt.Rows [0] [picture]。ToString();
}
hi all i have four fields in my screen like below
id
ename
date
image using fileupload control
so when i click on save button all field should enter in database
after that in second screen
i want images slide show withe the image we have inserted in database related to system date
below of that images marquee scrolling the "ename" of date indatabase related to sysdare should scroll
i want complete code
Assume this is our table:
CREATE TABLE dbo.MyPictures ( Id INT IDENTITY(1,1) NOT NULL, Picture IMAGE NOT NULL, CONSTRAINT PK_MyPictures PRIMARY KEY CLUSTERED (Id) );
This code will insert an image file into the database.
string commandText = "INSERT INTO dbo.MyPictures (Picture) VALUES (@picture);"; byte[] fileBuffer = File.ReadAllBytes("MyPicture.jpg"); using (SqlConnection sqlConnection = new SqlConnection("data source=localhost;initial catalog=Scratch;integrated security=True;")) using (SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection)) { sqlCommand.Parameters.AddWithValue("@picture", fileBuffer); sqlConnection.Open(); sqlCommand.ExecuteNonQuery(); sqlConnection.Close(); }
Make sure you read the MSDN documentation on using this data type. This is a fixed length field which means if the image is one byte or 100 bytes, both will use the same amount of storage. Also, I wasn't able to confirm this, but rumors have been that Microsoft is planning on dropping support of this data type in favor of theBINARY
andVARBINARY
.
ntext, text, and image (Transact-SQL)[^]
code on button click for insert
string _path, _fname;
if (FileUpload1.HasFile)
{
_fname = FileUpload1.FileName;
_path="~/uploads/"+_fname.ToString();
FileUpload1.SaveAs(Server.MapPath(_path));
string ins = "insert into tblname(picture)values('" + _path + "')";
SqlCommand cmd = new SqlCommand(ins, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
for select
string sel = "select picture from tblname";
SqlDataAdapter da = new SqlDataAdapter(sel,conn);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Image1.ImageUrl = dt.Rows[0]["picture"].ToString();
}
这篇关于如何使用.net将图像上传到sqlserver数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!