利用OpenFileDialog 获取图片存储到数据库中
2016-07-06 11:03
363 查看
private void button1_Click(object sender, EventArgs e)
{
string fName;
OpenFileDialog openFileDialog = new OpenFileDialog();//实例化
openFileDialog.InitialDirectory = "e:\\141\\";//打开的默认路径
openFileDialog.Filter = "图像文件 (*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
fName = openFileDialog.FileName;
//textBox1.Text = File.ReadAllText(fName);
FileStream fs=new FileStream (fName ,FileMode.Open );
byte [] imgbt=new byte [fs .Length ];
BinaryReader br = new BinaryReader(fs);
imgbt = br.ReadBytes(Convert.ToInt32(fs.Length));
string cnnstr = "server=.;User ID=sa;Password=admin;Database=student";
SqlConnection conn = new SqlConnection(cnnstr);
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
string sql = "insert into a values('01',@image)";
comm.CommandType = CommandType.Text;
comm.CommandText = sql;
comm.Parameters.Add("image", SqlDbType .Image , imgbt.Length);
comm.Parameters[0].Value = imgbt;
comm.ExecuteNonQuery();
conn.Close();
}
再读取出来
SqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
{
if (dr["imagetest"] != DBNull.Value)
{
MemoryStream ms = new MemoryStream((byte[])dr["imagetest"]);//把照片读到MemoryStream里
Image imageBlob = Image.FromStream(ms, true);//用流创建Image
pictureBox1.Image = imageBlob;//输出图片
}
else//照片字段里没值,清空pb
{
pictureBox1.Image = null;
}
}
{
string fName;
OpenFileDialog openFileDialog = new OpenFileDialog();//实例化
openFileDialog.InitialDirectory = "e:\\141\\";//打开的默认路径
openFileDialog.Filter = "图像文件 (*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
fName = openFileDialog.FileName;
//textBox1.Text = File.ReadAllText(fName);
FileStream fs=new FileStream (fName ,FileMode.Open );
byte [] imgbt=new byte [fs .Length ];
BinaryReader br = new BinaryReader(fs);
imgbt = br.ReadBytes(Convert.ToInt32(fs.Length));
string cnnstr = "server=.;User ID=sa;Password=admin;Database=student";
SqlConnection conn = new SqlConnection(cnnstr);
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
string sql = "insert into a values('01',@image)";
comm.CommandType = CommandType.Text;
comm.CommandText = sql;
comm.Parameters.Add("image", SqlDbType .Image , imgbt.Length);
comm.Parameters[0].Value = imgbt;
comm.ExecuteNonQuery();
conn.Close();
}
再读取出来
SqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
{
if (dr["imagetest"] != DBNull.Value)
{
MemoryStream ms = new MemoryStream((byte[])dr["imagetest"]);//把照片读到MemoryStream里
Image imageBlob = Image.FromStream(ms, true);//用流创建Image
pictureBox1.Image = imageBlob;//输出图片
}
else//照片字段里没值,清空pb
{
pictureBox1.Image = null;
}
}
相关文章推荐
- JAVA链接ORACLE数据工具类
- 燃情7月,ACMUG厦门、上海双城技术沙龙及MySQL/MariaDB创始人Monty中国行活动
- Mysql(三)
- 数据库动态加载省市区信息,实现三级联动
- sql linq lamda
- SQL Prompt5 破解版+使用说明 [转]
- MySQL体系结构详解——组件
- 优化系列 | DELETE子查询改写优化
- Redis Service
- SQLSERVER 中GO的作用详解
- Oracle EBS新建Java 并发程序(Java Concurrent Program)
- mysql5.7半自动同步设置
- Oracle外键级联删除和级联更新
- ComboPooledDataSource数据库连接池
- redis启用持久化
- 1.1 mysql安装
- MYSQL技巧
- oracle统计(当前层级以及所有下级组织机构登录次数总和)
- Oracle:ORA-01810:格式代码出现两次 日期格式、时间格式、to_date错误
- 使用jdbc修改并显示数据库中的数据