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();
}
[=========> 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();
}
Bài liên quan
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.
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ĩ