winform中 上传照片到mysql 数据库中。照片直接保存到数据库 photo字段中。

解决方案 »

  1.   

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            SqlConnection conn;
            private void button1_Click(object sender, EventArgs e)
            {
                string connecString;
                Stream imgStream;
                string filePath;
                string strSql;
                SqlCommand cmd;
                
                int intImgSize;
                connecString = "data Source=127.0.0.1;user id=sa;password=5566;initial catalog=xin;";
                conn = new SqlConnection(connecString);
                try
                {
                    openFile.ShowDialog();
                    imgStream = openFile.OpenFile();
                    strSql = @"insert into test(picture,discription) values(@picture,@disc) ";
                    cmd = new SqlCommand(strSql, conn);
                    conn.Open();
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    openFile.ShowDialog();
                    
                    imgStream = openFile.OpenFile();
                    intImgSize = (int)imgStream.Length;
                    byte[] imgContex = new byte[intImgSize];
                    imgStream.Read(imgContex, 0, intImgSize);
                    
                    cmd.Parameters.Add("@picture", SqlDbType.Binary);
                    cmd.Parameters["@picture"].Value = imgContex;
                    cmd.Parameters.Add("@disc", SqlDbType.VarChar);
                    cmd.Parameters["@disc"].Value = "new picture";
                    int intResult = cmd.ExecuteNonQuery();
                    if (intResult<0)
                    {
                        MessageBox.Show("更新失敗");
                    }   
               }
                catch (Exception ex)
                {
                    MessageBox.Show("Error:" + "\n" + ex.Message);
                }
                finally
                {
                    if(conn.State==ConnectionState.Open)
                    conn.Close();
                    conn.Dispose();
                }
            }
        }
      

  2.   

     openFile是一個openFileDialog控件
      

  3.   

     private void btnSave_Click(object sender, EventArgs e)
                {
                    if (imageFilePath.Trim().Length == 0)
                    {
                        MessageBox.Show("你没有选择图片","注意",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                        return;
                    }                try
                    {
                        using (SqlConnection conn = new SqlConnection(connString))
                        {
                            string comdText = "UPDATE dbo.章立民研究室 SET 相片 = @BLOBData WHERE 身份证号码 = @Id ";                        SqlCommand comd = new SqlCommand(comdText, conn);                        comd.Parameters.Add("@Id", SqlDbType.NVarChar, 18).Value = this.lstId.SelectedItem.ToString();                        //建立一个MemoryStream对象
                            MemoryStream ms = new MemoryStream();                        //将PictureBox中的影像以JPEG图形格式存入MemoryStream对象中
                            Bitmap myBitmap = new Bitmap(this.pirPoto.BackgroundImage);
                            myBitmap.Save(ms, ImageFormat.Jpeg);                        byte[] byteBLOBData = new byte[(int)ms.Length];                        ms.Position = 0;
                            ms.Read(byteBLOBData, 0, (int)ms.Length);
                            ms.Close();                        comd.Parameters.Add("@BLOBData", SqlDbType.VarBinary, byteBLOBData.Length).Value = byteBLOBData;                        conn.Open();
                            comd.ExecuteNonQuery();
                        }
                    }
                    catch
                    { 
                    }
                }