附加文件永远不会存储在我的本地数据库中 [英] Attached files are not getting stored in my local database for ever
问题描述
你好,
我根据 BALA SATHISH 发表的文章提出申请
< a href =http://www.codeproject.com/Articles/201722/Upload-and-Download-Files-From shellsGridView-Cells>从DataGridView单元格上传和下载文件 [ ^ ]
我开发了一个本地数据库软件,它还处于最后阶段。我的Windows应用程序可以在GridView(单元格)中存储不同类型的数据,并可以搜索数据。每当我上传并保存数据库中的数据时,它都会在指定的单元格和列中正确显示并保存文件,而且我也可以将文件下载到我的计算机上,但是当我关闭我的应用程序并且我提供的时候再次软件,上传的数据无法下载到我的电脑。在这种情况下该怎么办?
除了BALA SATHISH发表的文章之外,我还添加了一个名为 storage.sdf 的本地数据库。我的应用程序,我将所有文件保存到该。如果我第二次运行该软件,我也无法检索上传的数据
另外,我有一个保存按钮以保存我的整个工作并更新我的数据库中的所有内容。
如果我关闭我的应用程序并在之后获取它,我需要回调我的文件。现在该怎么办?
我需要认真帮助....
我的网格快照: http://tinypic.com/view.php?pic= 5z0bpv& s = 8#.U8tQRRYYEpE [ ^ ]
我拍摄错误的快照: http://tinypic.com/view.php?pic=2ziuas3&s=8#.U8tQRhYYEpE [ ^ ]
我的上传/下载和保存按钮代码如下:
HELLO,
I Have made an application based on the article published by BALA SATHISH
Upload and Download Files From DataGridView Cells[^]
I developed a local database software and it's in the final stage. My Windows application can store the different types of data in GridView (cells) and can search for data. While whenever I upload and save the data in my database, it's properly showing and saving the file in the specified cell and column, and also I am able to download the file to my computer too, yet when I close my application and if I feed the software again, the uploaded data cannot be download to my computer. What to do in this situation?
Additional to the article published by BALA SATHISH, I have added a localdata base called storage.sdf to my application and i am saving all my files to that. Then also i am unable to retrieve my uploaded data if I am running the software for the second time
Also, I have a save button in order to save my entire work and update everything in my database.
I require to call back my file if I close my application and also get it afterward. What to do now?
I need serious help pleasee....
Snapshot of my gridvew: http://tinypic.com/view.php?pic=5z0bpv&s=8#.U8tQRRYYEpE[^]
Snap shot of error i am getting: http://tinypic.com/view.php?pic=2ziuas3&s=8#.U8tQRhYYEpE[^]
My uploading/downloading and save button code is given below:
private void Savebtn_Click(object sender, EventArgs e)
{
this.Validate();
this.cncInfoBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.cncDataSet1);
try
{
MessageBox.Show("File Saved");
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnUpload_Click(object sender, EventArgs e)
{
try
{
//Throw error if attachment cell is not selected.
//make sure user select only single cell
if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1)
{
UploadAttachment(cncInfoDataGridView.SelectedCells[0]);
}
else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 2)
{
UploadAttachment(cncInfoDataGridView.SelectedCells[0]);
}
else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 3)
{
UploadAttachment(cncInfoDataGridView.SelectedCells[0]);
}
else
MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnDownload_Click(object sender, EventArgs e)
{
//Throw error if attachment cell is not selected.
//make sure user select only single cell
//and the cell have a value in it
if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1 && cncInfoDataGridView.SelectedCells[0].Value != null)
{
DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
}
else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 2 && cncInfoDataGridView.SelectedCells[0].Value != null)
{
DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
}
else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 3 && cncInfoDataGridView.SelectedCells[0].Value != null)
{
DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
}
else
MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void cncInfoDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//Throw error if attachment cell is not selected.
//make sure user select only single cell
//and the cell have a value in it
if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1 && cncInfoDataGridView.SelectedCells[0].Value != null)
{
DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
}
else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 2 && cncInfoDataGridView.SelectedCells[0].Value != null)
{
DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
}
else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 3 && cncInfoDataGridView.SelectedCells[0].Value != null)
{
DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
}
else
MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
/// <summary>
/// Upload Attachment at the provided DataGridViewCell
/// </summary>
/// <param name="dgvCell"></param>
private void UploadAttachment(DataGridViewCell dgvCell)
{
if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1)
{
using (OpenFileDialog fileDialog = new OpenFileDialog())
{
//Set File dialog properties
fileDialog.CheckFileExists = true;
fileDialog.CheckPathExists = true;
fileDialog.Filter = "All Files|*.*";
fileDialog.Title = "Select a file";
fileDialog.Multiselect = false;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
FileInfo fileInfo = new FileInfo(fileDialog.FileName);
byte[] binaryData = File.ReadAllBytes(fileDialog.FileName);
cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value = fileInfo.Name;
if (_myAttachments.ContainsKey(dgvCell.RowIndex))
_myAttachments[dgvCell.RowIndex] = binaryData;
else
_myAttachments.Add(dgvCell.RowIndex, binaryData);
}
}
}
else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 2)
{
using (OpenFileDialog fileDialog = new OpenFileDialog())
{
//Set File dialog properties
fileDialog.CheckFileExists = true;
fileDialog.CheckPathExists = true;
fileDialog.Filter = "All Files|*.*";
fileDialog.Title = "Select a file";
fileDialog.Multiselect = false;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
FileInfo fileInfo = new FileInfo(fileDialog.FileName);
byte[] binaryData = File.ReadAllBytes(fileDialog.FileName);
cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[2].Value = fileInfo.Name;
if (_myAttachments.ContainsKey(dgvCell.RowIndex))
_myAttachments[dgvCell.RowIndex] = binaryData;
else
_myAttachments.Add(dgvCell.RowIndex, binaryData);
}
}
}
else
{
using (OpenFileDialog fileDialog = new OpenFileDialog())
{
//Set File dialog properties
fileDialog.CheckFileExists = true;
fileDialog.CheckPathExists = true;
fileDialog.Filter = "All Files|*.*";
fileDialog.Title = "Select a file";
fileDialog.Multiselect = false;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
FileInfo fileInfo = new FileInfo(fileDialog.FileName);
byte[] binaryData = File.ReadAllBytes(fileDialog.FileName);
cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[3].Value = fileInfo.Name;
if (_myAttachments.ContainsKey(dgvCell.RowIndex))
_myAttachments[dgvCell.RowIndex] = binaryData;
else
_myAttachments.Add(dgvCell.RowIndex, binaryData);
}
}
}
}
/// <summary>
/// Download Attachment from the provided DataGridViewCell
/// </summary>
/// <param name="dgvCell"></param>
private void DownloadAttachment(DataGridViewCell dgvCell)
{
string fileName = Convert.ToString(dgvCell.Value);
//Return if the cell is empty
if (fileName == string.Empty)
return;
FileInfo fileInfo = new FileInfo(fileName);
string fileExtension = fileInfo.Extension;
byte[] byteData = null;
//show save as dialog
using (SaveFileDialog saveFileDialog1 = new SaveFileDialog())
{
//Set Save dialog properties
saveFileDialog1.Filter = "Files (*" + fileExtension + ")|*" + fileExtension;
saveFileDialog1.Title = "Save File as";
saveFileDialog1.CheckPathExists = true;
saveFileDialog1.FileName = fileName;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
byteData = _myAttachments[dgvCell.ColumnIndex];
File.WriteAllBytes(saveFileDialog1.FileName, byteData);
}
}
}
推荐答案
我添加了一个名为localdata的基地storage.sdf到我的应用程序
可能这是你的问题。运行应用程序后立即手动检查文件 - 不要再次运行应用程序来检查:使用SSMS或VS执行此操作 - 并确保您的数据肯定在其中。当然,你需要看看VS是如何测试你添加到项目中的 - 你很有可能在每次执行时都将它复制到Bin / Debug文件夹中 - 这会抛弃你的改变。
"I have added a localdata base called storage.sdf to my application"
Probably, this is your problem. Check the file manually immediately after running your application - do not run your app again to check: use SSMS or VS to do this - and ensure your data is definitely in it. Of it is, then you need to look at how VS is testing the for you added to your project - there is a very good chance that you have set it to be copied into the Bin/Debug folder each time you execute it - which would throw away your changes.
这篇关于附加文件永远不会存储在我的本地数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!