Smobiler图片二进制上传处理

#region 数据库图片存取
    /// 
    /// 导入图片到数据库
    /// 
    /// 
    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();
    }

    /// 
    ///导出图片
    /// 
    /// 
    /// bitmap
    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);
}
}

你可能感兴趣的:(Smobiler)