Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF C将结果缓存到文本,然后缓存到列表框_C#_Wpf_Xaml_Caching_Listbox - Fatal编程技术网

C# WPF C将结果缓存到文本,然后缓存到列表框

C# WPF C将结果缓存到文本,然后缓存到列表框,c#,wpf,xaml,caching,listbox,C#,Wpf,Xaml,Caching,Listbox,我在玩这个。我将在应用程序中的对象上实现某种类型的缓存,但我想全面了解它是如何工作的。显示的所有数据均为假数据和测试数据。没有使用PHI 目标:将SQL表结果缓存到本地计算机上的文本文件中 什么有效? 从SQL表创建的文本文件。 正在读取应用程序中的文本文件 什么不起作用? 在列表框或组合框中显示数据 发生了什么事? 文本文件整洁干净。每行1条记录。没有多余的空间。当我将文本文件读入MessageBox时,宾果。漂亮、整洁、易读。当我将文本显示到组合框或列表框中时,每个全名中的每个字母都是一个新

我在玩这个。我将在应用程序中的对象上实现某种类型的缓存,但我想全面了解它是如何工作的。显示的所有数据均为假数据和测试数据。没有使用PHI

目标:将SQL表结果缓存到本地计算机上的文本文件中

什么有效? 从SQL表创建的文本文件。 正在读取应用程序中的文本文件

什么不起作用? 在列表框或组合框中显示数据

发生了什么事? 文本文件整洁干净。每行1条记录。没有多余的空间。当我将文本文件读入MessageBox时,宾果。漂亮、整洁、易读。当我将文本显示到组合框或列表框中时,每个全名中的每个字母都是一个新行

XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <TextBlock Text="Name of Patient - " HorizontalAlignment="Center"
               VerticalAlignment="Center"
               Grid.Row="0" Grid.Column="0" />
    <TextBox Name="FullName" VerticalContentAlignment="Center"
             Text="{Binding Path=EmployeeName, Mode=TwoWay}"
             Height="30" Width="150" HorizontalAlignment="Center"
             VerticalAlignment="Center" Grid.Row="0" Grid.Column="1" />
    <Button Name="Search" Content="Search" Click="Search_Click"
            Grid.Row="0" Grid.Column="2" Height="30" Width="150" />
    <ListBox Height="200" Width="200" Name="LbFullName"
             ItemsSource="{Binding}" Grid.Row="1" Grid.Column="0"
             Grid.ColumnSpan="3" />
</Grid>
C:

public partial class MainWindow
{
    //cache object
    private readonly ObjectCache _cache = MemoryCache.Default;

    public MainWindow()
    {
        InitializeComponent();
        var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        string dbFile = path + @"\\Razor Sharp Technology\\PharmaSYS Management Suite\\Files\\PatientNames.txt";
        SendSqlToText(dbFile);
    }

    private void Search_Click(object sender, RoutedEventArgs e)
    {
        // ReSharper disable once RedundantAssignment
        if (!(_cache["cachecontents"] is string cacheContents))
        {
            var policy = new CacheItemPolicy {AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(10.0)};

            var filePaths = new List<string>();
            var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            filePaths.Add(path + "\\Razor Sharp Technology\\PharmaSYS Management Suite\\Files\\PatientNames.txt");

            policy.ChangeMonitors.Add(new
                                          HostFileChangeMonitor(filePaths));

            cacheContents =
                File.ReadAllText(path + "\\Razor Sharp Technology\\PharmaSYS Management Suite\\Files\\PatientNames.txt");

            _cache.Set("cachecontents", cacheContents, policy);
            LbFullName.DataContext = cacheContents;
            MessageBox.Show(cacheContents);
        }
    }

    private void SendSqlToText(string dbFile)
    {
        var conString = ConfigurationManager.ConnectionStrings["Dbconn"].ConnectionString;
        using (var sqlConnection = new SqlConnection(conString))
        {
            var cmd = "SELECT FullName FROM[dbo].[PatientData]";
            var command = new SqlCommand(cmd, sqlConnection);
            sqlConnection.Open();
            SqlDataReader sqlReader = command.ExecuteReader();
            using (StreamWriter file = new StreamWriter(dbFile, false))
            {
                while (sqlReader.Read())
                {
                    file.WriteLine(sqlReader["FullName"]);
                }
            }
        }
    }
}
你可以用。这样,您就可以将每一行作为字符串[]中的一个字符串,并可以绑定到该字符串上

步骤1 步骤二 将组合框的ItemSource绑定到您在步骤1中用行填充的字符串[]


使用ItemSource={Binding}

ItemSource={Binding}将ItemSource设置为LbFullName.DataContext中的字符串,即cacheContents。ItemsSource需要一个IEnumerable,字符串是IEnumerable,因此您可以看到单个字符。如果我可以帮助解决您的问题,如果您接受答案,我将非常高兴:
var strings = File.ReadAllLines(path + "\\Razor Sharp Technology\\PharmaSYS Management Suite\\Files\\PatientNames.txt");