C#寫入和讀取資料庫blob欄位代碼

來源:互聯網
上載者:User

讀取本地檔案,存入資料庫blob欄位。

try            {                OpenFileDialog openFileDialog = new OpenFileDialog();                openFileDialog.Filter = "*.*|*.*";                openFileDialog.CheckFileExists = true;                openFileDialog.Title = "選擇上傳的檔案";                if (openFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)                {                    return;                }                FileStream fileStream = new FileStream(                    openFileDialog.FileName, FileMode.Open, FileAccess.Read);      byte[] fileData = new byte[fileStream.length + 1];                fileStream.Read(fileData, 0, (int)fileStream.Length);                string sql = "select * from TABLE_NAME where id = '" + id + "'";                DataSet ds = OA.RsGet(sql, null);                DataRow row;                if (ds.Tables[0].Rows.Count > 0)                {                    row = ds.Tables[0].Rows[0];                }                else                {                    row = ds.Tables[0].NewRow();                                        row["ID"] = Guid.NewGuid().ToString("N");    ds.Tables[0].Rows.Add(row);}                row["FILE"] = fileData;                row["otherField"] = roadWidth;                                if (OA.RsUpdate(ds) > 0)                {                    MessageBox.Show("儲存成功!");                }            }            catch(Exception exc)            {                MessageBox.Show("儲存出錯!請檢查資料。\n" + exc.Message);            }

         

讀取資料庫blob欄位,存成本地檔案。

/// <summary>        /// 讀取ORACLEBLOB欄位到檔案,返迴文件名  Add by ZhaoYong  |2012-03-21|        /// </summary>        /// <param name="idValue">索引值</param>        /// <param name="idField">索引欄位名稱</param>        /// <param name="table">要查詢的表名稱</param>        /// <param name="blobField">存放檔案的欄位名稱</param>        /// <param name="outFileFullName">儲存到本地的檔案名稱</param>        /// <returns></returns>        public static bool ReadBlobToFile(string idValue, string idField, string table, string blobField, string outFileFullName)        {            int PictureCol = 0;            outFileFullName = outFileFullName.Trim();            try            {                OracleCommand cmd = new OracleCommand("Select " + blobField + " From " + table +                    " Where " + idField + "='" + idValue + "'", OracleDb.OracleDb.Connection);                                OracleDataReader myReader = cmd.ExecuteReader();                myReader.Read();                if (myReader.HasRows == false)                {                    return false;                }                byte[] b = new byte[myReader.GetBytes(PictureCol, 0, null, 0, int.MaxValue) - 1];                myReader.GetBytes(PictureCol, 0, b, 0, b.Length);                myReader.Close();                System.IO.FileStream fileStream = new System.IO.FileStream(                    outFileFullName, System.IO.FileMode.Create, System.IO.FileAccess.Write);                fileStream.Write(b, 0, b.Length);                fileStream.Close();            }            catch            {                return false;            }            return true;        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.