将数据库sql server image字段取出来保存为图片

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XZL.ERP.Base.Common;
using XZL.ERP.Base.Connection;
using XZL.ERP.POS.Model.ViewModel.Mobile;
using XZL.ERP.POS.Model.Weixin;
using System.IO;
using System.Drawing;
using System.Xml.Linq;

namespace XZL.ERP.POS.DAL.Mobile
{
    public  class Scan_Dal : Base_DAL
    {

        /// <summary>
        /// 查询商品数据
        /// </summary>
        /// <returns></returns>
        public Result GetScandata(string code)
        {
            string str;
            try
            {
                StringBuilder sSQL = new StringBuilder();
                DataTable Com = new DataTable();
                 
                sSQL.Append("select   ImageID, ItemName,price,c1 ,IMG1.ItemCode ,itemImage,img1.ImageType from  OITM,IMG1" +
                    " where  IMG1.ItemCode = OITM.ItemCode  and OITM.itemcode  = '" + code + "'");
                Com = PosDbContex.QueryToDataTable(sSQL.ToString(), null);
                //byte[] bytes = Convert.FromBase64String(Com.Rows[0]["itemImage"].ToString());

                //byte[] bytes = Encoding.Default.GetBytes( Com.Rows[0]["itemImage"].ToString());
                //MemoryStream memStream = new MemoryStream(bytes);
                //Image mImage = Image.FromStream(memStream);
                //memStream.Close();

                //创建命令对象,con参数是一个连接对象
                //SqlCommand com = new SqlCommand(sql, con);
                //con.Open();
                //SqlDataReader dr = com.ExecuteReader();
                //if (dr.Read())
                //{
                //    //声明数组,用于保存数据库的二进制数据
                //    byte[] mybyte = null;
                //    //读取数据保存到数组中
                //    mybyte = (byte[])dr["photo"];
                //    Image image;
                //    //读取数组数据成为文件流
                //    MemoryStream mymemorystream = new MemoryStream(mybyte);
                //    //转换成为图片格式。
                //    image = Image.FromStream(mymemorystream, true);
                //    this.pictureBox1.Image = image;
                //    mymemorystream.Close();  //关闭流
                //}

                MemoryStream ms = new MemoryStream();
                byte[] img = (byte[])Com.Rows[0]["itemImage"];
                MemoryStream memStream = new MemoryStream(img);
                Image mImage = Image.FromStream(memStream);
                Bitmap bp = new Bitmap(mImage);
                bp.Save("D:/img/"+DateTime.Now.ToString("yyyyMMddHHss") + ".jpg");//注意保存路径

                Result.Code= ResultCode.Success.GetHashCode();

                if (Com != null && Com.Rows.Count >0)
                {
                    Result.Code = ResultCode.Success.GetHashCode();
                    Result.Message = "成功";
                    Result.Data = Com;
                    return Result;
                }
                else
                {

                    Result.Message = "失败";
                }
                return Result;

                ///写到这里
            }
            catch (Exception ex)
            {
            //    Result.Code = ResultCode.Custom.GetHashCode();
            //    GetAchieResult_M.Message = ex.Message;
            //    GetAchieResult_M.Data = null;
                return Result;
            }
            
        }


        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        public string GetScandata1( string code)
        {
            string str;
            try
            {
                StringBuilder sSQL = new StringBuilder();
                Result resp = new Result();
                sSQL.Append("select top 1 * from OITM where ItemCode = '" + code + "'");
                str = PosDbContex.FirstOrDefault<string>(sSQL.ToString(), null);
                return str;
            }
            catch (Exception ex)
            {
            //    Result.Code = ResultCode.Custom.GetHashCode();
            //    GetAchieResult_M.Message = ex.Message;
            //    GetAchieResult_M.Data = null;
                return "没有数据";
            }
            
        } 
        public Boolean ChangCom(string Company, string Password)
        {
            try
            {
                string sSQL="";
                sSQL = "update  COMC set isactive =1 where  companyname = @Company and DBname = @Password  ;";
                sSQL += "update  COMC set isactive =0 where  companyname != @Company ;";
                bool ret = PosDbContex.Execute(sSQL, new  { Company = Company, Password = Password });
                return ret;
            }
            catch (Exception ex)
            {
            //    Result.Code = ResultCode.Custom.GetHashCode();
            //    GetAchieResult_M.Message = ex.Message;
            //    GetAchieResult_M.Data = null;
                return false;
            }
            
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值