日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

C# 圖片存入SQL Server數據庫

系統 3409 0
             OpenFileDialog openfiledialog1 = 
      
        new
      
      
         OpenFileDialog();

            
      
      
        if
      
       (openfiledialog1.ShowDialog() ==
      
         DialogResult.OK)

            {

                textBox1.Text 
      
      = openfiledialog1.FileName;
      
        //
      
      
        文件路徑
      
      
                    }



            
      
      
        string
      
       imageName = DateTime.Now.ToString(
      
        "
      
      
        yyyymmddhhMMss
      
      
        "
      
      );
      
        //
      
      
        生成文件名到數據庫
      
      
        

            

            FileStream fs 
      
      = 
      
        new
      
      
         FileStream(textBox1.Text, FileMode.Open);

            BinaryReader br 
      
      = 
      
        new
      
      
         BinaryReader(fs);

            Byte[] byData 
      
      = br.ReadBytes((
      
        int
      
      )fs.Length);
      
        //
      
      
        生成二進制流
      
      
                    fs.Close();

            
      
      
        //
      
      
        將圖片轉化為二進制流,存入數據庫
      
      
        string
      
       conn = 
      
        "
      
      
        server=.;database=ImageDB;Uid=sa;Pwd=1234 
      
      
        "
      
      
        ;

            SqlConnection myconn 
      
      = 
      
        new
      
      
         SqlConnection(conn);

            myconn.Open();

            
      
      
        string
      
       str = 
      
        "
      
      
        insert into ImageFile (ImageName,ImageContext) values(@name,@file)
      
      
        "
      
      
        ;

            SqlCommand mycomm 
      
      = 
      
        new
      
      
         SqlCommand(str, myconn);

            mycomm.Parameters.Add(
      
      
        "
      
      
        @file
      
      
        "
      
      
        , SqlDbType.Binary, byData.Length);

            mycomm.Parameters[
      
      
        "
      
      
        @file
      
      
        "
      
      ].Value =
      
         byData;



            mycomm.Parameters.Add(
      
      
        "
      
      
        @name
      
      
        "
      
      , SqlDbType.NVarChar,
      
        50
      
      
        );

            mycomm.Parameters[
      
      
        "
      
      
        @name
      
      
        "
      
      ].Value =
      
         imageName;

            mycomm.ExecuteNonQuery();
      
      
        //
      
      
        將二進制流寫入數據庫,對應數據庫IMAGE類型
      
      
                    myconn.Close();



            
      
      
        //
      
      
        將二進制流轉化為IMAGE
      
      

            str = 
      
        "
      
      
        select top 1 ImageContext from ImageFile where ImageName='
      
      
        "
      
       + imageName + 
      
        "
      
      
        '
      
      
        "
      
      
        ;

            myconn 
      
      = 
      
        new
      
      
         SqlConnection(conn);

            SqlDataAdapter sda 
      
      = 
      
        new
      
      
         SqlDataAdapter(str, conn);

            DataSet myds 
      
      = 
      
        new
      
      
         DataSet();

            myconn.Open();

            sda.Fill(myds);

            myconn.Close();

            Byte[] Files 
      
      = (Byte[])myds.Tables[
      
        0
      
      ].Rows[
      
        0
      
      ][
      
        "
      
      
        ImageContext
      
      
        "
      
      
        ];

            MemoryStream ms 
      
      = 
      
        new
      
      
         MemoryStream(Files);

            Image i 
      
      =
      
         Image.FromStream(ms);

            pictureBox1.Image 
      
      = i;
      
        //
      
      
        綁定到pictureBox1控件上
      
    

?

C# 圖片存入SQL Server數據庫


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 建湖县| 阿巴嘎旗| 周至县| 双鸭山市| 客服| 朝阳市| 台南市| 原平市| 微山县| 枞阳县| 桐城市| 南阳市| 渑池县| 连城县| 景宁| 清远市| 梁平县| 河间市| 仁寿县| 怀仁县| 石泉县| 修水县| 宽城| 潞西市| 阿拉善右旗| 东辽县| 嘉黎县| 内江市| 绥江县| 甘德县| 清原| 岳阳市| 会理县| 合山市| 和平区| 石河子市| 城市| 门源| 抚松县| 遵义县| 河东区|