10/10/2018, 09:27

Làm thế nào đưa file nhạc lên CSDL dưới định dạng binary

Xin chào các bạn, hiện tại mình đã đưa được file image lên CSDL dưới dạng binary, và cũng có thể đọc ra được. Nhưng mình cũng làm tương tự như khi insert hình ảnh cho file nhạc thì lại không được. Không hiểu có vấn đề gì ở đây. Các bạn giúp mình với. Bạn nào có code cho mình xin lun ok. Có thể gởi lên diễn đàn lun, hoặc nếu cảm thấy bất tiện thì có thể send vào email của mình: khoa.phamdang@ymail.com. Cảm ơn các sư huynh rất nhiều. Giúp em nhé!

[=========> Bổ sung bài viết <=========]

Xin bổ sung thêm, đây là code mình đã insert hình ảnh thành công vào CSDL. Rất mong các bạn giúp mình để có thể insert thành công file nhạc vào CSDL lun. Cảm ơn các bạn rất nhiều

[=========> Bổ sung bài viết <=========]

Xin bổ sung thêm, đây là code mình đã insert hình ảnh thành công vào CSDL. Rất mong các bạn giúp mình để có thể insert thành công file nhạc vào CSDL lun. Cảm ơn các bạn rất nhiều


protected void btnThemhinh_Click(object sender, EventArgs e)
{
FileInfo imageInfo = new FileInfo(File1.Value.Trim());

if (!imageInfo.Exists)
this.RegisterClientScriptBlock("alertMsg", "<script>alert('please select one image file.');</script>");
else
{
switch (imageInfo.Extension.ToUpper())
{
case ".JPG": this.UploadImageFile(imageInfo); break;
case ".GIF": this.UploadImageFile(imageInfo); break;
case ".BMP": this.UploadImageFile(imageInfo); break;
default: this.RegisterClientScriptBlock("alertMsg", "<script>alert('file type error.');</script>"); break;
}
}
Show_Listhinh();
}
private void UploadImageFile(FileInfo info)
{
SqlConnection objConn = null;
SqlCommand objCom = null;
try
{
byte[] content = new byte[info.Length];
FileStream imagestream = info.OpenRead();
imagestream.Read(content, 0, content.Length);
imagestream.Close();

objConn = new SqlConnection("server=localhost;database=PhamDangK hoa;uid=sa;pwd=sa;");
objCom = new SqlCommand("insert into Pictures(PicName,Descriptions,Pic,SerrialID)values (@PicName,@Descriptions,@Pic,@SerrialID)", objConn);

SqlParameter PicNameParameter = new SqlParameter("@PicName", SqlDbType.NVarChar);
if (this.txtHinh.Text.Trim().Equals(""))
PicNameParameter.Value = "Default";

else
PicNameParameter.Value = this.txtHinh.Text.Trim();
objCom.Parameters.Add(PicNameParameter);

SqlParameter DescriptonsParameter = new SqlParameter("@Descriptions", SqlDbType.NVarChar);
if (this.FreeTextBox4.Text.Trim().Equals(""))
DescriptonsParameter.Value = "Default";

else
DescriptonsParameter.Value = this.FreeTextBox4.Text.Trim();
objCom.Parameters.Add(DescriptonsParameter);

SqlParameter pictureParameter = new SqlParameter("@Pic", SqlDbType.Image);
pictureParameter.Value = content;
objCom.Parameters.Add(pictureParameter);

SqlParameter SerrialIDParameter = new SqlParameter("@SerrialID", SqlDbType.Int);
if (this.DropDownList2.SelectedValue.Trim().Equals("" ))
SerrialIDParameter.Value = "Default";

else
SerrialIDParameter.Value = this.DropDownList2.SelectedValue.Trim();
objCom.Parameters.Add(SerrialIDParameter);

objConn.Open();
objCom.ExecuteNonQuery();
objConn.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
objConn.Close();
}
}
protected void btnXoahinh_Click(object sender, EventArgs e)
{
Classprogress cls_ = new Classprogress();
string Del = "Delete from Pictures where PicID=" + DropDownList3.SelectedValue;
Boolean kq_ = cls_.Insert_Update_Delete(Del);
if (kq_ == true)

thongbao.InnerHtml = "Đã xóa thành công !";

else
thongbao.InnerHtml = "Chưa xóa được. Thử lại !";

Show_Nhomhinh();
Show_Listhinh();
}
protected void btnCapnhatnhom_Click(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
string SqlStr = "Update SerrialPics set Title=N'" + txtCNXoa_TieuDe.Text + "',Descriptions=N'" + FreeTextBox2.Text + "' where SerrialID=" + DropDownList2.SelectedValue;
Classprogress cls = new Classprogress();
Boolean kq = cls.Insert_Update_Delete(SqlStr);
if (kq == true)
{
thongbao.InnerHtml = "Đã cập nhật thành công !";
Show_MenuHinhanh();
}
else
thongbao.InnerHtml = "Chưa cập nhật được vào cơ sở dữ liệu !";
Show_Nhomhinh();
Show_Listhinh();
}
lucky-boy viết 11:34 ngày 10/10/2018
Database của bạn chắc dạng khủng lắm mới làm lưu file nhạc vào CSDL :-s
khoa_maspro viết 11:44 ngày 10/10/2018
Vậy theo bạn thì mình phải làm thế nào? Vì mình muốn website của mình có thể nghe được nhạc luôn. Nếu ta lưu trong folder thì dung lượng cũng sẽ rất nặng như khi để trong máy tính. Mọi người góp ý giúp mình nhé! Cảm ơn rất nhiều!!!
ngoc_viet08 viết 11:33 ngày 10/10/2018
lưu ở đâu thì nó cũng chiếm nhiều dung lượng cả chứ ??? cách tốt nhứt là bạn trang bị ổ đĩa khủng =))
fotech_nd viết 11:30 ngày 10/10/2018
Mình cũng có câu hỏi giống với bạn lucky-boy là chắc cái DB của bạn phải có dung ... tích khủng lắm nhỉ
khuongCOMPUTER viết 11:34 ngày 10/10/2018
Trong khi người ta tìm cách lưu bớt dữ liệu ít quan trọng ra txt để xử lý cho nhẹ thì bác này tìm cách nhét file mp3 vào DB.
Muốn cái site nghe được nhạc thì file nhạc để đâu chả được, viết cái database chứa link đến file nhạc thôi là được rồi.
khoa_maspro viết 11:37 ngày 10/10/2018
Cảm ơn mọi người nhiều lắm, Vậy là mình phải bỏ cái ý định đưa file nhạc vào database rùi. Hic, làm mình nghiên cứu mất nửa ngày vì dụ này.
trumaninza viết 11:40 ngày 10/10/2018
2 vấn đề thường quyêt định chất lương sản phẩm ở trong lập trình la bộ nhớ và tốc đọ.

Lưu ở đâu cũng tốn chừng đó bộ nhó - điều này đúng nhưng
lưu trên đĩa cứng thì có thể truy xuát qua địa chỉ thư mục và dùng ngay

Còn lưu trong database, phải đọc db, tái tạo lại file gốc rồi mới play

Như vậy tóc độ giảm hẳn. Các ứng dụng web giải trí mà đợi lâu quá ko nghe nhạc được ko chơi được thì ng ta sẽ tìm đến site khác - cứ theo tâm lý người dùng mà nghĩ
Bài liên quan
0