C#写入和读取数据库blob字段代码

读取本地文件,存入数据库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;
        }







评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值