C# 在数据库中插入和更新图像或文件
通过从图片框中读取图像,我将图像存储到表test中的数据库(id、name、image)。请帮帮我。 这是我的代码:C# 在数据库中插入和更新图像或文件,c#,sql-server,entity-framework,ado.net,C#,Sql Server,Entity Framework,Ado.net,通过从图片框中读取图像,我将图像存储到表test中的数据库(id、name、image)。请帮帮我。 这是我的代码: public byte[] COnvertToBinary(string Path) { byte[] data = null; FileInfo fInfo = new FileInfo(Path); long numBytes = fInfo.Length; FileStream fStream = n
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.");
}
为什么要贴标签?我在那里看不到SQL。也许你应该改为使用标签。我使用ADO.Net Entity Data Model@jarlh连接到db你确定要添加“表”而不是“行”吗?通常情况下,您不使用Table,而是直接使用表的名称(例如:MyTableOfImage.File=Convert(textbox1.Text))@Digital3D tbl.File=Convert(textbox1.Text);文件,它是我表中的行名称。我更正(编辑)了要理解的名称。你的问题缺少重要信息。运行代码时会发生什么?错误消息?未处理的异常?没有什么?