开发者

How To: Store and Retrieve pdf file to mysql using C#

I am cur开发者_如何学Pythonrently working on a windows form application and I need to create functionality that enables users to upload PDF files to MySQL, and also be able to read the PDF file from the MySQL database then display the PDF file in the window form.

Can anyone show me some example code for how to do this?


You can store the pdf in a mysql database inside a column (varbinary(MAX)) with a byte[], to build byte[] try this:

byte[] bytes = null;
try
{
 bytes = File.ReadAllBytes(fileName);
}
catch (IOException)
        {
            ...      
        }

filename is the pdf name with the path. After that build some sql query to insert the byte[] and to get the byte[]

To show the pdf you need to convert byte[] to file like this:

Directory.CreateDirectory(Path.GetDirectoryName(fileName));
using (Stream file = File.Create(fileName))
{
file.Write(buffer, 0, buffer.Length);
}

buffer is the byte[]

then finally if you want to open the pdf:

Process process = new Process();
process.StartInfo.FileName = path;
process.Start();

path is the pdf name with the path.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜