0

当我尝试将文件上传到本地数据库时,我收到一个名为“Object must implement |Convertible”的错误

我真的不明白为什么会这样说。我需要帮助。

错误图片:

在此处输入图像描述

删除尝试和捕获时出错 - https://imageshack.com/i/kpgFCzkrp

我正在尝试的代码:

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();
            }
        }
    }

当我用以下代码更新捕获时。我得到了这样的错误https://imageshack.com/i/ip6ESuuhp

   catch (Exception ex)
        {
           MessageBox.Show(ex.StackTrace, "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);

`

4

1 回答 1

0

不推荐使用Add方法,因为它不能以您期望的方式工作。执行警告消息告诉您的操作并使用AddWithValue

于 2014-07-30T08:33:55.127 回答