怎样将图片存储到SQL数据库中,帮我写下 详细代码

解决方案 »

  1.   

    //保存图片到数据库
    SqlConnection mycnn=new SqlConnection(@"server=zhurongj;database=my1;Trusted_connection=yes");
    mycnn.Open();
     SqlCommand mycmd=new SqlCommand("update picture set picture=@a where ID=1",mycnn);

    FileStream mystream=new FileStream("f:\\1.jpg",FileMode.Open,FileAccess.Read);
    long len=mystream.Length;

    mycmd.Parameters.Add("@a",SqlDbType.Image,(int)len,"picture");
    mycmd.Parameters["@a"].Direction=System.Data.ParameterDirection.Input;
               
    byte []box=new byte[len]; 
    mystream.Read(box,0,(int)len); mycmd.Parameters["@a"].Value=box;
      

  2.   

    I have an example to access picture into access database.first OleDBConnection should be configed correctly
    (1)store
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = conn;
                FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                byte[] content = new byte[fs.Length];
                fs.Read(content, 0, content.Length);
                fs.Close(); 
                
                cmd.CommandText = "insert into ladder (ladderpic) values(@img)";
                cmd.Parameters.Add("@img",content);
                if (cmd.ExecuteNonQuery() == 1)
                    MessageBox.Show("insert into database success.");
                else
                    MessageBox.Show("insert into database failure");            fs.Close();
                fs.Dispose();
              //  br.Close();
                cmd.Dispose();(2) get OleDbCommand cmd = new OleDbCommand("select top 1 ladderpic from 
    ladder order by nindex desc", conn);
                OleDbDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    reader.Read(); //               BinaryReader br = new BinaryReader(reader["ladderpic"].ToString());
                    MemoryStream ms = new MemoryStream((byte[])reader["ladderpic"]);
                    Image image = Image.FromStream(ms, true);
                    pictureBox1.Image = image;
                                    reader.Close();
      

  3.   

    保存图片到SQL 2000 Server数据库 
     
    在ASP.NET的Web页面中怎样上传文件?怎样使用ADO.NET技术从数据库中读取一幅图片,并在Web页面上显示?  
    摘要 
    .NET是由微软开发的一种新型的分布式计算平台,ASP.NET是它针对Web开发的编程模式。本文的目的是在开发数据驱动的ASP.NET Web应用程序中获取一些好的经验。这个应用程序将告诉你怎么把一幅图片保存到数据库中以及怎样把图片从数据库中读取出来。它以ADO.NET作为数据访问机制,C#作为编程语言,SQL 2000 Server作为后台数据库。 概述 
    一般的,很大的图片文件往往被保存在Web服务器的文件夹中,而不是数据库中。在一些实例中,以银行系统为例,人们先把用户的签名做成图片文件,然后保存到数据库中。 数据库模式 在这个示范中,微软的SQL 2000 Server被用作后台数据库。我使用了一种比较特殊的数据类型 image 。这 image 数据类型是被用来保存图片到数据库的。 所使用的控件: System.Web.UI.HtmlControls.HtmlInputFile System.Web.UI.WebControls.TextBox 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="); 
    然后公布你的工程以获得最好的结果。 
      

  4.   

    这类代码太多了 
    建议 lz先搜索,后发帖。</FONT></FONT></FONT><FONT color=#ff0000 size=4></FONT><FONT size=4>功能: 
    1。把图片文件(JPG GIF PNG)上传, 
    2。保存到指定的路径(在web.config中设置路径,以文件的原有格式保存), 
    3。并自动生成指定宽度的(在web.config中设置宽度) 
    4。和指定格式的(在web.config中指定缩略图的格式) 
    5。和原图比例相同的缩略图(根据宽度和原图的宽和高计算所略图的高度) 
    6。可以判断是否已经存在文件 
    7。如果不覆盖,则给出错误 
    8。如果选中"覆盖原图"checkbox,则覆盖原图。 
    9。可以根据要求,在webform上设置1个以上的file input和相应的checkbox 
    10。并在文件上传完毕后,显示原图的文件名,尺寸,字节,和 
    11。缩略图的文件名尺寸。 
    12。缩略图的文件名格式:原图+"_thumb."+指定格式,如:test.jpg_thumb.gif,以便于管理。 
    public void UploadFile(object sender, System.EventArgs e) 

    string imgNameOnly, imgNameNoExt, imgExt; 
    string imgThumbnail; 
    int erroNumber = 0; 
    System.Drawing.Image oriImg, newImg; 
    string strFePicSavePath = ConfigurationSettings.AppSettings["FePicSavePath"].ToString(); 
    string strFePicThumbFormat = ConfigurationSettings.AppSettings["FePicThumbFormat"].ToString().ToLower(); 
    int intFeThumbWidth = Int32.Parse(ConfigurationSettings.AppSettings["FePicThumbWidth"]); 
    string fileExt; 
    StringBuilder picInfo = new StringBuilder(); 
    if(Page.IsValid) 
    { for(int i = 0;i < Request.Files.Count; i++) 

    HttpPostedFile PostedFile = Request.Files[i]; 
    fileExt = (System.IO.Path.GetExtension(PostedFile.FileName)).ToString().ToLower(); 
    imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName); 
    if(fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png") 

    if(System.IO.File.Exists(strFePicSavePath + imgNameOnly) && (checkboxlistRewrite.Items[i].Selected == false)) 

    erroNumber = erroNumber + 1; 
    picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 已经存在,请修改文件名<br/>" ); 


    else 

    erroNumber = erroNumber + 1; 
    picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 扩展名 " + fileExt + " 不被许可<br/>" ); 


    if(erroNumber > 0) 

    picInfo.Append("<font color=red>全部操作均未完成,请修改错误,再进行操作</font><br/>"); 

    else 

    for(int i = 0;i < Request.Files.Count; i++) 

    HttpPostedFile PostedFile = Request.Files[i]; 
    imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName); 
    imgNameNoExt = System.IO.Path.GetFileNameWithoutExtension(PostedFile.FileName); 
    imgExt = System.IO.Path.GetExtension(PostedFile.FileName).ToString().ToLower(); oriImg = System.Drawing.Image.FromStream(PostedFile.InputStream); 
    newImg = oriImg.GetThumbnailImage(intFeThumbWidth, intFeThumbWidth * oriImg.Height/oriImg.Width,null,new System.IntPtr(0)); 
    switch(imgExt) 

    //case ".jpeg": 
    case ".jpg": 
    oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Jpeg); 
    break; 
    case ".gif": 
    oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Gif); 
    break; 
    case ".png": 
    oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Png); 
    break; 

    //oriImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameNoExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 
    switch(strFePicThumbFormat) 

    //jpeg format can get the smallest file size, and the png is the largest size 
    //case "jpeg": 
    case "jpg": 
    newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg); 
    imgThumbnail = imgNameOnly + "_thumb.jpg"; 
    break; 
    case "gif": 
    newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.gif",System.Drawing.Imaging.ImageFormat.Gif); 
    imgThumbnail = imgNameOnly + "_thumb.gif"; 
    break; 
    case "png": 
    newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.png",System.Drawing.Imaging.ImageFormat.Png); 
    imgThumbnail = imgNameOnly + "_thumb.png"; 
    break; 
    default: 
    newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg); 
    imgThumbnail = imgNameOnly + "_thumb.jpg"; 
    break; 
    }//switch 
    picInfo.Append("<b>文件 名:</b>" + imgNameOnly + " ( " + oriImg.Width + " x " + oriImg.Height + " ) " + PostedFile.ContentLength/1024 + "KB<br/>"); 
    picInfo.Append("<b>缩略图名:</b>" + imgThumbnail + " ( " + newImg.Width + " x " + newImg.Height + " )<br/><br/>"); 
    oriImg.Dispose(); 
    newImg.Dispose(); 
    }//for 
    picInfo.Append("<font color=red>所有操作成功</font><br/>"); 
    }// if erronumber = 0 
     

    else 

    picInfo.Append("<font color=red>有错误,请检查。操作未成功</font><br/>"); 

      

  5.   

    将图片先转成byte[]类型,存入即可