图片和视频并不是真的存储到数据库的,数据库中存放的是图片的指针.
访问时通过数据库获取指针,在通过指针访问到图片.参考
http://community.csdn.net/Expert/topic/3640/3640804.xml?temp=8.449733E-03

解决方案 »

  1.   

    NET是由微软开发的一种新型的分布式计算平台,ASP.NET是它针对Web开发的编程模式。本文的目的是在开发数据驱动的ASP.NET Web应用程序中获取一些好的经验。这个应用程序将告诉你怎么把一幅图片保存到数据库中以及怎样把图片从数据库中读取出来。它以ADO.NET作为数据访问机制,C#作为编程语言,SQL 2000 Server作为后台数据库。
    概述
    一般的,很大的图片文件往往被保存在Web服务器的文件夹中,而不是数据库中。在一些实例中,以银行系统为例,人们先把用户的签名做成图片文件,然后保存到数据库中。
    • 数据库模式 
    在这个示范中,微软的SQL 2000 Server被用作后台数据库。我使用了一种比较特殊的数据类型 image 。这 image 数据类型是被用来保存图片到数据库的。
    • 所使用的控件: 
    o System.Web.UI.HtmlControls.HtmlInputFile 
    o System.Web.UI.WebControls.TextBox 
    o System.Web.UI.WebControls.Button 
    • 所使用的名字空间: 
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Data;
    using System.IO;
    using System.Drawing.Imaging;
    编码
    使用 HtmlInputFile 类,它可以用 <input type="file" runat="server"/> 标签来声明一个实例。下面的例子是一个完整的 ASPX 文件,它让用户上传图片文件以及图片的说明。OnUpload 方法把图片以及说明写到iSense 数据库的Picture 表中。
    // 保存图片文件到数据库的源码
     
    public void OnUpload(Object sender, EventArgs e)
    {
    // 从输入文件中创建一个 byte[]
    int len = Upload.PostedFile.ContentLength;
        byte[] pic = new byte[len];
        Upload.PostedFile.InputStream.Read (pic, 0, len);
    // 插入图片和说明到数据库中
     SqlConnection connection = new 
         SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
        try
        {
            connection.Open ();
            SqlCommand cmd = new SqlCommand ("insert into Image " 
              + "(Picture, Comment) values (@pic, @text)", connection);
            cmd.Parameters.Add ("@pic", pic);
            cmd.Parameters.Add ("@text", Comment.Text);
            cmd.ExecuteNonQuery ();
        }
        finally 
        {
            connection.Close ();
        }
    }
    上面创建的函数可以通过使用按钮的 onClick 属性来调用。
    如何使用ADO.NET技术从数据库中读取图片并把它显示在Web页面上?
    这里,我使用Web页面来显示图片,而没有用其他任何控件。下面的代码是用来显示数据库中的图片。
    private void Page_Load(object sender, System.EventArgs e)
    {
        MemoryStream stream = new MemoryStream ();
        SqlConnection connection = new 
          SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
        try
        {
            connection.Open ();
            SqlCommand command = new 
              SqlCommand ("select Picture from Image", connection);
            byte[] image = (byte[]) command.ExecuteScalar ();   
            stream.Write (image, 0, image.Length);
            Bitmap bitmap = new Bitmap (stream);
            Response.ContentType = "image/gif";
            bitmap.Save (Response.OutputStream, ImageFormat.Gif);
        } 
        finally
        {
            connection.Close ();
            stream.Close ();
        }
    }
    GDI+函数为操作和定义图片提供了一个丰富的功能集合。本文的例子只能看到它的一小部分功能。你可以使用 System.Drawing 和 System.Drawing.Imaging 名字空间来调用这些功能。举例来说,你可以开发在Web上保存和管理图片文件的应用程序,或者你可以一个简单、易配置的应用程序使用户能够操作图片。
    如何运行程序? 
    首先,创建一个虚拟目录,把你的工程文件放到这虚拟目录中。然后改变服务器名称,数据库名称以及表的名称,如下所示:
    SqlConnection connection = new SqlConnection
            ("server=localhost;database=mypictures;uid=sa;pwd=");
    然后公布你的工程以获得最好的结果。
      

  2.   

    以下是一個ASP.NET(C#)+SQL Server2000的上偉圖片并怎么保存到數據庫
    根據樓主的具體情況而定
    public void OnUpload(Object sender, EventArgs e)
    {
    //從輸入文件中創建一個byte[]
    int len = Upfile.PostedFile.ContentLength;
    byte[] pic = new byte[len];
    Upfile.PostedFile.InputStream.Read(pic, 0, len);
    //插入圖片和說明到數據庫中
    SqlConnection connection = new SqlConnection ("server=192.168.42.32;database=hopewell;uid=sa;pwd=javabean");
    try
    {
    connection.Open ();
    SqlCommand cmd = new SqlCommand ("insert into Image " + "(pic) values (@pic)", connection);
    //cmd.Parameters.Add ("@pic", pic);
    cmd.Parameters.Add("@pic",pic);
    //cmd.Parameters.Add ("@text", TextBox1.Text);
    //cmd.ExecuteNonQuery ();
    cmd.ExecuteNonQuery();
    }
    finally 
    {
    connection.Close ();
    }
    }
      

  3.   

    再加上
    protected System.Web.UI.HtmlControls.HtmlInputFile Upfile;
      

  4.   

    1.把图片和视频的路径存储到数据库,然后读取路径,返回给服务页面
    (從數據庫取到路徑,然後在PageOnLoad上,設置一load頁面讀取 數據地址,並顯示2.把图片和视频本身存储到数据库,然后直接从数据库返回(----你看看我給你的例子哪裡有)
      

  5.   

    按照talantlee的方法可以把图片写到数据库,但是读不出来,不知道怎么显示在页面上
    请高人指点
      

  6.   

    存储和显示图片:
    请参考:http://dotnet.aspx.cc/ShowDetail.aspx?id=J9UBRVER-L3VB-49M3-GOU1-Z6C2PVR6FZ3K
      

  7.   

    C#.NET主要代码:
    publice string constr;//constr为数据库连接字符串
    private string sqlstr;//sql字符串
    private openFileDialog ofd;
    private byte[] b;
    private Bitmap image;
    private pictureBox pictureBox1;ofd=new openFileDialog();
    FileStream fs=new FileStream(ofd.FileName,FileMode.Open,FileAccess.Read);
    image=new Bitmap(fs);
    pictureBox1.Image=(Image)image;
    b=new byte[fs.Length];
    fs.Read(b,0,b.Length);
    try{
    SqlConnection conn=new SqlConnection(constr);
    conn.open();
    sqlstr=@"insert into table(photo) values(@photo)";
    SqlCommand comm=new SqlCommand(conn,sqlstr);
    comm.Parameters.Add("@photo",SqlDbType.Image,"photo");
    comm.Parameters["@photo"].Value=b
    }
    catch(Exception)
    {
    comm.ExecteNonQuery();
    }
    conn.Close();
      

  8.   

    參考
    http://search.csdn.net/Expert/topic/2520/2520344.xml?temp=.9333307
      

  9.   

    http://blog.csdn.net/zjcxc/archive/2003/12/29/20077.aspx
    http://blog.csdn.net/zjcxc/archive/2003/12/29/20079.aspx
      

  10.   

    今天看了一下,上面的有几处是错的,修改如下:(c#.net)
    publice string constr;//constr为数据库连接字符串
    private string sqlstr;//sql字符串
    private openFileDialog openFileDialog1;
    private byte[] b;
    private pictureBox pictureBox1;openFileDialog1=new openFileDialog();
    pictureBox1.Image=Image.FileFrom(openFileDialog1.FileName);FileStream fs=new FileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read);
    b=new byte[fs.Length];
    fs.Read(b,0,b.Length);
    SqlConnection conn=new SqlConnection(constr);
    conn.open();sqlstr=@"insert into table(photo) values(@photo)";
    SqlCommand comm=new SqlCommand(sqlstr,conn);
    comm.Parameters.Add("@photo",SqlDbType.Image,"photo");
    comm.Parameters["@photo"].Value=b
    try
    {
    comm.ExecteNonQuery();
    }
    catch(Exception err)
    {
    MessageBox.Show(err.Message)
    }
    conn.Close();