建议以编程方式下载SQL Server CE文件

问题描述:

我需要一些关于从服务器以编程方式下载SQL Server Compact Edition .sdf文件的最佳方法的建议。我尝试了两种不同的方式,我喜欢一种更好的方式,但是由于部署项目后客户端的安全问题,我的老板要求我转向另一个方向,一旦下载它们就会破坏.sdf文件。



方法1(我喜欢的方式):



I need some advice on best way to programmatically download SQL Server Compact Edition .sdf files from a server. I've tried 2 different ways and I like one way better, but due to security issues with the client once the project is deployed, my boss is asking me to go the other direction that ends up corrupting the .sdf files once they are downloaded.

Method 1 (The way I like):

int bytesRead = 0;
byte[] buffer = new byte[2048];

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(FTPLocation + "sdfFile"));

request.Proxy = null;

request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true;
request.Credentials = new NetworkCredential(userNameInput, passwordInput);                                                
request.Method = WebRequestMethods.Ftp.DownloadFile;

Stream reader = request.GetResponse().GetResponseStream();
FileStream fileStream = new FileStream("File Location to Store .sdf file", FileMode.Create);

while (true)
{
     bytesRead = reader.Read(buffer, 0, buffer.Length);
     if (bytesRead == 0)
          break;

     fileStream.Write(buffer, 0, bytesRead);
}

fileStream.Close();
fileStream.Dispose();
reader.Close();
reader.Dispose();





方法2(这会导致文件损坏)



Method 2 (This causes the corruption of the files)

WebClient dbClient = new WebClient();
dbClient.DownloadFile(downloadLocation + "sdfFile", "File Location to store .sdf file");





有没有人得到第二种方法没有任何问题?我无法使用第二种方法进行干净的测试。安全问题是客户端的笔记本电脑无法登录到FTP服务器下载文件,他们只能从网站下载。



Has anyone gotten the second method to work without any issues? I can't get a clean test using the 2nd method. The security issue is that the client's laptops can't login to FTP server to download files, they can only download from a website.

尝试这个控制台应用程序将会显示服务器返回的所有标题。

作为命令行参数的SDF文件的URL。

Try this console app that will show you all of the headers returned by the server.
Url to the SDF file as the command line argument.
using System;
using System.Net;

namespace HttpHeaders
{
  class Program
  {
    static void Main(string[] args)
    {
      if (args.Length < 1)
        return; // just quit. this is a hack tool after all...
      string uri = args[0];
      var request = (HttpWebRequest)WebRequest.Create(uri);
      request.Method = "HEAD";
      var response = request.GetResponse();
      foreach (var key in response.Headers.Keys)
      {
        Console.Write((string)key);
        Console.Write(": ");
        Console.WriteLine(response.Headers[(string)key]);
      }
    }
  }
}