數據庫實例
2009-03-24 13:39:41| 分类: 默认分类 |举报|字号 订阅
//圖片上傳
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "图像文件 (*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG"; //上傳格式
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fullpath = openFileDialog1.FileName;//文件路径
FileStream fs = new FileStream(fullpath, FileMode.Open);
Byte[] imagebytes = new byte[fs.Length]; //二進制轉換
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length)); //讀取二進制流
string cnnstr = "provider=OraOLEDB.Oracle;data source=testdb;User Id=hrerp;Password=hrerppwd;";
OleDbConnection conn = new OleDbConnection(cnnstr);
OleDbCommand cmd = new OleDbCommand(cnnstr, conn);
conn.Open();
string sql = "insert into hr_emp_attach(emp_no,emp_photo) values('000002',:photo)";
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Parameters.Add("photo", OleDbType.Binary,imagebytes.Length);
cmd.Parameters[0].Value = imagebytes;
cmd.ExecuteNonQuery();
MessageBox.Show("上传成功");
conn.Close();
displayPhoto();//顯示圖片
}
}
public void displayPhoto()
{
string strSQL = "select emp_photo from hr_emp_attach where emp_no='000002'";
string cnnstr = "provider=OraOLEDB.Oracle;data source=testdb;User Id=hrerp;Password=hrerppwd;";
OleDbConnection con = new OleDbConnection(cnnstr);
try
{
con.Open();
}
catch
{ }
OleDbCommand cmd = new OleDbCommand(strSQL, con);
System.Data.OleDb.OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr["emp_photo"] != DBNull.Value)//照片字段里有值才能进到方法体显示图片,否则清空pb
{
MemoryStream ms = new MemoryStream((byte[])dr["emp_photo"]);//把照片读到MemoryStream里
Image imageBlob = Image.FromStream(ms, true);//用流创建Image
pictureBox1.Image = imageBlob;//输出图片
//pictureBox1.Image.Save("d:\\abcc.jpg", pictureBox1.Image.RawFormat); //將圖片保存起來
}
else//照片字段里没值,清空pb
{
pictureBox1.Image = null;
}
}
}
winform保存二进制图片到oracle
最新推荐文章于 2021-04-13 03:23:34 发布