zoukankan      html  css  js  c++  java
  • SharePoint 从数据库中下载 端口下的所有文件

    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;
        }
  • 相关阅读:
    $动态规划系列(1)——金矿模型的理解
    $Java HttpClient库的使用
    $Java-json系列(二):用JSONObject解析和处理json数据
    $百度应用引擎BAE的使用与应用部署
    利用ajax短轮询+php与服务器交互制作简易即时聊天网站
    MYSQL explain详解
    Redis 5种数据结构使用及注意事项
    Redis 存储机制
    memcache
    mysql分表和表分区详解
  • 原文地址:https://www.cnblogs.com/gzh4455/p/2663556.html
Copyright © 2011-2022 走看看