C# 将数据上载到本地数据库时,我遇到一个名为“的错误”;对象必须实现“可转换”;
当我试图将文件上载到本地数据库时,我遇到一个名为“对象必须实现|可转换”的错误 我真的不知道为什么这么说。我需要帮助 错误图像: 删除尝试捕获时出错- 我正在尝试的代码:C# 将数据上载到本地数据库时,我遇到一个名为“的错误”;对象必须实现“可转换”;,c#,sql,sql-server,visual-studio-2010,local-database,C#,Sql,Sql Server,Visual Studio 2010,Local Database,当我试图将文件上载到本地数据库时,我遇到一个名为“对象必须实现|可转换”的错误 我真的不知道为什么这么说。我需要帮助 错误图像: 删除尝试捕获时出错- 我正在尝试的代码: private void btnUpload_Click(object sender, EventArgs e) { try { //Throw error if attachment cell is not selected. //ma
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
MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace, "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UploadAttachment(DataGridViewCell dgvCell)
{
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 = true;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
SqlCeConnection cnn = new SqlCeConnection(Properties.Settings.Default.CncConnectionString);
FileInfo fileInfo = new FileInfo(fileDialog.FileName);
byte[] binaryData = File.ReadAllBytes(fileDialog.FileName);
cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value = fileInfo.Name;
SqlCeCommand cmd = new SqlCeCommand("INSERT INTO CncInfo (Drawings) VALUES (@DATA)", cnn);
cmd.Parameters.Add("@DATA", binaryData);
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
}
}
}
当我用下面的代码更新catch时。我得到了这样的错误
`该方法之所以被弃用,正是因为它不能以您期望的方式工作。按照警告消息的指示操作并使用。这是不可更改的。接口通常以“I”开头。试着评论一下你的Try-catch块。这样,您将看到错误的完整堆栈跟踪,它将告诉您错误来自代码中的何处。或者打印出ex.StackTrace.@kristianp如果你能编辑它并告诉我,我真的很感激。因为我对这个领域很陌生,你能把代码中的ex.Message改为ex.StackTrace并把结果放在你的问题中吗?这很容易做到。@kristianp好的,我按照你说的更新了错误。。请告诉我您似乎需要设置参数的类型,例如cmd.Parameters.Add(“@DATA”、binaryData、SqlType.Blob);
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace, "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);