62,025
社区成员
发帖
与我相关
我的任务
分享
1. 如何显示
//前台放一个datalist
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("图片地址字段") %>' />
</ItemTemplate>
</asp:DataList>
//后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBind();
}
}
//数据绑定方法
public void DataBind()
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;User ID=sa;Password=123456");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "select * from Tb1 ";//换成你自己的表
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
con.Close();
DataList1.DataSource = dt;
DataList1.DataBind();
}
//2 如何上传图片,返回string类型你直接存入数据库中即可(下面的Files是上传控件FileUpload)
public string filesUrl()
{
string Path;
string FileName;
int Po;
long FileSize;
string fType;
try
{
Path = Server.MapPath("\\UpFiles").ToString(); //upfiles是你上传图放的文件夹
FileName = Files.PostedFile.FileName.ToString();
fType = Files.PostedFile.ContentType;
FileSize = Files.PostedFile.ContentLength;
Po = FileName.LastIndexOf((char)92);
FileName = FileName.Substring(Po + 1, FileName.Length - Po - 1);
ViewState["PostFileName"] = FileName; //Remember PostFile's name
Files.PostedFile.SaveAs(Path + "\\" + FileName);
string filesurl = "\\UpFiles\\" + FileName;
return filesurl;
}
catch (Exception er)
{
Response.Write("<script>alert('" + er + "');</script>");
return null;
}
}