WPF中怎么显示C#连接数据库

.Net技术 码拜 9年前 (2015-11-24) 909次浏览
刚刚学WPF不知道怎么利用WPF来显示C# 连接数据库当中的数据,求指导
最好能给代码,让本人学习下,谢谢了
解决方案:20分
前台代码:
<Window x:Class=”WpfApplication9.MainWindow”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”MainWindow” Height=”343″ Width=”564″>
<Grid>
<Button Content=”Button” Height=”39″ HorizontalAlignment=”Left” Margin=”427,107,0,0″ Name=”button1″ VerticalAlignment=”Top” Width=”75″ Click=”button1_Click” />
<DataGrid AutoGenerateColumns=”True” Name=”datagrid” CanUserAddRows=”False” Margin=”21,0,170,9″ />
</Grid>
</Window>
后台:
private void button1_Click(object sender, RoutedEventArgs e)
{
string  strConn = “Data Source=192.168.1.2;Initial Catalog=db;User ID=user;password=12345;Integrated Security=False”;
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
string strSql = “”;
SqlDataAdapter myCommand = null;
DataSet ds = null;
strSql = “SELECT *  FROM  table”;
myCommand = new SqlDataAdapter(strSql, strConn);
ds = new DataSet();
myCommand.Fill(ds, “table1”);
datagrid.ItemsSource = ds.Tables[0].DefaultView;
}
解决方案:20分
代码差不多吧,连接部分修改为mysql的连接
引入命名空间;看一下这例子,http://www.wpf8.net/news/Data/201303/43.html

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明WPF中怎么显示C#连接数据库
喜欢 (0)
[1034331897@qq.com]
分享 (0)