Print Page | Close Window

Upload file into Database

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1105
Printed Date: 28-Mar-2024 at 9:08am


Topic: Upload file into Database
Posted By: vinothvdp
Subject: Upload file into Database
Date Posted: 28-Feb-2009 at 9:09pm

Hi sir,

I am using SQL server 2005, how to store and retrieve. Xls,. Doc,. PDF,. jpg ,.bmp file into database, can you give some me examples.

 
Thank you,



Replies:
Posted By: davidklitzke
Date Posted: 03-Mar-2009 at 8:41am
This would appear to be  a SQL Server issue involving storing and retrieving of data, not an IdeaBlade issue involving storing amd retrieving of Business Entities.


Posted By: vinothvdp
Date Posted: 03-Mar-2009 at 9:48pm
i tried this,this working fine,is this right?

PersistenceManager PM = PersistenceManager.DefaultManager;

EntityList<ImageUpload> ListDatas = new EntityList<ImageUpload>();

private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)

{

ListDatas.Add(ImageUpload.Create(PM));

bsFile.MoveLast();

}

private void Form1_Load(object sender, EventArgs e)

{

ListDatas.ReplaceRange(PM.GetEntities<ImageUpload>());

bsFile.DataSource = ListDatas;

}

OpenFileDialog openFileDialog1 = new OpenFileDialog();

private void SelectFileultraButton_Click(object sender, EventArgs e)

{

openFileDialog1.Filter = "All Files (*.*)|*.*";

openFileDialog1.Title = "Select File";

if (DialogResult.Cancel == openFileDialog1.ShowDialog())

{

return;

}

fileNameUltraTextEditor.Text = openFileDialog1.FileName;

string fileName = Path.GetFileName(fileNameUltraTextEditor.Text.ToString());

byte[] content = ReadFileToByteArray(fileName);

ImageUpload CurrentFile = bsFile.Current as ImageUpload;

CurrentFile.File= content;

CurrentFile.FileName = fileName;

}

protected static byte[] ReadFileToByteArray(string fileName)

{

FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read);

long len;

len = fileStream.Length;

Byte[] fileAsByte = new Byte[len];

fileStream.Read(fileAsByte, 0, fileAsByte.Length);

MemoryStream memoryStream = new MemoryStream(fileAsByte);

return memoryStream.ToArray();

}

private void saveToolStripButton_Click(object sender, EventArgs e)

{

if (PM.HasChanges())

{

try

{

PM.SaveChanges();

MessageBox.Show("Sucessfully saved");

}

catch (Exception ex)

{

MessageBox.Show(ex.Message.ToString());

}

}

else

{

MessageBox.Show("Nothing to save");

}

}

private void RetriveultraButton_Click(object sender, EventArgs e)

{

string fileName = string.Empty;

byte[] content = null;

ImageUpload CurrentFile = bsFile.Current as ImageUpload;

fileName = CurrentFile.FileName;

content = CurrentFile.File;

DirectoryInfo di = new DirectoryInfo("C:\\Test");

if (di.Exists == false)

{

di.Create();

}

FileStream fs = new FileStream(di + "\\" + fileName, FileMode.Create);

fs.Write(content, 0, System.Convert.ToInt32(content.Length));

fs.Seek(0, SeekOrigin.Begin);

fs.Close();

}




Print Page | Close Window