首页 > 解决方案 > 在数据库中插入和更新图像或文件

问题描述

我通过从图片框中读取图像将图像存储到表测试(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.");
    }

标签: c#sql-serverentity-frameworkado.net

解决方案


推荐阅读