asp.net 图片的读写入库实现代码
程序员文章站
2024-03-08 19:57:22
写图片c:\1.jpg到表cinfo中 复制代码 代码如下: private static void addcinfo() { string strsql = "inser...
写图片c:\1.jpg到表cinfo中
private static void addcinfo()
{
string strsql = "insert into cinfo (srvtitle,csttitle,introduction,logo) values
(@srvtitle,@csttitle,@introduction,@logo)";
sqlparameter[] parms =
{
new sqlparameter("@srvtitle",sqldbtype.varchar,30),
new sqlparameter("@csttitle",sqldbtype.varchar,30),
new sqlparameter("@introduction",sqldbtype.nvarchar,500),
new sqlparameter("@logo",sqldbtype.image)
};
parms[0].value = "旅业互动";
parms[1].value = "lyhd";
parms[2].value = "简介";
string filepath = @"c:\1.jpg";
filestream fs = file.openread(filepath);
byte[] content = new byte[fs.length];
fs.read(content, 0, content.length);
fs.close();
parms[3].value = content;
dbhelper.executenonquery(commandtype.text, strsql, parms);
}
读取图片的页面 test.aspx
protected void page_load(object sender, eventargs e)
{
string strsql = "select * from cinfo where id=1";
sqldatareader reader=dbhelper.executereader(commandtype.text, strsql, null);
if(reader.read())
{
byte[] c=(byte[])reader["logo"];
response.binarywrite(c);
}
}
用来显示图片的页面 test2.aspx
<img src="test.aspx" />
复制代码 代码如下:
private static void addcinfo()
{
string strsql = "insert into cinfo (srvtitle,csttitle,introduction,logo) values
(@srvtitle,@csttitle,@introduction,@logo)";
sqlparameter[] parms =
{
new sqlparameter("@srvtitle",sqldbtype.varchar,30),
new sqlparameter("@csttitle",sqldbtype.varchar,30),
new sqlparameter("@introduction",sqldbtype.nvarchar,500),
new sqlparameter("@logo",sqldbtype.image)
};
parms[0].value = "旅业互动";
parms[1].value = "lyhd";
parms[2].value = "简介";
string filepath = @"c:\1.jpg";
filestream fs = file.openread(filepath);
byte[] content = new byte[fs.length];
fs.read(content, 0, content.length);
fs.close();
parms[3].value = content;
dbhelper.executenonquery(commandtype.text, strsql, parms);
}
读取图片的页面 test.aspx
复制代码 代码如下:
protected void page_load(object sender, eventargs e)
{
string strsql = "select * from cinfo where id=1";
sqldatareader reader=dbhelper.executereader(commandtype.text, strsql, null);
if(reader.read())
{
byte[] c=(byte[])reader["logo"];
response.binarywrite(c);
}
}
用来显示图片的页面 test2.aspx
复制代码 代码如下:
<img src="test.aspx" />