上传,检索PDF,DOC和图像到SQL Server
我有以下代码将数据从我的计算机上的任何位置上载到名为Documents
的列中,数据类型为nvarchar(255)
,SQL Server中的表中作为文件路径。上传,检索PDF,DOC和图像到SQL Server
如何在表格中加载到Document
列的文件路径,并在需要点击Open
按钮时检索它? [见下面的截图]
我要完成这样的事情每个记录
在Save
按钮,点击保存到数据库文件路径
这里是我想要的代码使用
List<string> pdfFiles = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.CheckFileExists = true;
openFileDialog.AddExtension = true;
openFileDialog.Multiselect = true;
openFileDialog.Filter = "PDF files (*.pdf)|*.pdf";
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pdfFiles = new List<string>();
foreach (string fileName in openFileDialog.FileNames)
pdfFiles.Add(fileName);
}
}
private void button2_Click(object sender, EventArgs e)
{
string installedPath = Application.StartupPath + "pdf";
// Check whether folder path is exist
if (!System.IO.Directory.Exists(installedPath))
{
// If not create new folder
System.IO.Directory.CreateDirectory(installedPath);
}
// Save pdf files in installedPath
foreach (string sourceFileName in pdfFiles)
{
string destinationFileName = System.IO.Path.Combine(installedPath, System.IO.Path.GetFileName(sourceFileName));
System.IO.File.Copy(sourceFileName, destinationFileName);
}
}
如果您要将PDF文件内容存储到SQL数据库navchar(255)
不是一个好的解决方案。您应该阅读varbinary
数据类型。
此链接应该给你从哪里开始一些线索:http://www.c-sharpcorner.com/uploadfile/a20beb/how-to-save-pdf-word-and-excel-files-into-the-database/
谢谢你,我在Winforms – KAKA
即使你是赢的形式,只是前端或用户界面。将数据保存到数据库的后端工作应该相同或非常相似。 –
毫无疑问这里。请提供可以回答的有效问题。 –
@ShaneRay我想浏览加载到文本框和保存按钮单击,将其保存到数据库中的字段,并在需要时检索它 – KAKA
他看起来像他问了一个深远的,但有效的问题“我怎样才能加载文件路径到现场并在我需要时检索它?“尽管充满了语言错误,但我怀疑英语不是这里的第一语言。 – Trey