1.数据绑定:
数据绑定对被绑定对象有特殊要求,如果只是普通的get、set、属性的对象用在数据绑
定上无法双向绑定(只有Model值不会变或者Model变了不要求界面跟着变才可以使用普
通属性),一般要求类实现INotifyPropertyChanged接口;
需要绑定的属性上写=“{Binding Name}”,并且设定数据txt1.DataContext=p1;
<Grid>
<TextBox Text="{Binding Name}" Name="txtName"
HorizontalAlignment="Left" Height="23" Margin="47,52,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox Text="{Binding Age}" Name="txtAge"
HorizontalAlignment="Left" Height="23" Margin="47,122,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</Grid>
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
p1.Name = "lyc";
p1.Age = 22;
txtName.DataContext = p1;
txtAge.DataContext = p1;
}
-----------------------------------------
如果后台对象的值改变,界面要跟着改变要实现接口INotifyPropertyChanged;
//INotifyPropertyChanged是。net内置的接口
//数据绑定会检测DataContext是否实现了INotifyPropertyChanged
//如果实现了,就会监听PropertyChanged得知属性变化
class Person:INotifyPropertyChanged
{
private int age;
private String name;
public String Name
{
get { return name; }
set {
this.name = value;
if (PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs
("Name"));
}
}
}
public int Age
{
get { return age; }
set {
this.age = value;
if (PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs
("Age"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
2.数据上下文:
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
p1.Name = "lyc";
p1.Age = 22;
p1.Height=170;
p1.Gender = false;
//txtName.DataContext = p1;
//txtAge.DataContext = p1;
//txtHeight.DataContext = p1;
//cbGender.DataContext = p1;
//子控件和他的所有后代都默认继承父控件的DataContext
//子控件可以修改默认继承的datacontext
grid1.DataContext = p1;
}
<Grid Name="grid1">
<TextBox Text="{Binding Name}" Name="txtName"
HorizontalAlignment="Left" Height="23" Margin="161,60,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox Text="{Binding Age}" Name="txtAge"
HorizontalAlignment="Left" Height="23" Margin="161,109,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Click="Button_Click_1" Content="agePluse"
HorizontalAlignment="Left" Margin="354,113,0,0" VerticalAlignment="Top"
Width="75"/>
<Label Content="姓名" HorizontalAlignment="Left" Margin="86,60,0,0"
VerticalAlignment="Top" RenderTransformOrigin="1.543,0.647"/>
<Label Content="年龄" HorizontalAlignment="Left" Margin="86,113,0,0"
VerticalAlignment="Top" RenderTransformOrigin="-0.042,0.303"/>
<Label Content="性别" HorizontalAlignment="Left" Margin="86,181,0,0"
VerticalAlignment="Top"/>
<Label Content="身高" HorizontalAlignment="Left" Margin="86,243,0,0"
VerticalAlignment="Top" RenderTransformOrigin="1.081,0.367"/>
<CheckBox IsChecked="{Binding Gender}" x:Name="cbGender"
Content="CheckBox" HorizontalAlignment="Left" Margin="161,185,0,0"
VerticalAlignment="Top"/>
<TextBox Text="{Binding Height}" x:Name="txtHeight"
HorizontalAlignment="Left" Height="23" Margin="161,243,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</Grid>
3.ListBox的使用:
<ListBox DisplayMemberPath="Name" x:Name="lbPerson"
HorizontalAlignment="Left" Height="173" Margin="26,120,0,0"
VerticalAlignment="Top" Width="332"/>
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
List<Person> list = new List<Person>();
list.Add(new Person { Name="lyc",Age=12});
list.Add(new Person { Name = "wocao", Age = 82 });
//listbox显示的集合是ItemSource,不是dataContext
lbPerson.ItemsSource = list;
}
4.Demo:选中左边的每一个list,在右边的listbox显示选中项目的子项;
private void window1_Loaded_1(object sender, RoutedEventArgs e)
{
List<String> listPro = new List<string>();
listPro.Add("广东");
listPro.Add("湖南");
lbProvince.ItemsSource = listPro;
}
private void lbProvince_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
String pro=(String)lbProvince.SelectedItem;
//List<String> listCity = new List<string>();
if (pro=="广东")
{
List<String> listCity = new List<string>();
listCity.Add("广州");
listCity.Add("东莞");
listCity.Add("深圳");
lbCity.ItemsSource = listCity;
}
else if (pro == "湖南")
{
List<String> listCity = new List<string>();
listCity.Add("张家界");
listCity.Add("长沙");
listCity.Add("株洲");
lbCity.ItemsSource = listCity;
}
}
5.DataGrid:
//关闭自动生成列AutoGenerateColumns="False";
<Grid>
<DataGrid Name="dg1" AutoGenerateColumns="False">
</DataGrid>
</Grid>
、、、、
//禁止自动添加一行CanUserAddRows="False";IsReadOnly="True"设置只读;
<DataGrid Name="dg1" AutoGenerateColumns="False"
CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding
Name}" Header="姓名"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Age}" Header="年
龄"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
6.数据库基础:
主键有两种选用策略:业务主键(有业务意义);逻辑主键(没有业务意义,完全给程
序看的);推荐使用逻辑主键;
select * from T_Student where Name='lyc';
insert into T_Student(Name,Age)values('nima',23)
update T_Student set Age=Age+2
7.数据检索
改别名:select Name as N1,Age as A1 from T_Student;
函数:select MAX(age)from T_Student;\count()\min()\AVG()\
通配符过滤:like、
order by (asc、desc),where 要放order by 之前;
8.ADO.Net基础:
sql server验证的两种方式:
windows身份验证:本机连接或者受信的局域网连接;
sql server验证:使用用户名密码;
sa用户:sqlserver的最高权限管理员账户,启用方法:根节点-安全性--sa-常规中修
改密码,默认是强制复杂密码,可以取消,状态的登录勾选启用。基于安全考虑,不要
启用sa账户,而是针对数据库建立数据库的专用受限账户;
连接字符串:Data Source=.;Initial Catalog=MyTest;User ID=sa;Password=sa
使用using这种方式会自动进行资源回收;
(1)
private void btnLink_Click(object sender, RoutedEventArgs e)
{
//using (SqlConnection conn = new SqlConnection
(@"server=.;database=booksystem;uid=sa;pwd=sa"))
//{
// conn.Open();
// MessageBox.Show(conn.State.ToString());
//}
using (SqlConnection conn = new SqlConnection(@"Data
Source=.;Initial Catalog=MyTest;User ID=sa;Password=sa"))
{
conn.Open();
MessageBox.Show(conn.State.ToString());
using(SqlCommand com=conn.CreateCommand()){
//com.CommandText = "insert into T_Student
(Name,Age)values('院方',23)";
//com.ExecuteNonQuery();
//ExecuteNonQuery一般用来执行Update,delete,insert语句
//ExecuteScalar一般用来执行有且只有一行一列返回值的sql语
句
com.CommandText = "select count(*)from T_Student";
int count = (int)com.ExecuteScalar();
MessageBox.Show(count.ToString());
}
}
MessageBox.Show("over");
}
/
(2)using (SqlConnection conn = new SqlConnection(@"Data
Source=.;Initial Catalog=booksystem;User ID=sa;Password=sa"))
{
conn.Open();
MessageBox.Show(conn.State.ToString());
}
//获得当前插入的行的自动增长的id,output inserted.Id:
insert into T_Student(Name,Age) output inserted.Id values('worinimei',23);
注意:
sql server |||| .net/c#
int int
bigint long
varchar string
bit bool
com.CommandText = "select Id from T_Student where
Name='lyc'";
long count = (long)com.ExecuteScalar();//Id是bigint--
>long
MessageBox.Show(count.ToString());
9.
数据绑定对被绑定对象有特殊要求,如果只是普通的get、set、属性的对象用在数据绑
定上无法双向绑定(只有Model值不会变或者Model变了不要求界面跟着变才可以使用普
通属性),一般要求类实现INotifyPropertyChanged接口;
需要绑定的属性上写=“{Binding Name}”,并且设定数据txt1.DataContext=p1;
<Grid>
<TextBox Text="{Binding Name}" Name="txtName"
HorizontalAlignment="Left" Height="23" Margin="47,52,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox Text="{Binding Age}" Name="txtAge"
HorizontalAlignment="Left" Height="23" Margin="47,122,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</Grid>
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
p1.Name = "lyc";
p1.Age = 22;
txtName.DataContext = p1;
txtAge.DataContext = p1;
}
-----------------------------------------
如果后台对象的值改变,界面要跟着改变要实现接口INotifyPropertyChanged;
//INotifyPropertyChanged是。net内置的接口
//数据绑定会检测DataContext是否实现了INotifyPropertyChanged
//如果实现了,就会监听PropertyChanged得知属性变化
class Person:INotifyPropertyChanged
{
private int age;
private String name;
public String Name
{
get { return name; }
set {
this.name = value;
if (PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs
("Name"));
}
}
}
public int Age
{
get { return age; }
set {
this.age = value;
if (PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs
("Age"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
2.数据上下文:
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
p1.Name = "lyc";
p1.Age = 22;
p1.Height=170;
p1.Gender = false;
//txtName.DataContext = p1;
//txtAge.DataContext = p1;
//txtHeight.DataContext = p1;
//cbGender.DataContext = p1;
//子控件和他的所有后代都默认继承父控件的DataContext
//子控件可以修改默认继承的datacontext
grid1.DataContext = p1;
}
<Grid Name="grid1">
<TextBox Text="{Binding Name}" Name="txtName"
HorizontalAlignment="Left" Height="23" Margin="161,60,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox Text="{Binding Age}" Name="txtAge"
HorizontalAlignment="Left" Height="23" Margin="161,109,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Click="Button_Click_1" Content="agePluse"
HorizontalAlignment="Left" Margin="354,113,0,0" VerticalAlignment="Top"
Width="75"/>
<Label Content="姓名" HorizontalAlignment="Left" Margin="86,60,0,0"
VerticalAlignment="Top" RenderTransformOrigin="1.543,0.647"/>
<Label Content="年龄" HorizontalAlignment="Left" Margin="86,113,0,0"
VerticalAlignment="Top" RenderTransformOrigin="-0.042,0.303"/>
<Label Content="性别" HorizontalAlignment="Left" Margin="86,181,0,0"
VerticalAlignment="Top"/>
<Label Content="身高" HorizontalAlignment="Left" Margin="86,243,0,0"
VerticalAlignment="Top" RenderTransformOrigin="1.081,0.367"/>
<CheckBox IsChecked="{Binding Gender}" x:Name="cbGender"
Content="CheckBox" HorizontalAlignment="Left" Margin="161,185,0,0"
VerticalAlignment="Top"/>
<TextBox Text="{Binding Height}" x:Name="txtHeight"
HorizontalAlignment="Left" Height="23" Margin="161,243,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</Grid>
3.ListBox的使用:
<ListBox DisplayMemberPath="Name" x:Name="lbPerson"
HorizontalAlignment="Left" Height="173" Margin="26,120,0,0"
VerticalAlignment="Top" Width="332"/>
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
List<Person> list = new List<Person>();
list.Add(new Person { Name="lyc",Age=12});
list.Add(new Person { Name = "wocao", Age = 82 });
//listbox显示的集合是ItemSource,不是dataContext
lbPerson.ItemsSource = list;
}
4.Demo:选中左边的每一个list,在右边的listbox显示选中项目的子项;
private void window1_Loaded_1(object sender, RoutedEventArgs e)
{
List<String> listPro = new List<string>();
listPro.Add("广东");
listPro.Add("湖南");
lbProvince.ItemsSource = listPro;
}
private void lbProvince_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
String pro=(String)lbProvince.SelectedItem;
//List<String> listCity = new List<string>();
if (pro=="广东")
{
List<String> listCity = new List<string>();
listCity.Add("广州");
listCity.Add("东莞");
listCity.Add("深圳");
lbCity.ItemsSource = listCity;
}
else if (pro == "湖南")
{
List<String> listCity = new List<string>();
listCity.Add("张家界");
listCity.Add("长沙");
listCity.Add("株洲");
lbCity.ItemsSource = listCity;
}
}
5.DataGrid:
//关闭自动生成列AutoGenerateColumns="False";
<Grid>
<DataGrid Name="dg1" AutoGenerateColumns="False">
</DataGrid>
</Grid>
、、、、
//禁止自动添加一行CanUserAddRows="False";IsReadOnly="True"设置只读;
<DataGrid Name="dg1" AutoGenerateColumns="False"
CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding
Name}" Header="姓名"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Age}" Header="年
龄"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
6.数据库基础:
主键有两种选用策略:业务主键(有业务意义);逻辑主键(没有业务意义,完全给程
序看的);推荐使用逻辑主键;
select * from T_Student where Name='lyc';
insert into T_Student(Name,Age)values('nima',23)
update T_Student set Age=Age+2
7.数据检索
改别名:select Name as N1,Age as A1 from T_Student;
函数:select MAX(age)from T_Student;\count()\min()\AVG()\
通配符过滤:like、
order by (asc、desc),where 要放order by 之前;
8.ADO.Net基础:
sql server验证的两种方式:
windows身份验证:本机连接或者受信的局域网连接;
sql server验证:使用用户名密码;
sa用户:sqlserver的最高权限管理员账户,启用方法:根节点-安全性--sa-常规中修
改密码,默认是强制复杂密码,可以取消,状态的登录勾选启用。基于安全考虑,不要
启用sa账户,而是针对数据库建立数据库的专用受限账户;
连接字符串:Data Source=.;Initial Catalog=MyTest;User ID=sa;Password=sa
使用using这种方式会自动进行资源回收;
(1)
private void btnLink_Click(object sender, RoutedEventArgs e)
{
//using (SqlConnection conn = new SqlConnection
(@"server=.;database=booksystem;uid=sa;pwd=sa"))
//{
// conn.Open();
// MessageBox.Show(conn.State.ToString());
//}
using (SqlConnection conn = new SqlConnection(@"Data
Source=.;Initial Catalog=MyTest;User ID=sa;Password=sa"))
{
conn.Open();
MessageBox.Show(conn.State.ToString());
using(SqlCommand com=conn.CreateCommand()){
//com.CommandText = "insert into T_Student
(Name,Age)values('院方',23)";
//com.ExecuteNonQuery();
//ExecuteNonQuery一般用来执行Update,delete,insert语句
//ExecuteScalar一般用来执行有且只有一行一列返回值的sql语
句
com.CommandText = "select count(*)from T_Student";
int count = (int)com.ExecuteScalar();
MessageBox.Show(count.ToString());
}
}
MessageBox.Show("over");
}
/
(2)using (SqlConnection conn = new SqlConnection(@"Data
Source=.;Initial Catalog=booksystem;User ID=sa;Password=sa"))
{
conn.Open();
MessageBox.Show(conn.State.ToString());
}
//获得当前插入的行的自动增长的id,output inserted.Id:
insert into T_Student(Name,Age) output inserted.Id values('worinimei',23);
注意:
sql server |||| .net/c#
int int
bigint long
varchar string
bit bool
com.CommandText = "select Id from T_Student where
Name='lyc'";
long count = (long)com.ExecuteScalar();//Id是bigint--
>long
MessageBox.Show(count.ToString());
9.