使用存储在sql数据库中的文件路径检索图像

时间:2009-12-02 09:43:24

标签: c# asp.net image

我的SQL数据库中有一个存储AccountID和ImageID的表 - ImageID是.gif格式的实际帐户图像的文件路径。

我需要做的是,将图像从我的SQL数据库中保存的文件路径检索到图像控件上吗?我在c#中使用asp.net。

当访问页面时,可能会根据登录的帐户,检索特定于该帐户的图像(我想我可以在以后设置@AccountID参数)

如果有人为他们做过类似的事情提供了示例代码,我将非常感谢您可能提供的任何指示。

3 个答案:

答案 0 :(得分:1)

以下是一个示例:

var connectionString = ConfigurationManager.ConnectionStrings["SomeCN"].ConnectionString;
using (var cn = new SqlConnection(connectionString))
using (var cmd = cn.CreateCommand())
{
    cn.Open();
    cmd.CommandText = "select imageid from accounts where accountid = @accountid";
    cmd.Parameters.AddWithValue("@accountid", 5);
    using (var reader = cmd.ExecuteReader())
    {
        if (reader.Read())
        {
            var filePath = reader.GetString(0);
            // For this to work images must be stored inside the web application.
            // filePath must be a relative location inside the virtual directory
            // hosting the application. Depending on your environment some
            // transformations might be necessary on filePath before assigning it
            // to the image url.
            imageControl.ImageUrl = filePath;
        }
    }
}

答案 1 :(得分:0)

海, 看一下这个... 在你的aspx中:

<div>
    <asp:FileUpload ID="ImgUpload" runat="server" />
    <asp:Button ID="Upload" runat="server" Text="Upload" onclick="Upload_Click" />
    <asp:Image ID="samp" runat="server" ImageUrl="~/images/new//Testingimages1253899911515.gif " />
</div>

在你的aspx.cs中:

protected void Page_Load(object sender, EventArgs e)
{

}
private bool IsImage(HttpPostedFile file)
{
    if (file != null && Regex.IsMatch(file.ContentType, "image/\\S+") &&
      file.ContentLength > 0)
    {
        return true;
    }
    return false;
}
public string SaveImageFile(FileUpload fu, string directoryPath, int MaxWidth, int MaxHeight, string prefixName)
{
    string serverPath = "", returnString = "";
    if (fu.HasFile)
    {
        Byte[] bytes = fu.FileBytes;
        //Int64 len = 0;
        prefixName = "Testing" + prefixName;
        //directoryPath = "Testing/";
        System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
        System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
        string dipath = System.Web.HttpContext.Current.Server.MapPath("~/") + directoryPath;
        DirectoryInfo di = new DirectoryInfo(dipath);
        if (!(di.Exists))
        {
            di.Create();
        }
        HttpPostedFile file = fu.PostedFile;
        DateTime oldTime = new DateTime(1970, 01, 01, 00, 00, 00);
        DateTime currentTime = DateTime.Now;
        TimeSpan structTimespan = currentTime - oldTime;
        prefixName += ((long)structTimespan.TotalMilliseconds).ToString();
        if (IsImage(file))
        {
            using (Bitmap bitmap = new Bitmap(file.InputStream, false))
            {
                serverPath = dipath + "//" + prefixName + fu.FileName.Substring(fu.FileName.IndexOf("."));
                img.Save(serverPath);
                returnString = "~/" + directoryPath + "//" + prefixName + fu.FileName.Substring(fu.FileName.IndexOf("."));
            }
        }
    }
    return returnString;
}

protected void Upload_Click(object sender, EventArgs e)
{
    string imageUrl;
    imageUrl = SaveImageFile(ImgUpload, "images/new", 600, 600, "images");
    Response.Write(imageUrl);
}

答案 2 :(得分:0)

尝试这些链接可能对您有帮助..

您也可以尝试将图像文件存储在服务器上并将路径存储在Sql表中..通过这些链接

http://pratikataspdotnet.blogspot.in/2014/11/retrieve-images-from-path-stored-in.html