跪求将文本文件(txt,pdf,word)转化成二进流,存入数据库的代码!
   在线等,希望各位朋友们救救急。。

解决方案 »

  1.   


    //以txt为例
    byte[] buffer = Encoding.Unicode.GetBytes("");  FileStream fsw = new FileStream( "C:\\A.txt ",FileMode.Create);  
    fsw.Write(buffer,0,buffer.Length);  
    fsw.Close();  //剩下的你懂的
      

  2.   

    //保存word文档
    static void saveword()
    {
    FileInfo fi=new FileInfo(@"E:\学习试验项目\WordStore\wordStore\1.doc");
    FileStream fs=fi.OpenRead();
    byte[] wbytes=new byte[fs.Length];
    fs.Read(wbytes,0,Convert.ToInt32(fs.Length));
    string sqlstring = "insert into tb_word (memo,document) values ( @memo,@document)"; SqlParameter[] parameters = new SqlParameter[2]; parameters[0]=new SqlParameter("@memo",SqlDbType.NText);
    parameters[0].Value="ssss";//这里可以将word内容保存到数据库中
    parameters[1]=new SqlParameter("@document",SqlDbType.Image);
    parameters[1].Value = wbytes;
    DbHelperSQL.ExecuteSql(sqlstring,parameters);
    Console.WriteLine(i);

    } //从数据库读取word文档
    static void readword()
    {
    string sqlstring = "select top 1 * from Tb_word ";
    DataSet ds= new DataSet();
    ds = DbHelperSQL.Query(sqlstring);
    byte[] File=null;

    File=(byte[])ds.Tables[0].Rows[0]["document"];
    FileStream fs;
    string filename = System.Environment.GetEnvironmentVariable("Temp").ToString() + @"\temp";
    FileInfo fi=new System.IO.FileInfo(filename);
    fs=fi.OpenWrite();
    fs.Write(File,0,File.Length);
    fs.Close();

    string temp = ReadAllFromWord(filename);
    Console.WriteLine(temp);
    } #region  读取word 
    /// <summary>
    /// 读取word所有文字内容(不包含表格)
    /// </summary>
    static string ReadAllFromWord(string m_FilePath)
    {
    Word.ApplicationClass app = null;
    Word.Document doc = null;
    object missing = System.Reflection.Missing.Value;
    object FileName = m_FilePath;//@"E:\学习试验项目\ReadFromWordDoc\test.doc";
    object readOnly = true;
    object isVisible = false;
    try
    {
    app = new Word.ApplicationClass();
    doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
    ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref isVisible, ref missing,
    ref missing, ref missing, ref missing); string textString = "";
    //读取全部内容
    textString = doc.Content.Text.Trim();
    // int ParCount = this.getParCount(doc);//段数
    // for (int i = 1 ; i <= ParCount ; i++)
    // {
    // textString = textString + doc.Paragraphs[i].Range.Text.Trim();//doc.Content.Text.Trim();//
    // }
    textString = textString.Replace("\a",""); //替换空串为空。(word中\a代表空串,但在C#中,代表响铃 晕~~)否则显示控制台程序时会响
    textString = textString.Replace("\r","\n"); //替换回车为回车换行
    return textString;      
    }
    catch(Exception ex)
    {
    throw ex;
    }
    finally
    {
    if (doc != null)
    {
    try
    {
    doc.Close(ref missing, ref missing, ref missing);
    }
    catch
    {}
    doc = null;
    }
    if (app != null)
    {
    try
    {
    app.Quit(ref missing, ref missing, ref missing);
    }
    catch
    {}
    app = null;
    }
    GC.Collect();
    GC.WaitForPendingFinalizers(); }
    }
    #endregion

      

  3.   

    所有文件都是以二进制的方式存的
    BinaryReader:二进制读取类,
    BinaryWriter :二进制写入类
    txt File.ReadAllText("")