C# 在C中将二进制图像保存到SQL-CE

C# 在C中将二进制图像保存到SQL-CE,c#,.net,sql,sql-server-ce,C#,.net,Sql,Sql Server Ce,我有一个名为Recipe的SQL-CE表,在这个表中有4列IDIT、Namenvarchar、Instructionsnvarchar和imagebinary 这就是UI的外观: 单击“添加新”按钮时,我希望用户输入名称、说明,然后单击“添加图像”按钮并选择图像。然后,我希望包含文本“内存中没有图像”的标签更改为“内存中的图像”,以表示图像正在等待写入数据库。最后,当用户单击Save图标时,我希望保存3个nvarchar字段,以及作为二进制文件写入表中的图像 代码如下: 添加图像按钮 保存图标

我有一个名为Recipe的SQL-CE表,在这个表中有4列IDIT、Namenvarchar、Instructionsnvarchar和imagebinary

这就是UI的外观:

单击“添加新”按钮时,我希望用户输入名称、说明,然后单击“添加图像”按钮并选择图像。然后,我希望包含文本“内存中没有图像”的标签更改为“内存中的图像”,以表示图像正在等待写入数据库。最后,当用户单击Save图标时,我希望保存3个nvarchar字段,以及作为二进制文件写入表中的图像

代码如下:

添加图像按钮

保存图标

最后,我希望数据库中保存的每个对应图像都显示在图片框中的“添加图像”按钮下方,因此需要将其从二进制格式转换回显示图像

我的问题


在打开OpenFileDialog屏幕选择文件后,如何将“内存中无图像”标签更改为显示“内存中图像”。然后,我需要添加什么代码才能使“保存”按钮单击事件将图像作为二进制文件写入我的SQL-CE db。

一旦有了该文件,就可以将其读入字节数组,然后将该数组复制到当前记录中的正确字段中

类似于

OpenFileDialog OpenFileDialog=新建OpenFileDialog

// if the user selects a file
if(openFileDialog.ShowDialog() == DialogResult.OK)
{
    // now open the file ..
    FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read);

    // or you may use 
    // FileStream fs = (FileStream)openFileDialog.OpenFile();

    BinaryReader br = new BinaryReader(fs);
    Byte[] buffer = br.ReadBytes((Int32)fs.Length);
    br.Close();
    fs.Close();
}

// now copy content of 'buffer' to the correct filed of the recordset

要更改标签,您应该能够通过订阅正确的BindingSource事件CurrentItemChanged(如果我的内存正常)在记录集中导航时检查上述字段是否有值,我已经在下面总结了我需要完成的工作。在你的文件路径中,我将做什么?用户将使用openfiledialog从其本地计算机选择文件,因此我希望文件路径是autp填充的?您是对的。。用通过OpenFileDialog-cmApologies收集的path+文件名替换yourFilePath,但代码是什么,因为我以前没有使用过OpenFileDialog。谢谢你,我为你修改了代码。。试试看。。请注意,在进行修改时,我没有VS,因此您必须检查语法的准确性。关于OpenFileDialog的一个很好的参考资料-希望这对您有所帮助。。快乐编码
 private void recipeBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.recipeBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.recipeDataSet);
            MessageBox.Show("Recipe Saved!", "Save Item");

        }
// if the user selects a file
if(openFileDialog.ShowDialog() == DialogResult.OK)
{
    // now open the file ..
    FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read);

    // or you may use 
    // FileStream fs = (FileStream)openFileDialog.OpenFile();

    BinaryReader br = new BinaryReader(fs);
    Byte[] buffer = br.ReadBytes((Int32)fs.Length);
    br.Close();
    fs.Close();
}

// now copy content of 'buffer' to the correct filed of the recordset