1

For saving File Info:

string[] fileStream = File.ReadAllBytes(files[1]);
FileInfo fileinfo = new FileInfo(files[1]);
string fileName = Path.GetFileNameWithoutExtension(files[1]);
string extension = Path.GetExtension(files[1]);

Inserting an object[,] in to database:

rowData[0, 0] = "@cvTitle";
rowData[0, 1] = fileName;
rowData[1, 0] = "@cvData";
rowData[1, 1] = fileStream;
rowData[2, 0] = "@recieveDate";
rowData[2, 1] = DateTime.Now;
rowData[3, 0] = "@cvSize";
rowData[3, 1] = fileinfo.Length;
rowData[4, 0] = "@fileTypeID";
rowData[4, 1] = extension.ToString();

But its not upload it in correct format.

And how can I get the same file from my database without any change in my file.

2 Answers 2

1

You have string array instead of byte array

your updated code for uploading image

byte[] fileStream = File.ReadAllBytes(files[1]);
FileInfo fileinfo = new FileInfo(files[1]);
string fileName = Path.GetFileNameWithoutExtension(files[1]);
string extension = Path.GetExtension(files[1]);

for retrieving the image

DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("Your query", Your Connection);
cmd.CommandType = CommandType.text;
da.SelectCommand = cmd;
cmd.ExecuteNonQuery();
da.Fill(dt);

Hope it will help you

0
1

The reason your code wouldn't work is because you are using a string[] rather than a byte[], which is the return type of the ReadAllBytes command.

byte[] fileBytes = File.ReadAllBytes(files[1]); // assuming files[1] is a string pointing to the file with a relative/absolute path
FileInfo info = new FileInfo(files[1]);
string name = Path.GetFileNameWithoutExtension(files[1]);
string extension = Path.GetExtension(files[1]);

And for your other problem:

byte[] fileBytes = rowData[1, 1];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.