C# 將圖片保存到數據庫


static void Main(string[] args)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=192.168.30.7;database=AIS20170401213620;uid=sa;pwd=ABCabc123";
            //con.ConnectionString = "server=192.168.30.7;database=AIS20170720100743;uid=sa;pwd=ABCabc123";
            con.Open();
            //創建文件流,path參數是文件路徑
            FileStream file = new FileStream(@"C:\Users\VULCAN\Desktop\火影桌面壁紙\Cg-4V1RSC-qIb5v4AAwP30B8ANkAAQsPAAtspsADA_3018.jpg", FileMode.Open);
            int streamLength = (int)file.Length;
            byte[] image = new byte[streamLength];//聲明字節數組,保存圖片文件
            file.Read(image, 0, streamLength); //把圖片文件轉換成字節數組保存
            file.Close();

            string sql = string.Format(" update t_BOS200000075  set fpicture=@image where fid='1000' ", image);
            //string sql = string.Format("update ");
            SqlCommand com = new SqlCommand(sql, con);
            com.Parameters.Add(new SqlParameter("image", SqlDbType.Binary, image.Length, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, image));

            SqlDataReader dr = com.ExecuteReader();
            dr.Close();
            con.Close();

        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM