sql 将文件转换为二进制上传(或下载)到数据库中

附件表结构如下:

上传附件:

        /// <summary>
        /// 上传附件
        /// </summary>
        /// <param name="filePath">上传文件路径</param>
        /// <param name="strAttachmentId">返回附件ID</param>
        /// <returns>true :上传成功  ; false :上传失败</returns>

private bool UploadAttachment(string filePath, out string strAttachmentId)
        {

            strAttachmentId = null;
            if (string.IsNullOrEmpty(filePath))
            {
                return false;
            }
            //截取路径下的文件名
            int index = filePath.LastIndexOf(@"\");
            string fileName = string.Empty;
            if (index > 0)
            {
                fileName = filePath.Substring(index + 1, filePath.Length - index - 1);
            }
            //将附件内容转换成二进制信息
            FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            byte[] bufContent = new byte[stream.Length];
            stream.Read(bufContent, 0, Convert.ToInt32(stream.Length));
            stream.Flush();
            stream.Close();

          

           //三层架构 访问数据库代码

            DBAccess.Model.attachment model = new DBAccess.Model.attachment();
            strAttachmentId = Guid.NewGuid().ToString("D");
            model.Id = strAttachmentId;
            model.Name = fileName;
            model.Content = bufContent;
            model.UploadTime = DateTime.Now;

            DBAccess.BLL.attachment bll = new DBAccess.BLL.attachment();
            if (bll.Add(model) <= 0)
            {
                strAttachmentId = null;
                return false;
            }
            return true;
        }

 

 

下载附件:

        /// <summary>
        /// 下载附件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDownload_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(tbAttachment.Text))
            {
                return;
            }

            //保存对话框
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "下载附件";
            saveFileDialog.Filter = "所有文件|*.*";
            saveFileDialog.FileName = tbAttachment.Text.Trim();
            saveFileDialog.RestoreDirectory = true;
            if ((bool)saveFileDialog.ShowDialog().GetValueOrDefault())
            {

                //三层架构 访问数据库代码
                DBAccess.BLL.attachment aBll = new DBAccess.BLL.attachment();
                DBAccess.Model.attachment modelAt = aBll.GetModel(m_ModelProblemorder.AttachmentId);

                //将数据库中的二进制转换为文件
                FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write);
                fs.Write(modelAt.Content, 0, modelAt.Content.Length);
                fs.Flush();
                fs.Close();
                MessageBox.Show("下载成功!", "提示信息");
            }
        }

 

 

    以上代码是上传,下载附件的主要代码,仅供参考~

posted @ 2013-05-17 15:22  三叶草╮  阅读(833)  评论(0编辑  收藏  举报