android 如何使用ftp更新毛伊岛数据库

oknwwptz  于 2023-06-04  发布在  Android
关注(0)|答案(1)|浏览(97)

我有一个麻烦内更新数据库(嵌入式)毛伊岛应用程序,之前我添加existinf文件database.db为我的毛伊岛项目与嵌入式资源。现在我删除此database.db,然后使用FTP服务器下载此database.db,但我的应用程序无法捕获绑定数据。。你可以看到这里(我命名为1.0版)连接sqlite数据库,和我的应用程序捕捉绑定数据很好

public static string DatabasePath => 
   Path.Combine(Environment.GetFolderPath
  (Environment.SpecialFolder.MyDocuments), DatabaseFilename);

private SQLiteConnection _connection;
public SQLiteConnection Connection => _connection ??= new 
       SQLiteConnection(ConstantsInfo.DatabasePath);

public  void Initialize()
  {
    //1.open target file to filestream
    using var dbFileStream = new FileStream(ConstantsInfo.DatabasePath, FileMode.OpenOrCreate);
    //2.open resource file database -->resourcestream
    using var dbAssetStream = typeof(ProdottiStorage)
 .Assembly.GetManifestResourceStream("Dataoffline."+ConstantsInfo.DatabaseFilename);
    //3.copy resource stream to filestream 
    dbAssetStream.CopyTo(dbFileStream);
   }

现在(版本2.0)我想更新我的数据库-> database.db(相同的名称,但不同的数据),所以我删除旧的database.db和下载新的database.db从ftp服务器,代码在这里:

public RelayCommand _yesupdate;
public RelayCommand YesUpdate => _yesupdate ??= new 
RelayCommand(() =>
    {
     // Get the object used to communicate with the server.
      FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + ftpInfo.IPV4 + @"/" + ftpInfo.FileName);

     request.Method = WebRequestMethods.Ftp.DownloadFile;
     //login
     request.Credentials = new NetworkCredential(ftpInfo.UseName, ftpInfo.Pwd);

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            string content = reader.ReadToEnd();
            content = content.ToUpperInvariant();
            string targetFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), ftpInfo.FileName);
        
            using FileStream outputStream = System.IO.File.OpenWrite(targetFile);
            using StreamWriter streamWriter = new StreamWriter(outputStream);
            streamWriter.Write(content);
           
            Alert.Execute(null);
       
            reader.Close();
            response.Close();

结果是我的APP无法获取数据,但ftp服务器显示我下载成功时,我的android设备无法找到database.db,只能看到一个无效文件(缓存)

jexiocij

jexiocij1#

需要对应上述数据库的处理方法(在public void Initialize())

public RelayCommand _yesupdate;
  public RelayCommand YesUpdate => _yesupdate ??= new RelayCommand(() =>
    {
        string serverPath = "ftp://" + ftpInfo.IPV4 + @"/" + 
        ftpInfo.FileName;

        string destinationFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ftpInfo.FileName);

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath);

        request.KeepAlive = true;
        request.UsePassive = true;
        request.UseBinary = true;

        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential(ftpInfo.UseName, ftpInfo.Pwd);
           // Read the file from the server & write to destination                
        using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
           //2.open resource file database -->resourcestream 
        using (Stream responseStream = response.GetResponseStream())

此处更改代码,因为在database.db是嵌入资源之前,我们也使用相同的方法打开//1.open目标文件到filestream,并将//3.copy资源流复制到filestream

using (var dbFileStream = new FileStream(destinationFile, 
 FileMode.OpenOrCreate))
        {
            responseStream.CopyTo(dbFileStream);

        }
    
        Alert.Execute(null);
    });

此处为https://learn.microsoft.com/zh-cn/dotnet/framework/network-programming/how-to-download-files-with-ftp

相关问题