c# - 在数据库中插入和更新图像或文件
问题描述
我通过从图片框中读取图像将图像存储到表测试(id、名称、图像)中的数据库中。请帮我。这是我的代码:
public byte[] COnvertToBinary(string Path)
{
byte[] data = null;
FileInfo fInfo = new FileInfo(Path);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(Path, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int) numBytes);
return data;
}
private void Browse_Click(object sender, EventArgs e)
{
OpenFileDialog LoadMyFile1 = new OpenFileDialog();
LoadMyFile1 .Filter = "REPX Dosyaları(*repx.*) | *.repx*";
if (LoadMyFile1 .ShowDialog() == DialogResult.OK)
{
filename = LoadMyFile1 .FileName;
textBox1.Text = filename;
db.MyTable.Attach(MyTableBindingSource.Current as MyTable);
}
}
private void Save_Click(object sender, EventArgs e)
{
DbEntities1 db = new DbEntities1();
MyTable tbl = new MyTable();
tbl.MyfileName= textBox1.Text;
tbl.Myfile= COnvertToBinary(textBox1.Text);
db.MyTable.Add(tbl);
db.SaveChanges();
MessageBox.Show("Saved.");
}
解决方案
推荐阅读
- graph - 将文件层次结构制作为边列表,然后将树结构绘制为图形
- node.js - 如何在flutter(解密)和Node(加密)中实现RSA?
- django - 如何创建所有 Django 项目详细信息页面 url 的 xml 站点地图
- python - macOS python 2.7错误:IndexError:列表索引超出范围
- spring - Redis Sentinel 认证使用 Lettuce+SpringDataRedis
- selenium - 下拉列表/Xpath 帮助(Selenium w/Python
- python - 与值表迭代合并 - 以区间之间的值为条件(熊猫)
- javascript - 如何在 React 中导入 png 图像作为组件的背景?
- python - 如何在列上使用 dask groupby 分隔文件
- javascript - 在 `constructor()` 方法之外向 `this` 添加新属性是否被认为是不好的做法?