0

您好,我有以下课程,我正在使用 WPF 中的 Linq to Sql 从 sql server 下载 Excel 文件。我在使该方法起作用时遇到问题。

public class Tables
            {
                public Guid Id { get; set; }
                public byte[] Data { get; set; }
                public string Notes{ get; set; }            
            }

财产

public ObservableCollection<Tables> Table
        {
            get
            {
                return mTables;
            }
        }

方法(错误 - fileBytes 未出现在当前上下文中)

 private void executeSaveAttachment(object parameter)
            {
                //Enables the apperance of a Dialog, where the user can specify where to save the file
                SaveFileDialog textDialog = new SaveFileDialog();

                //save the file in a bite array

               // byte[] fileBytes = Table.ToList().ForEach(p => p.Data);
                Table.ToList().ForEach(p =>
                {
                    byte[] fileBytes = p.Data;
                });

                //Open dialog where the user determines where to save the file.
                bool? result = textDialog.ShowDialog();
                if (result == true)
                {
                    using (Stream fs = (Stream)textDialog.OpenFile())
                    {
                        fs.Write(fileBytes, 0, fileBytes.Length);
                        fs.Close();
                    }
                }
            }
4

2 回答 2

1

您收到错误是因为fileBytes仅存在于传递给 ForEach 的委托中。试试这个:

private void executeSaveAttachment(object parameter)
{
    using (var dlg = new SaveFileDialog())
    {
        foreach (var table in Table)
        {
            if (dlg.ShowDialog() ?? false)
            {
                File.WriteAllBytes(dlg.FileName, table.Data)
            }
        }
    }
}
于 2013-06-05T20:11:46.727 回答
0

对于 WPF

private void executeSaveAttachment(object parameter)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            {
                foreach (var table in Table)
                {
                    if (dlg.ShowDialog() ?? false)
                    {
                        File.WriteAllBytes(dlg.FileName, table.Data);
                    }
                }
            }
        }  
于 2013-06-05T20:48:15.837 回答