在论坛中找了很多类似问题,都没有符合要求的,本人实在是没有办法,特发贴求助!
保存源码:
DataSet CertList=new DataSet();
string sql=""; sql="select Person_pic from ent_info where 1=2";
Public Pub=new Public();
OracleDataAdapter CertDA=new OracleDataAdapter(sql,Pub.OracleConnection);
OracleCommandBuilder CertCMD=new OracleCommandBuilder(CertDA); CertDA.Fill(CertList); Bitmap MyImage=null;
MyImage=(Bitmap)PerPic.Image;
if (MyImage!=null)
{
System.IO.MemoryStream stream=new System.IO.MemoryStream();
                
MyImage.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg); Byte[] buffer=new Byte[stream.Length];
stream.Read(buffer,0,(int)stream.Length);
myRow["Person_pic"]=buffer;
} CertList.Tables[0].Rows.Add(myRow);
CertDA.Update(CertList); 
其中Person_pic为BLOB类型的字段
读取源码:
string Sql="";
Sql="select Person_pic from ent_info where card_no='"+CardNo+"'";
OracleCommand selectCMD = new OracleCommand(Sql, OracleConnection);
if (OracleConnection.State!=System.Data.ConnectionState.Open)
{
OracleConnection.Open();
}
try
{
HandByte= (Byte[])selectCMD.ExecuteScalar();
if (HandByte!=null)
{
MemoryStream stmBLOBData = new MemoryStream(HandByte);
i1=Image.FromStream(stmBLOBData);
stmBLOBData.Close();
PerPic.Image = i1 ;
}

}
catch(System.Exception e)
{
MessageBox.Show(e.Message);
HandByte=null;
}
finally
{
} 其中PerPic是Infragistics.Win.UltraWinEditors.UltraPictureBox这个控件

解决方案 »

  1.   

    程序可以读取由DELPHI存储的图片,在C#中用上面的代码存储后再读取出来时报“使用了无效参数”的错误。出错语句:i1=Image.FromStream(stmBLOBData);
      

  2.   

    可以把图片读到byte[] 流里, 再把流存到数据库中,
      

  3.   

    可以将二进制大对象 (BLOB) 作为二进制或字符数据写入数据库,具体视数据源的字段类型而定。若要将 BLOB 值写入数据库,请发出相应的 INSERT 或 UPDATE 语句并将 BLOB 值作为输入参数传递(请参见将存储过程用于命令)。如果 BLOB 存储为文本格式(如 SQL Server text 字段),则可将 BLOB 作为字符串参数传递。如果 BLOB 存储为二进制格式(如 SQL Server image 字段),则可将类型 byte 的数组作为二进制参数传递。注意   BLOB 可能会相当大,因此作为单个值写入时可能会占用大量的系统内存,从而导致应用程序性能下降。若要减少写入 BLOB 值时所使用的内存量,可以按“块”将 BLOB 写入数据库。用该方法将 BLOB 写入数据库的过程具体取决于数据源的功能。有关按“块”将 BLOB 值写入 SQL Server 的示例,请参见将 BLOB 值写入 SQL Server 时保留资源。
    以下代码示例将员工信息添加到 Northwind 数据库中的 Employees 列表中。员工照片将从文件中读取并添加到表中的 Photo 字段,该字段为 image 字段。using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;public class EmployeeData
    {
      public static void Main()
      {
        DateTime hireDate = DateTime.Parse("5/21/99");
        AddEmployee("Jones", "Mary", "Sales Representative", hireDate, 5, "jones.bmp");
      }  public static void AddEmployee(string lastName, string firstName, string title, DateTime hireDate , int reportsTo, string photoFilePath)
      {
        byte[] photo = GetPhoto(photoFilePath);    SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");    SqlCommand addEmp = new SqlCommand("INSERT INTO Employees (LastName, FirstName, Title, HireDate, ReportsTo, Photo) " +
                                           "Values(@LastName, @FirstName, @Title, @HireDate, @ReportsTo, @Photo)", nwindConn);     addEmp.Parameters.Add("@LastName",  SqlDbType.NVarChar, 20).Value = lastName;
        addEmp.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10).Value = firstName;
        addEmp.Parameters.Add("@Title",     SqlDbType.NVarChar, 30).Value = title;
        addEmp.Parameters.Add("@HireDate",  SqlDbType.DateTime).Value     = hireDate;
        addEmp.Parameters.Add("@ReportsTo", SqlDbType.Int).Value          = reportsTo;    addEmp.Parameters.Add("@Photo",     SqlDbType.Image, photo.Length).Value = photo;    nwindConn.Open();    addEmp.ExecuteNonQuery();    nwindConn.Close();
      }  public static byte[] GetPhoto(string filePath)
      {
        FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);    byte[] photo = br.ReadBytes((int)fs.Length);    br.Close();
        fs.Close();    return photo;
      }
    }
      

  4.   

    先将文件转换成字节数组
    //将文件转化成字节数组,保存到数据库
    private byte[] GetFileBytes(string Filename)
    {
    if(Filename=="") return null;
    FileStream fileStream = new FileStream(Filename, FileMode.Open, FileAccess.Read);
    BinaryReader binaryReader = new BinaryReader(fileStream);

    byte[] fileBytes = binaryReader.ReadBytes((int)fileStream.Length);
    binaryReader.Close();
    fileStream.Close();
    return fileBytes;
    }
    再保存到数据库的OracleType.Blob
      

  5.   

    to:panda2fw2(我爱Monkey) 谢谢你。再就是很讨厌那种不看题目就瞎贴代码的人。