我使用 WPF Toolkit 创建了一个饼图。我必须从我的数据库表中提取 Ram Usage 的值。在我的 MainWindow.xaml.cs 中,我创建了一个数据库连接字符串来建立数据库连接,如下所示:
public partial class MainWindow : Window
{
SqlConnection con;
SqlDataAdapter da_Ram=null;
DataSet ds_Ram = null;
public MainWindow()
{
InitializeComponent();
try
{
con = new SqlConnection("Data Source=User212;Initial Catalog=UsageDB;Persist Security Info=True);
con.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void myChart1_Loaded(object sender, RoutedEventArgs e)
{
string selectStatement = "select * from Used,Total from UsageDB.dbo.RamUsage";
da_Ram = new SqlDataAdapter(selectStatement, con);
ds_Ram = new DataSet();
// I am stuck here to proceed for filling my Dataset
}
} }
XAML 代码:
<loc1:Chart Name="myChart1" Background="YellowGreen" Foreground="DarkBlue"
Title="Ram Usage - Pie Chart" LegendTitle="Usage Details" Loaded="myChart1_Loaded" Margin="26,12,12,12">
<loc1:PieSeries Title="Ram Usage" ItemsSource="{Binding RamUsage}"
IndependentValueBinding="{Binding Path=Total}"
DependentValueBinding="{Binding Path=Used}">
</loc1:PieSeries>
</loc1:Chart>
这种方法正确吗?还是我必须创建一个单独的类来从数据源中检索值?谢谢。