Smobiler图片二进制上传处理

#region 数据库图片存取
    /// <summary>
    /// <strong><span style="color:#ff0000;">导入图片到数据库</span></strong>
    /// </summary>
    /// <param name="filePath"></param>
    public void Import(string filePath)
    {
        string fileName = filePath.Substring(filePath.LastIndexOf(@"\") + 1, filePath.LastIndexOf(".") - filePath.LastIndexOf(@"\") - 1);
        FileStream fileStream = new FileStream(filePath, FileMode.Open);
        byte[] imageBytes = new byte[fileStream.Length];
        BinaryReader binaryReader = new BinaryReader(fileStream);
        imageBytes = binaryReader.ReadBytes(Convert.ToInt32(fileStream.Length));
        Toast(imageBytes.ToString());
        SqlConnection sqlConnection = new SqlConnection(@"data source=;database=;uid=sa;pwd=;");
        sqlConnection.Open();
        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = sqlConnection;
        sqlCommand.CommandText = "update AA_Picture set Picture = @Image where cGUID = @cGUID";
        sqlCommand.Parameters.Add("Image", SqlDbType.Image);
        sqlCommand.Parameters.Add("cGUID", SqlDbType.VarChar);
        sqlCommand.Parameters["Image"].Value = imageBytes;
        sqlCommand.Parameters["cGUID"].Value = "17";
        sqlCommand.ExecuteNonQuery();
        sqlConnection.Close();
    }

    /// <summary>
    //<strong>/<span style="color:#ff0000;">导出图片</span></strong>
    /// </summary>
    /// <param name="SID"></param>
    /// <returns>bitmap</returns>
    public byte[] Export(string ID)
    {
        byte[] imagebytes = null;
        SqlConnection sqlConnection = new SqlConnection(@"data source=;database=;uid=sa;pwd=;");
        sqlConnection.Open();
        SqlCommand sqlCommand = new SqlCommand("select Picture from AA_Picture where cGUID=@cGUID", sqlConnection);
        sqlCommand.Parameters.Add("cGUID", SqlDbType.VarChar);
        sqlCommand.Parameters["cGUID"].Value = ID;
        SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
        while (sqlDataReader.Read())
        {
            imagebytes = (byte[])sqlDataReader.GetValue(0);
        }
        sqlDataReader.Close();
        sqlCommand.Clone();
        sqlConnection.Close();
        return imagebytes;
    }

private void cameraButton1_ImageCaptured(object sender, BinaryResultArgs e)
{
try {
if (string.IsNullOrEmpty(e.error)) {
string name = DateTime.Now.ToString(“yyyyMMddHHmmss”) + “.png”;
e.SaveFile(name);
image1.ResourceID = name;
string filePath = @"…\bin\Debug\Resources\Upload\InventoryPhoto" + name;
Import(filePath);//保存图片到数据库
}
} catch (Exception ex) {
Toast(ex.Message);
}
}
private void button1_Press(object sender, EventArgs e)
{
try
{
image1.ResourceMode = ResourceMode.Data;
image1.ResourceData = Export(“17”);
image1.Refresh();
}
catch (Exception ex)
{
Toast(ex.Message);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值