C# 如何使用DataBingin将图片添加到数据库

C# 如何使用DataBingin将图片添加到数据库,c#,sql-server,ado.net,C#,Sql Server,Ado.net,我想使用DataBingin将图片添加到数据库中,但我不知道如何操作。 这是我用来加载图像的代码: 字节[]imgData private void simpleButton5_Click_1(object sender, EventArgs e) { try { if (openFileDialog1.ShowDialog() == DialogResult.OK) { p

我想使用DataBingin将图片添加到数据库中,但我不知道如何操作。 这是我用来加载图像的代码:

字节[]imgData

 private void simpleButton5_Click_1(object sender, EventArgs e)
    {
        try
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                picture.ImageLocation = openFileDialog1.FileName;

                imgData = File.ReadAllBytes(openFileDialog1.FileName);
            }
        }
        catch (Exception ex)
        {
            // Could not load the image - probably related to Windows file system permissions.
            XtraMessageBox.Show("Cannot display the image.\n You may not have permission to read the file, or " +
                "it may be corrupt.\n\nReported error: " + ex.Message);
        }
    }

下面是如何从数据库中读取BLOB(在Oracle中)文件的代码。我希望它能对您有所帮助:

    private void ReadFileFromDatabase()
    {
        byte[] fileData = null;

        string selectSql = "SELECT FILE_DATA FROM BLOBTEST WHERE FILE_ID=1";

        OracleConnection con = new OracleConnection(conString);
        OracleCommand cmd = new OracleCommand(selectSql, con);

        try
        {
            con.Open();
            using (IDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    fileData = (byte[])reader["FILE_DATA"];
                    //do your operations with fileData here
                }
            }
        }
        finally
        {
            con.Close();
        }
    }

数据库和数据绑定是不同的事情。你到底想知道什么?例如,如果我想添加一个字符串,我使用以下命令:textbox1.DataBindings.add(“text”,sql.ds.Tables[“E”],“NUMETU”);如何添加图片?但您的数据不是来自数据库。为什么需要数据绑定?只需添加一个PictureBox并设置其图像:PictureBox.Image=Image.FromFile(openFileDialog1.FileName)是,但此图像保存在数据库中,我希望我的dataGrid显示数据库中的此图像,因为当我运行应用程序时,dataGrid应该显示数据库中已经存在的所有信息,我还需要解决这个问题,因为我将使用它以另一种形式加载信息