• Winform下如何上传图片并显示出来。同时保存到数据库


    通常,我们在开发软件或者网站是否,通常有时候需要添加图片,我们怎么做呢,直接贴例子。

    前提是添加openFileDialog控件哈

      #region 定义公共的类对象及变量
            SqlConnection sqlcon;       //声明数据库连接对象
            SqlDataAdapter sqlda;        //声明数据桥接器对象
            DataSet myds;                     //声明数据集对象
            //定义数据库连接字符串
            //string strCon = @"Data Source=lll;Database=db_CSharp;uid=sa;pwd=;";
            string strCon = "Data Source=ASUS-PC;Initial Catalog=BookManager;Integrated Security=True";
            #endregion     

     private void AddPhoto_Load(object sender, EventArgs e) //初始化
            {
                ShowInfo();//显示用户信息
            }

    【1】选择图片按钮

         private void button1_Click(object sender, EventArgs e)//选择图片
            {
                //定义可选择的头像类型
                openFileDialog1.Filter = "*.jpg,*jpeg,*.bmp,*.ico,*.png,*.tif,*.wmf|*.jpg;*jpeg;*.bmp;*.ico;*.png;*.tif;*.wmf";
                openFileDialog1.Title = "选择用户头像";
                //判断是否选择了头像
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    //显示选择的用户头像
                    pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
                    textBox1.Text = "";
                }
            }

    【2】保存图片按钮

     private void button2_Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Trim() == "")
                {
                    MessageBox.Show("用户名不为空","信息提示");
                }
                if (openFileDialog1.FileName == "")
                {
                    MessageBox.Show("用户头像不为空", "信息提示");
                }

                sqlcon = new SqlConnection(strCon);//创建链接对象
                sqlcon.Open();//打开数据库
                string sqlstr = "select * from userphoto where name='"+textBox1.Text.Trim()+"'";
                SqlCommand Mycom = new SqlCommand(sqlstr,sqlcon);
                Mycom.ExecuteNonQuery();
                if (null != Mycom.ExecuteScalar())
                {
                    MessageBox.Show("用户名 "+textBox1.Text.Trim() + " 已经存在,请重新注册用户名", "信息提示");
                    textBox1.Text = "";
                    textBox1.Focus();
                }
                //sqlcon.Close();//关闭数据库
                /***************************************************************/
               else if (openFileDialog1.FileName != "" && textBox1.Text != "")//如果用户名不为空,并且文件被选中
                {
                    //添加用户信息
                    if (AddInfo(textBox1.Text, openFileDialog1.FileName))
                    {
                        MessageBox.Show("用户信息添加成功", "信息提示");
                    }
                   
                }
                //else
                //{ 
                //    MessageBox.Show("请您输入用户名", "信息提示"); 
                //}
                sqlcon.Close();//关闭数据库
                ShowInfo();
            }

    【3】调用的函数:

    #region 添加用户信息
            /// <summary>
            /// 添加用户信息
            /// </summary>
            /// <param name="strName">用户名称</param>
            /// <param name="strImage">选择的头像名称</param>
            /// <returns>执行成功,返回true</returns>
            private bool AddInfo(string strName, string strImage)
            {
                sqlcon = new SqlConnection(strCon);//创建数据库连接对象


                FileStream FStream = new FileStream(//创建文件流对象
                    strImage, FileMode.Open, FileAccess.Read);
                BinaryReader BReader = new BinaryReader(FStream);//创建二进制流对象
                byte[] byteImage = BReader.ReadBytes((int)FStream.Length);//得到字节数组


                SqlCommand sqlcmd = new SqlCommand(//创建命令对象
                    "insert into userphoto(name,photo) values(@name,@photo)", sqlcon);

                sqlcmd.Parameters.Add("@name", //添加参数并赋值
                    SqlDbType.VarChar, 50).Value = strName;
                sqlcmd.Parameters.Add("@photo",//添加参数并赋值
                    SqlDbType.Image).Value = byteImage;

                sqlcon.Open();//打开数据库连接
                sqlcmd.ExecuteNonQuery();//执行SQL语句
                sqlcon.Close();//关闭数据库连接
                return true;//方法返回布尔值
            }
            #endregion

    *******************************************************************

     #region 在DataGridView中显示用户名称
            /// <summary>
            /// 在DataGridView中显示用户名称
            /// </summary>
            private void ShowInfo()//显示到DataGridView表格
            {
                sqlcon = new SqlConnection(strCon);//创建链接对象
                sqlda = new SqlDataAdapter("select name as 用户名称 from userphoto", sqlcon);//查询SQL
                myds = new DataSet();//填充数据集
                sqlda.Fill(myds);
                dataGridView1.DataSource = myds.Tables[0];//显示
            }
            #endregion

    ********************************************************

     #region 显示用户信息
            /// <summary>
            /// 显示用户头像
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                //记录选择的用户名
                string strName = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString().Trim();
                if (strName != "")
                {
                    sqlcon = new SqlConnection(strCon);     //实例化数据库连接对象
                    //实例化数据桥接器对象
                    sqlda = new SqlDataAdapter("select * from userphoto where name='" + strName + "'", sqlcon);
                    myds = new DataSet();                   //实例化数据集对象
                    sqlda.Fill(myds);                       //填充数据集
                    //显示用户名称
                    textBox1.Text = myds.Tables[0].Rows[0][1].ToString();
                    //使用数据库中存储的二进制头像实例化内存数据流
                    MemoryStream MStream = new MemoryStream((byte[])myds.Tables[0].Rows[0][2]);
                    pictureBox1.Image = Image.FromStream(MStream);  //显示用户头像
                }
            }
            #endregion

    文章出处:https://blog.csdn.net/paullink520/article/details/19563881

  • 相关阅读:
    安装python包的两种方法
    Oracle 查询当前用户下的所有表
    Oracle SQL存储过程结构、异常处理示例
    Oracle IF-ELSE条件判断结构
    Oracle 组函数count()
    从svn下载项目后出现 Error:java: Compilation failed: internaljava compiler error 解决办法
    当学习失去方向,开始荒废时光时
    给自己一个目标
    汇编环境搭建在Linux下
    汇编学习总结
  • 原文地址:https://www.cnblogs.com/net-sky/p/9338951.html
Copyright © 2020-2023  润新知