private void showpic()
{
//显示相片
string uid=textBox1.Text.Trim(); //查找"职工相片"表中是否有当前职工
int yn=0;
for(int i=0;i<dspic.Tables[0].Rows.Count;i++)
{
if (uid.Trim() == dspic.Tables[0].Rows[i]["职工编号"].ToString().Trim())
{
byte[] b = (byte[])dspic.Tables["职工相片"].Rows[i][1];
//MemoryStream s = new MemoryStream(b);
//pictureBox1.Image =  Image.FromStream(s);
//s.Close();
//上面代码与下面的功能相同,下面是用BMP格式相片效果要好些 //有相片则显示
if (b.ToString().Trim().Length>0)
{
MemoryStream s = new MemoryStream(b);
Bitmap bmp = new Bitmap(s);
System.Drawing.Image image = bmp;
pictureBox1.Image =  image;
s.Close();

yn=1;//有相片标标志
}
}
}
//没有找到相片,清空pictureBox1
if(yn != 1) pictureBox1.Image =null;

}
private void savepic()
{
//保存相片
MemoryStream s = new MemoryStream();
pictureBox1.Image.Save(s,System.Drawing.Imaging.ImageFormat.Bmp);//BMP格式相片效果要好些
byte[] b = s.ToArray();
s.Close();

//查找"职工相片"表中是否有当前职工
string uid=textBox1.Text.Trim();
string y="";
int x=0;
for(int i=0;i<dspic.Tables[0].Rows.Count;i++)
{
if (uid.Trim() == dspic.Tables[0].Rows[i]["职工编号"].ToString().Trim())
{
x=i;
y="y";
break;
}
}
if(y=="y")
{
dspic.Tables["职工相片"].Rows[x]["职工相片"]=b;
}
else
{
DataRow myrow =dspic.Tables["职工相片"].NewRow(); 
myrow["职工编号"]=uid;
myrow["职工相片"]=b;
dspic.Tables["职工相片"].Rows.Add(myrow);
}
//dspic.Tables["职工相片"].Rows[0]["职工相片"]=b;
dapic.Update(dspic,"职工相片");//到保存员工信息时一起保存
}

解决方案 »

  1.   

    插入:
    先将上传的图片存至本地fpath为路径
    //打开临时文件
    System.IO.FileStream fist= new FileStream(fpath,FileMode.Open);
    if(fist.CanRead)
    {

    //读取临时文件的类容
    byte[] bt = new byte[fist.Length];
    int dd = (int)fist.Length;
    fist.Read(bt,0,dd);
    fist.Close();
    //读取完数据关闭临时文件并删除
    if(File.Exists(fpath))
    {
    File.Delete(fpath);
    }
    sql = "insert into table(pic) values(:b_blob)";
    //加入参数
    System.Data.OracleClient.OracleParameter op = new OracleParameter();
    op.OracleType = OracleType.Blob;
    op.ParameterName = ":b_blob";
    op.Value = bt;
    comd.Parameters.Add(op); comd.CommandText = sql;
    comd.ExecuteNonQuery();
    }读取:
    strl_sql = "select pic from table"
    cmdl_i.CommandText = strl_sql;
    drl_i = cmdl_i.ExecuteReader();
    if(drl_i.Read())
    {
    System.IO.FileStream fsl_i = new FileStream("c\\1.jpg",FileMode.Create);
    if(fsl_i.CanWrite)
    {
    byte[] bt = (byte[])drl_i[0];
    fsl_i.Write(bt,0,bt.Length);
    fsl_i.Close();
    }
    }
    drl_i.Close();
    drl_i.Dispose();