请列出详细代码,谢谢

解决方案 »

  1.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506
      

  2.   

    在读取数据时用参数化的方法:
    try
    {
      SqlConnection conn=new SqlConnection("Server=.;database=image;uid=sa;pwd=;");
      conn.Open();
      Sqlcommand cmd=new Sqlcommand("insert into image values(@img)",conn);
      .
      .
      .
      .
      conn.Close();
    }
    catch(SqlException ex)
    {
     ......//连接错误提示
    }同时还要用到有关文件的知识:
    System.IO
    MemeryStream
    等等
      

  3.   

    /-------------存储图片--以 sqlserver 为例 -----------------------------
    using System.IO;   FileInfo fi = new FileInfo(this.strFileName);//图片文件名
    int imgdatalen=(int)fi.Length;
    imgdata = new byte[imgdatalen];
    Stream imgdatastream=fi.OpenRead(); 
    int n=imgdatastream.Read(imgdata,0,imgdatalen);//-- FPic 定义为 image类型
    string strInsert = "Insert MBInfor (FPic) values(@ImageData)";
    System.Data.SqlClient.SqlCommand cmdInsert = new System.Data.SqlClient.SqlCommand(strInsert,AccessDb.SQLConn);
    System.Data.SqlClient.SqlParameter paramImageData = new System.Data.SqlClient.SqlParameter("@ImageData",System.Data.SqlDbType.Image);
    paramImageData.Value = imgdata;      
    cmdInsert.Parameters.Add(paramImageData);
    cmdInsert.ExecuteNonQuery();
    //-----------------------读取图片-----------------------------------------
    FileStream myStream = new FileStream("temp",FileMode.Create);
    byte[] mydata = ((byte[])this.dsMbinfor.Tables["MBInfor"].Rows[0]["FPic"]);//从数据库中读取的byte数组
    foreach(byte a in mydata)
    {
     myStream.WriteByte(a); 
    }    
    Image myImage=Image.FromStream(myStream) ;
    myStream.Close();
      

  4.   

    示例
    1. 使用以下结构创建一个 SQL Server 或 Access 表:CREATE TABLE BLOBTest
    (
    BLOBID INT IDENTITY NOT NULL,
    BLOBData IMAGE NOT NULL
    )

     
    2. 打开 Visual Studio .NET,然后新建一个 Visual C# Windows 应用程序项目。 
    3. 从工具箱向默认的 Form1 添加一个 PictureBox 和两个 Button 控件。将 Button1 的 Text 属性设置为 File to Database,并将 Button2 的 Text 属性设置为 Database to PictureBox。 
    4. 在窗体的代码模块顶部插入 using 语句:using System.Data.SqlClient;
    using System.IO;
    using System.Drawing.Imaging;

     
    5. 将以下数据库连接字符串的声明添加到 public class Form1 :System.Windows.Forms.Form 类声明中,并根据需要调整连接字符串:    String strCn = "Data Source=localhost;integrated security=sspi;initial catalog=mydata";

     
    6. 将下面的代码插入 Button1 (File to Database) 的 Click 事件过程中。根据需要调整到一个可用示例图像文件的可用路径。此代码可将图像文件从磁盘(使用一个 FileStream 对象)读入 Byte 数组,然后使用一个参数化的 Command 对象将数据插入数据库。try
    {
    SqlConnection cn = new SqlConnection(strCn);
    SqlCommand cmd =  new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", cn);
    String strBLOBFilePath = @"C:\blue hills.jpg";//Modify this path as needed. //Read jpg into file stream, and from there into Byte array.
    FileStream fsBLOBFile =  new FileStream(strBLOBFilePath,FileMode.Open, FileAccess.Read);
    Byte[] bytBLOBData = new Byte[fsBLOBFile.Length];
    fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);
    fsBLOBFile.Close(); //Create parameter for insert command and add to SqlCommand object.
    SqlParameter prm = new  SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,
    0, 0, null, DataRowVersion.Current, bytBLOBData);
    cmd.Parameters.Add(prm); //Open connection, execute query, and close connection.
    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();
    }catch(Exception ex)
    {MessageBox.Show(ex.Message);}

     
    7. 将下面的代码插入 Button2 (Database to PictureBox) 的 Click 事件过程。此代码将行从数据库中的 BLOBTest 表检索到一个数据集,复制最新添加的图像到 Byte 数组,然后到 MemoryStream 对象,接着将 MemoryStream 加载到 PictureBox 控件的 Image 属性。try
    {
    SqlConnection cn = new SqlConnection(strCn);
    cn.Open(); //Retrieve BLOB from database into DataSet.
    SqlCommand cmd = new SqlCommand("SELECT BLOBID, BLOBData FROM BLOBTest ORDER BY BLOBID", cn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds, "BLOBTest");
    int c = ds.Tables["BLOBTest"].Rows.Count; if(c>0)
    {   //BLOB is read into Byte array, then used to construct MemoryStream,
    //then passed to PictureBox.
    Byte[] byteBLOBData =  new Byte[0];
    byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
    MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
    pictureBox1.Image= Image.FromStream(stmBLOBData);
    }
    cn.Close();
    }
    catch(Exception ex)
    {MessageBox.Show(ex.Message);}

     
    8. 按 F5 键编译并运行该项目。 
    9. 单击 File to Database 按钮将至少一个示例图像加载到数据库。 
    10. 单击 Database to PictureBox 按钮将保存的图像显示在 PictureBox 控件中。 
    11. 如果想能够直接将图像从 PictureBox 控件插入数据库,则请添加第三个 Button 控件,并将下面的代码插入其 Click 事件过程。此代码将图像数据从 PictureBox 控件检索到 MemoryStream 对象,将 MemoryStream 复制到一个 Byte 数组,然后使用一个参数化的 Command 对象将 Byte 数组保存到数据库。try
    {
    SqlConnection cn = new SqlConnection(strCn);
    SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", cn); //Save image from PictureBox into MemoryStream object.
    MemoryStream ms  = new MemoryStream();
    pictureBox1.Image.Save(ms, ImageFormat.Jpeg); //Read from MemoryStream into Byte array.
    Byte[] bytBLOBData = new Byte[ms.Length];
    ms.Position = 0;
    ms.Read(bytBLOBData, 0, Convert.ToInt32(ms.Length)); //Create parameter for insert statement that contains image.
    SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,
    0, 0,null, DataRowVersion.Current, bytBLOBData);
    cmd.Parameters.Add(prm);
    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();
    }catch(Exception  ex)
     {MessageBox.Show(ex.Message);}

     
    12. 运行该项目。单击 Database to PictureBox 按钮以显示刚才在 PictureBox 控件中保存过的图像。单击新添加的按钮将此图像从 PictureBox 保存到数据库。然后再次单击 Database to PictureBox 按钮以确认图像已正确保存。 
      

  5.   

    不知道版主解决了没。
    jetxia(Thinking&Asking&Studying)的差不多能解决啊。
    其实:image中可存放任何类型,程序也可扩展为:mp3,图片等一切数据啊