如何使用数据绑定将图片添加到数据库

时间:2012-07-29 23:38:53

标签: c# sql-server ado.net

我想使用DataBindign将图片添加到数据库,但我不知道该怎么做。 这是我用来加载图片的代码:

byte [] 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);
        }
    }

1 个答案:

答案 0 :(得分:0)

以下是如何从数据库中读取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();
        }
    }