zoukankan      html  css  js  c++  java
  • 利用OpenFileDialog 获取图片存储到数据库中

    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;
                    }
                }

  • 相关阅读:
    对于捐赠承诺和劳务捐赠,不予以确认,但应在会计报表附注中披露
    R语言代写线性混合效应模型Linear Mixed-Effects Models的部分折叠Gibbs采样
    matlab代写MCMC贝叶斯方法用于加筋复合板的冲击载荷识别
    R语言代写dplyr-高效的数据变换与整理工具
    GIS代写遥感数据可视化评估:印度河流域上部的积雪面积变化
    R语言代写向量自回归模型(VAR)及其实现
    r语言代写实现似然的I(2)协整VAR模型弱外生性推理
    python代写缺失值处理案例分析:泰坦尼克数据
    Python代写高性能计算库——Numba
    matlab递归神经网络RNN实现:桨距控制控制风力发电机组研究
  • 原文地址:https://www.cnblogs.com/qinweizhi/p/5646207.html
Copyright © 2011-2022 走看看