如何在Sql数据库中上传图像 [英] How to Upload Image In Sql Database

查看:122
本文介绍了如何在Sql数据库中上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好小组成员,
我曾经将用户的个人资料图像保存在一个文件夹中并保存用户信息,但是现在我需要将图像保存在数据库中.谁能告诉我如何在c#.net

Hello Group Members,
I use to save the profile image of user in a folder and save user information,But now I need to save image in database.Can any one tell me how to save image in database in c#.net

推荐答案

看看以下链接,这些链接可能会对您有所帮助,
将图片上传/下载到SQL Server/从SQL Server下载图片 [将图像保存到SQL Server 2000数据库 [ http://www.shabdar.org/sql-server/105-store-save-images-in-sql-server.html [
have a look at tyhe following links , these might help you,
Uploading / Downloading Pictures to / from a SQL Server[^]
Save An Image Into SQL Server 2000 Database[^]
http://www.shabdar.org/sql-server/105-store-save-images-in-sql-server.html[^]


图像就像其他文件一样被终止:在页面上放置一个上传控件,并处理Upload Click事件.调用以下例程:

Images are terated just like any other file: put an upload control on your page, and handle the Upload Click event. Call the following routine:

/// <summary>
/// Save an upload into the database.
/// </summary>
/// <param name="fl">Control containing the download.</param>
/// <returns>Status as a string</returns>
private string SaveUpload(FileUpload fl)
    {
    if (fl.HasFile)
        {
        try
            {
            int version = 0;
            string filename = Path.GetFileName(fl.FileName);
            byte[] filedata = fl.FileBytes;
            string strCon = ConnectionStrings.Download;
            using (SqlConnection con = new SqlConnection(strCon))
                {
                con.Open();
                // Check version - if the file exists in the DB already, then increase version number.
                using (SqlCommand ver = new SqlCommand("SELECT MAX(version) FROM dlContent WHERE fileName=@FN", con))
                    {
                    ver.Parameters.AddWithValue("@FN", filename);
                    object o = ver.ExecuteScalar();
                    if (o != null && o != System.DBNull.Value)
                        {
                        // Exists already.
                        version = (int) o + 1;
                        }
                    }
                // Stick file into database.
                using (SqlCommand ins = new SqlCommand("INSERT INTO dlContent (iD, fileName, description, dataContent, version, uploadedOn) " +
                                                       "VALUES (@ID, @FN, @DS, @DT, @VS, @UD)", con))
                    {
                    ins.Parameters.AddWithValue("@ID", Guid.NewGuid());
                    ins.Parameters.AddWithValue("@FN", filename);
                    ins.Parameters.AddWithValue("@DS", "");
                    ins.Parameters.AddWithValue("@DT", filedata);
                    ins.Parameters.AddWithValue("@VS", version);
                    ins.Parameters.AddWithValue("@UD", DateTime.Now);
                    ins.ExecuteNonQuery();
                    }
                }
            return string.Format("{0} uploaded, version = {1}", filename, version);
            }
        catch (Exception ex)
            {
            return "The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    return "Please select a file.";
    }

这可能比您需要的要多,但是我直接从代码中取消了.

如果您需要了解如何显示个人资料图片,请参见此处:

This probably does more than you need, but I lifted direct from my code.

If you need to know how to display the profile pic, see here: A generic Image-From-DB class for ASP.NET[^]


要将图像上传到数据库:
C#代码:

To Upload the image to database :
C# CODE:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;

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

        
        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string path = "~\\images\\" + FileUpload1.FileName;
        FileUpload1.SaveAs(Server.MapPath(path));
        
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=arun;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand();
        string sql = string.Format("insert into Products values({0},'{1}','{2}','{3}','{4}')",Txtcode.Text,Txtname.Text,Txtcost.Text,Txtdescr.Text,path);
        cmd.Connection = con;
        cmd.CommandText = sql;

        cmd.ExecuteNonQuery();
        con.Close();



ASPX页面代码:

<![CDATA [<%@页面语言="C#" AutoEventWireup ="true" CodeFile ="Default.aspx.cs" Inherits ="_ Default"%>



ASPX PAGE CODE:

<![CDATA[<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


这篇关于如何在Sql数据库中上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆