private static void SaveImage() { MySqlConnection connection = UserLab.Connect(); string sql = "UPDATE user SET user_avatar = @image where user_id = 4"; string textPath = Application.dataPath + "/Image/avatar.jpg"; FileStream fs = new FileStream(textPath, FileMode.Open); MySqlCommand command = new MySqlCommand(sql, connection); command.Parameters.Add("@image", MySqlDbType.Blob); byte[] fileData = new byte[fs.Length + 1]; fs.Read(fileData, 0, (int)fs.Length); command.Parameters["@image"].Value = fileData; command.ExecuteNonQuery(); }
private static void ReadImage() { MySqlConnection connection = UserLab.Connect(); string sql = "select user_avatar from user where user_id = 4"; MySqlCommand command = new MySqlCommand(sql, connection); MySqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { long len = reader.GetBytes(0, 0, null, 0, 0); byte[] buffer = new byte[len]; reader.GetBytes(1, 0, buffer, 0, (int)len); FileStream fileStream = new FileStream( Application.dataPath + "/Image/avatar_read.jpg", FileMode.Create, FileAccess.Write); fileStream.Write(buffer, 0, buffer.Length); fileStream.Close(); } }