将用户配置文件图像保存到数

时间:2010-09-16 13:04:53

标签: c# asp.net sql-server

当用户在ASP.NET中使用C#将图像文件上传到SQL Server 2005时,如何动态插入图像?这是为了让用户在我的网络应用中上传他们的个人资料照片。它与使用C#的Windows应用程序的完成方式有很大不同吗?

3 个答案:

答案 0 :(得分:2)

网上有一公吨的例子:

http://aspalliance.com/138
http://www.4guysfromrolla.com/articles/120606-1.aspx
http://www.aspfree.com/c/a/ASP.NET/Uploading-Images-to-a-Database--C---Part-I/

您应该能够遵循其中任何一项来完成您想要的任务。

答案 1 :(得分:1)

与WinForms中的方式相同。获取byte[]并与image列相同。但我强烈建议使用文件系统来存储图片。 DB用于关系数据,文件系统用于原始字节。

http://msdn.microsoft.com/en-us/library/aa479405.aspx

答案 2 :(得分:0)

以下是在C#中将图像插入数据库的代码示例。您将需要粗略支持表,图片应该是一个字节字段并保留图片类型,以便您可以稍后检索图像以显示它。除此之外,您还需要在页面上放置一个文件输入框和一个提交按钮。

public void AddImage(object sender, EventArgs e)
{

    int intImageSize;

    String strImageType;
    Stream ImageStream;
    FileStream fs = File.OpenRead(Request.PhysicalApplicationPath + "/Images/default_image.png");
    Byte[] ImageContent;

    if (PersonImage.PostedFile.ContentLength > 0)
    {
        intImageSize = PersonImage.PostedFile.ContentLength;
        strImageType = PersonImage.PostedFile.ContentType;
        ImageStream = PersonImage.PostedFile.InputStream;

        ImageContent = new Byte[intImageSize];
        int intStatus;
        intStatus = ImageStream.Read(ImageContent, 0, intImageSize);
    }
    else
    {
        strImageType = "image/x-png";
        ImageContent = new Byte[fs.Length];
        fs.Read(ImageContent, 0, ImageContent.Length);
    }

    SqlConnection objConn = new SqlConnection(ConfigurationManager.AppSettings["conn"]);
    SqlCommand objCmd;
    string strCmd;

    strCmd = "INSERT INTO ImageTest (Picture, PictureType) VALUES (@Picture, @PictureType)";

    objCmd = new SqlCommand(strCmd, objConn);

    SqlParameter prmPersonImage = new SqlParameter("@Picture", SqlDbType.Image);

    prmPersonImage.Value = ImageContent;

    objCmd.Parameters.Add(prmPersonImage);
    objCmd.Parameters.AddWithValue("@PictureType", strImageType);

    lblMessage.Visible = true;

    try
    {
        objConn.Open();
        objCmd.ExecuteNonQuery();
        objConn.Close();
        lblMessage.Text = "ImageAdded!";
    }
    catch
    {
        lblMessage.Text = "Error occured the image has not been added to the database!";
    }

}

public void AddImage(object sender, EventArgs e) { int intImageSize; String strImageType; Stream ImageStream; FileStream fs = File.OpenRead(Request.PhysicalApplicationPath + "/Images/default_image.png"); Byte[] ImageContent; if (PersonImage.PostedFile.ContentLength > 0) { intImageSize = PersonImage.PostedFile.ContentLength; strImageType = PersonImage.PostedFile.ContentType; ImageStream = PersonImage.PostedFile.InputStream; ImageContent = new Byte[intImageSize]; int intStatus; intStatus = ImageStream.Read(ImageContent, 0, intImageSize); } else { strImageType = "image/x-png"; ImageContent = new Byte[fs.Length]; fs.Read(ImageContent, 0, ImageContent.Length); } SqlConnection objConn = new SqlConnection(ConfigurationManager.AppSettings["conn"]); SqlCommand objCmd; string strCmd; strCmd = "INSERT INTO ImageTest (Picture, PictureType) VALUES (@Picture, @PictureType)"; objCmd = new SqlCommand(strCmd, objConn); SqlParameter prmPersonImage = new SqlParameter("@Picture", SqlDbType.Image); prmPersonImage.Value = ImageContent; objCmd.Parameters.Add(prmPersonImage); objCmd.Parameters.AddWithValue("@PictureType", strImageType); lblMessage.Visible = true; try { objConn.Open(); objCmd.ExecuteNonQuery(); objConn.Close(); lblMessage.Text = "ImageAdded!"; } catch { lblMessage.Text = "Error occured the image has not been added to the database!"; } }