标题:图片上传到数据库和在页面显示的问题
只看楼主
xiwdan
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-5-3
 问题点数:0 回复次数:2 
图片上传到数据库和在页面显示的问题

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


public partial class admin_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

SqlConnection conn = new SqlConnection("server=ZENGDAN;database=tushu; user id=sa;pwd=111");
conn.Open();
string selectstr = " SELECT COUNT(*) FROM BookInfo WHERE (BookName = @dname)";//OR (PublishingHouse =@dpublish) OR (Author =@dauthor) OR (Translator =@dtranslator) OR (ISBN =@disbn) OR (PublishDate =@dpublishdate) OR (Price =@dprice)  OR (Description =@ddescription) OR (suoyinhao =@dsuoyinhao)";


SqlCommand cmd1 = new SqlCommand(selectstr, conn);
cmd1.Parameters.Add("@dname", SqlDbType.VarChar, 80).Value = TextBox1.Text.ToString();
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.Read())
{

if ((int)dr.GetValue(0) > 0)
{
Response.Write("<script>alert(\"图书类型名已经存在\")</script>");
}

else
{


dr.Close();
conn.Close();

string Sqlstring = "Insert into BookInfo(BookName,PublishingHouse,Author,Translator,ISBN,PublishDate,Price,Description,suoyinhao,booktype)values(@dname,@dpublish,@dauthor,@dtranslator,@disbn,@dpublishdate,@dprice,@ddescription,@dsuoyinhao,@dbooktype)";

conn.Open();
SqlCommand cmd = new SqlCommand(Sqlstring, conn);
cmd.Parameters.Add("@dname", SqlDbType.VarChar, 80).Value = TextBox1.Text.ToString();
cmd.Parameters.Add("@dpublish", SqlDbType.VarChar, 80).Value = TextBox9.Text.ToString();
cmd.Parameters.Add("@dauthor", SqlDbType.VarChar, 80).Value = TextBox3.Text.ToString();
cmd.Parameters.Add("@dtranslator", SqlDbType.VarChar, 80).Value = TextBox7.Text.ToString();
cmd.Parameters.Add("@disbn", SqlDbType.VarChar, 80).Value = TextBox4.Text.ToString();
cmd.Parameters.Add("@dpublishdate", SqlDbType.VarChar,80).Value = TextBox8.Text.ToString();
cmd.Parameters.Add("@dprice", SqlDbType.VarChar, 80).Value = TextBox5.Text.ToString();

cmd.Parameters.Add("@ddescription", SqlDbType.Text, 16).Value = TextBox10.Text.ToString();
//cmd.Parameters.Add("@dcover", SqlDbType.VarChar, 50).Value = TextBox2.Text.ToString();
cmd.Parameters.Add("@dbooktype", SqlDbType.Int, 4).Value = Convert.ToInt32(DropDownList1.SelectedValue);

cmd.Parameters.Add("@dsuoyinhao", SqlDbType.VarChar,80).Value = TextBox6.Text.ToString();
cmd.ExecuteNonQuery();

Response.Write("<script>alert(\"添加成功\")</script>");
conn.Close();
Response.Write("<br><br><meta http-equiv='refresh' content='1;url=booklitterset.aspx'><br>");
//Response.Redirect("booklitterset.aspx");
}

}


}
protected void Button2_Click(object sender, EventArgs e)
{
int a = 1;
bool fileOK = false;
string path = Server.MapPath("~/admin/upload/");
if (FileUpload1.HasFile)
{
String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
string[] allowedExtension ={ ".bmp", ".pcx", ".png", ".tiff", ".dxf", ".cgm", ".cdr", ".wmf", ".eps", ".jpeg", ".gif", ".jpg" };
for (int i = 0; i < allowedExtension.Length; i++)
{
if (fileExtension == allowedExtension[i])
{
fileOK = true;
}
}
}
if (fileOK)
{
// string a="~/admin/upload/";
try
{
FileUpload1.SaveAs(path + FileUpload1.FileName);
Label1.Text = path + FileUpload1.FileName;

a = 2;

}
catch (Exception ex)
{
// Label4.Text = "文件上传不成功!";
//Response.Write("<script>alert(\"文件名称和描述不能为空!\")</script>");
}
}
else
{
Response.Write("<script>alert(\"只能够上传图片文件!!\")</script>");
}
try
{
if (a != 1)
{
SqlConnection conn1 = new SqlConnection("server=ZENGDAN;database=tushu; user id=sa;pwd=111");
conn1.Open();

string insertstr = "insert into image(image) " + "values ('" + this.Label1.Text.ToString() + "')";
// string insertstr = "insert into login (useName,password) values ('" + nameTxt.Text.ToString() + "','" + a.ToString() + "')";
SqlCommand cmd1 = new SqlCommand(insertstr, conn1);

cmd1.ExecuteNonQuery();
Response.Write("<script>alert(\"插入成功\")</script>");

conn1.Close();
}
else
{
Response.Write("<script>alert(\"插入失败!\")</script>");
}
}

catch (Exception ex)
{
Response.Write("<script>alert(\"文件上传不成功!!!文件名相同,请修改图片名称!\")</script>");
//Response.Write("<script>alert(\"文件名称和描述不能为空!\")</script>");
}
}

}

怎么把要上传的图片用 Image1 显示出来 并把它保存到数据库去
我这段代码的结果是其它信息都保存进去数据库了
但是图片 cover 数据类型是 image 这项是空
请各位高手给点意见




搜索更多相关主题的帖子: using System 数据库 Web 页面 
2007-05-11 17:12
cyyu_ryh
Rank: 8Rank: 8
等 级:贵宾
威 望:45
帖 子:1899
专家分:176
注 册:2006-10-21
得分:0 
直接粘贴撒,这样看起好烦哦
还有就是你错在哪里,请指出

有事无事都密我. MSN: cyyu_ryh@hotmail.co.jp E-mail: cyyu_ryh@
2007-05-11 17:57
beniao
Rank: 2
等 级:新手上路
威 望:4
帖 子:367
专家分:2
注 册:2004-12-17
得分:0 
详细情况我在 "求助!!怎样把照片保存在数据库中?" 这个帖里已经说得很清楚了,可以去参考下.

博客:http://
2007-05-11 18:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-138611-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.644491 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved