Click here to Skip to main content
15,887,328 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
HI ,
I am a beginner in Winfroms. I am using C# , Visual Studio 2008 and Sql server 2005

I am given a task to create a screen along with the functionality from which the user should be able to Add, view and remove an attachment.

I have three buttons on the from , Add Attachment , View Attachment and Remove Attachment.

I have been told save the attachement in the sql server 2005 database in bits format. Itseems the attachment can be any kind of file txt, pdf, doc, gif, audio, video .....


On clicking the Add Attachment Button the user should be able to browse the file and once the user browses and selects the file and clicks Save. The file should get saved in the a table in DB.

On click of the the View Attachement Button, the user attachement file should be shown to the User .

On clicking of the Remove Attachement Button , the attachement should be deleted from the database.


Itseems the size of the attachment should be limited to 5MB.

I am very new to programing... Can any one please help me out with this..
I also want to know what should be the data type of this field in the table in the database.



will be waiting for the reply....
thanks
Posted

Hey Christian Graus ,

Thanks for your encouragement, Your words stuck my heart, which made me think that I can work out this task my self, If I put some effort instead of expecting the answers from the forums, U Rock...

I am able to save and retrieve the file... with the below code:
private void button1_Click(object sender, EventArgs e)
{ save//
OpenFileDialog openFileDialog1 = new OpenFileDialog();

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string sFileName = "";
long nLength = 0;
byte[] barFile = null;
if (openFileDialog1.FileName != "")
{
System.IO.FileStream fs = new System.IO.FileStream(openFileDialog1.FileName, System.IO.FileMode.Open);
System.IO.FileInfo fileInfo = new System.IO.FileInfo(openFileDialog1.FileName);
sFileName = fileInfo.Name;
nLength = fs.Length;
barFile = new byte[fs.Length];
fs.Read(barFile, 0, Convert.ToInt32(fs.Length));
fs.Close();
}
//save into database....

DV_PRU.DALC.EmployeesDALC.InsertTest(barFile, nLength, sFileName);
}
}

private void button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = DV_PRU.DALC.EmployeesDALC.GetTest();
if (dt.Rows.Count > 0)
{

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
System.IO.MemoryStream ms = new System.IO.MemoryStream((Byte[])dt.Rows[0]["File_Content"]);

saveFileDialog1.FileName = dt.Rows[0]["File_Name"].ToString();
int FileLength = int.Parse(dt.Rows[0]["File_Length"].ToString());
//set default value
string sSaveLocation = @"C:\DVPRU";
saveFileDialog1.ShowDialog();
sSaveLocation = saveFileDialog1.FileName.ToString();

// create a write stream
System.IO.FileStream writeStream = new System.IO.FileStream(sSaveLocation, System.IO.FileMode.Create, System.IO.FileAccess.Write);

// write to the stream
mtdReadWriteStream(ms, writeStream,FileLength);
MessageBox.Show("OK");
}
}

private void mtdReadWriteStream(System.IO.MemoryStream readStream, System.IO.FileStream writeStream, int Length)
{
// int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, Length);
}
readStream.Close();
writeStream.Close();
}
}


Any improvements to the code..is appriciated...
 
Share this answer
 
v2
Comments
naylynn 8-Jun-13 11:57am    
I am beginner for C#. Now i want to load and retrieve PDF file to SQL by using C#. I want to write small E-Library project for my school. Please someone help me---
Adding a file:

You will need to use the System.Windows.Forms.OpenFileDialog to select the file, then load that into a byte array. Standard code to load it to a byte array would be something like:

C#
if (File.Exists(filename)) {
	FileInfo finfo = new FileInfo(filename);

	FileStream fstream = new FileStream(filename, FileMode.Open, FileAccess.Read);
	BinaryReader br = new BinaryReader(fstream);

	byte[] data = br.ReadBytes(Convert.ToInt32(finfo.Length));

	br.Close();
	fstream.Close();
	fstream.Dispose();

	//   Do the upload with data
}


You should store this in a column of type VarBinary(Max).

To review it, just reverse the process, saving the file to a temp location and calling Process.Start(<myfilename>) to open it.

Deleting it is obvious.
 
Share this answer
 
Hi Thanks for your quick reply,


i can do the deleting part...

I am a beginner in programing and winforms.

can you please explaing me in detail .. regarding
how to use the open dialog box, then once the user selects the file..
how would i check the size of the file ( 5 MB) and then save it ??

and also regarding retrieving from the database and opening the file



will be waiting for reply...
 
Share this answer
 
1 - edit your post, do not post replies as 'answers'. It makes it hard to read, esp when the posts are not ordered in the order they were posted, and are not threaded

2 - Who has given you this task ? If your teacher, you should refer to your books and ask them first if you are stuck. If it's paid work, then what a disgrace that you're being paid to do something simple and don't have the first clue how to do it. How about buying a book on winforms and reading it, trying to do your own work, then asking here when you get stuck, instead of just asking us to do your work for you ?

The task you describe is really too complex for a 'beginner in programming and winforms'. You need to know C#, SQL, ADO.NET, etc to do this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900