我想把WORD文档和EXCEL文件PDF文件等存储到数据库中,要使用的时候就另存为保存在磁盘用OFFICE打开,怎么办??用什么字段?那时示例代码??

解决方案 »

  1.   

    将文件转换为二进制流,然后存到数据库中text字段;
      

  2.   

    使用image类型字段可以存贮包括图片的大容量文件代码如下
    下面的函数使用 DataReader 检索 BLOB 值并将其分配到一个字节数组。因为 BLOB 已经完全在内存中,无需将数据分块,因此此值被分配一个到 Byte 数组。对 GetBytes 方法有两个调用;第一个接收以字节为单位的 BLOB 的长度,并用于确定 Byte 数组的维度。第二个调用检索数据。FileStream 对象用于将 Byte 数组写入磁盘。注意:在 Visual Basic 中,在声明 Byte 数组时,您必须从 BLOB 的长度中减去 1,因为 Visual Basic 声明数组的上限而不是长度。在其他语言如 C# 或 JScript 中,使用的长度值不需要减 1。 Private Sub SqlBlob2File(ByVal DestFilePath As String)
      Dim PictureCol As Integer = 0 ' the column # of the BLOB field
      Dim cn As New SqlConnection("server=localhost;integrated security=yes;database=NorthWind")
      Dim cmd As New SqlCommand("SELECT Picture FROM Categories WHERE CategoryName='Test'", cn)
      cn.Open()
      Dim dr As SqlDataReader = cmd.ExecuteReader()
      dr.Read()
      Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
      dr.GetBytes(PictureCol, 0, b, 0, b.Length)
      dr.Close()
      cn.Close()
      Dim fs As New System.IO.FileStream(DestFilePath, IO.FileMode.Create, IO.FileAccess.Write)
      fs.Write(b, 0, b.length)
      fs.Close()
    End SubPrivate Sub OlDbBlob2File(ByVal DestFilePath As String)
      Dim PictureCol As Integer = 0 ' the column # of the BLOB field
      Dim cn As New OleDbConnection("provider=sqloledb;server=localhost;user id=myuser;password=mypassword;initial catalog=NorthWind")
      Dim cmd As New OleDbCommand("SELECT Picture FROM Categories WHERE CategoryName='Test'", cn)
      cn.Open()
      Dim dr As OleDbDataReader = cmd.ExecuteReader()
      dr.Read()
      Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
      dr.GetBytes(PictureCol, 0, b, 0, b.Length)
      dr.Close()
      cn.Close()
      Dim fs As New System.IO.FileStream(DestFilePath, IO.FileMode.Create, IO.FileAccess.Write)
      fs.Write(b, 0, b.Length)
      fs.Close()
    End Sub
      

  3.   

    谢谢以上朋友!我的意思是磁盘上有WORD或EXCEL文件,我想选择了这个文件名后把它保存在数据库里,要用的时候就把它从数据库中取出,存在磁盘上,用OFFICE打开,其实还是一个WORD或EXCEL文档!!!只是不想它在磁盘上一文件夹里,太多,不好找
      

  4.   

    PictureCol是在数据库中的数据吧,我要把这个数据存储为磁盘上的文件如WORD,EXCEL...还要能用OFFICE打开它
      

  5.   

    如WORD文件,另存为:xxx.doc;EXCEL文件另存为:XXX.XLS
      

  6.   

    用OLE字段1 写进去    写好数据连接,操作数据库
        OleDbDataAdapter thisAdapter= new OleDbDataAdapter("SELECT ....",Connection)
        DataSet thisDateSet = new DataSEt();
        thisAdapter.Fill(thisDataSet,"表名")
        DataRow thisRow = thisDataSet.Tables["表名"].Row[0]);
        FileStream thisStream = new FileStream(ImageFile,FileMode,FileAccess,Read); 
        // ImageFile为你要写入的文件全名
        BinaryReader thisReader = new BinaryReader(thisStream);
        thisRow["你的OLE字段名"] = thidReader.ReadBytes((int)thisStream.length);
        thisAdapter.Update(thisDataSEt,"表名");2 读出来    写好数据连接,操作数据库
        OleDbDataReader thisRedaer = thisCommand.ExecuteReader();
        byte[] bs = (bytr[])thisReader["你的OLE字段"];
        MemoryStream thisStream = new MemoryStream(bs,0,bs.Length,true,true)
        PictureBox1.Image = Image.FromStream(thisSream);    要写成磁盘文件时
        byte[] bs = (bytr[])thisReader["你的OLE字段"];
        FileSream fs = new FileStream("你要存的文件名",FileMode.Create,FileAccess.Write);
        fs.Write(bs,0,bs.Length);
        
        应该懂了吧。
    你很小气,才给那么点儿分。