static void Main(string[] args)
        {
            // replace this string with your 

            // Sharepoint content DB connection string
            string DBConnString =
             "Server=SP2010\\gyccp;" +
             "Database=WSS_Content;Trusted_Connection=True;";

            // create a DB connection
            SqlConnection con = new SqlConnection(DBConnString);
            con.Open();

            // the query to grab all the files.
            SqlCommand com = con.CreateCommand();
            com.CommandText = "SELECT ad.SiteId, ad.Id, ad.DirName," +
                " ad.LeafName, ads.Content" +
                " FROM AllDocs ad, AllDocStreams ads" +
                " WHERE ad.SiteId = ads.SiteId" +
                " AND ad.Id = ads.Id" +
                " AND ads.Content IS NOT NULL" +
                " Order by DirName";

            // execute query
            SqlDataReader reader = com.ExecuteReader();

            while (reader.Read())
            {
                // grab the file’s directory and name
                string DirName = (string)reader["DirName"];
                string LeafName = (string)reader["LeafName"];

                Console.WriteLine("DirName:"+DirName);
                // create directory for the file if it doesn’t yet exist
                if (!Directory.Exists(DirName))
                {
                    Directory.CreateDirectory(DirName);

                    Console.WriteLine("Creating directory: " + DirName);
                }

                // create a filestream to spit out the file
                Console.WriteLine("full path:" + DirName + "/" + LeafName);
                FileStream fs = new FileStream(DirName + "/" + LeafName,
                    FileMode.Create, FileAccess.Write);
                BinaryWriter writer = new BinaryWriter(fs);


                
                // depending on the speed of your network,
                // you may want to change the buffer size (it’s in bytes)
                int bufferSize = 1000000;
                long startIndex = 0;
                long retval = 0;
                byte[] outByte = new byte[bufferSize];

                // grab the file out of the db one chunk
                // (of size bufferSize) at a time
                do
                {
                    retval = reader.GetBytes(4, startIndex, outByte, 0,
                        bufferSize);
                    startIndex += bufferSize;

                    writer.Write(outByte, 0, (int)retval);
                    writer.Flush();
                } while (retval == bufferSize);

                // finish writing the file
                writer.Close();
                fs.Close();

                Console.WriteLine("Finished writing file: " + LeafName);
            }

            // close the DB connection and whatnots
            reader.Close();
            con.Close();
            Console.ReadKey();
        }

 

文件转换成字节,代码如下:

 protected static byte[] GetByte(string strFilePath)
    {
        // 以二进制方式读文件
        FileStream fsMyfile = new FileStream(strFilePath, FileMode.Open, FileAccess.ReadWrite);
        // 创建一个二进制数据流读入器,和打开的文件关联
        BinaryReader brMyfile = new BinaryReader(fsMyfile);
        // 把文件指针重新定位到文件的开始
        brMyfile.BaseStream.Seek(0, SeekOrigin.Begin);
        byte[] bytes = brMyfile.ReadBytes(Convert.ToInt32(fsMyfile.Length.ToString()));
        // 关闭以上new的各个对象
        brMyfile.Close();
        return bytes;
    }

 

 

 

posted on 2012-08-30 13:13  gzh4455  阅读(291)  评论(0编辑  收藏  举报