现在可以利用前面的博文中的一些技术来实现一些小功能案例,例如带增删改查的小型管理窗口。下面将会带来一些主要步骤的详解:
①首先还是创建数据库表,这个没什么好说的,直接上图:
②然后定义一个与这个数据库表类型一直的类,名为Customer.cs:
class Customer { public int Id { get; set; } public string Name { get; set; } public DateTime? Birthday { get; set; } public string Address { get; set; } public string TelNum { get; set; } public int CustLevel { get; set; } }
③开始写三层架构里的DAL层,写在一个CustomerDAL.cs文件中,其中包括InsertCustomer(),DeleteById(),UpdateById(),GetById(),GetAll()这几个函数(增删改查),具体代码如下:
class CustomerDAL { public Customer GetById(int id) { DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Review where Id = @Id", new SqlParameter("@Id",id)); if (dt.Rows.Count <= 0) { return null; } else if (dt.Rows.Count > 1) { throw new Exception("多条数据"); } else { DataRow dr = dt.Rows[0]; Customer cust = new Customer(); cust.Id = id; cust.Name = (string)dr["Name"]; cust.Birthday = (DateTime)DBValue.FromDBValue(dr["Birthday"]); cust.Address = (string)dr["Address"]; cust.TelNum = (string)dr["TelNum"]; cust.CustLevel = (int)dr["CustLevel"]; return cust; } } public void DeleteById(int id) { SqlHelper.ExecuteNonQuery("delete from T_Review where Id = @Id", new SqlParameter("@Id",id)); } public void UpdateById(Customer customer) { SqlHelper.ExecuteNonQuery("update T_Review set Name =@Name,Birthday=@Birthday,Address=@Address,TelNum=@TelNum,CustLevel=@CustLevel where Id=@Id", new SqlParameter("@name", customer.Name), new SqlParameter("@Birthday", DBValue.ToDBValue(customer.Birthday)), new SqlParameter("@Address", customer.Address), new SqlParameter("@TelNum", customer.TelNum), new SqlParameter("@CustLevel", customer.CustLevel), new SqlParameter("@Id", customer.Id)); } public void InsertCustomer(Customer customer) { SqlHelper.ExecuteNonQuery("Insert into T_Review (Name,Birthday,Address,TelNum,CustLevel) values (@Name,@Birthday,@Address,@TelNum,@CustLevel)", new SqlParameter("@Name",customer.Name), new SqlParameter("@Birthday",DBValue.ToDBValue(customer.Birthday)), new SqlParameter("@Address",customer.Address), new SqlParameter("@TelNum",customer.TelNum), new SqlParameter("@CustLevel",customer.CustLevel)); } public List<Customer> GetAll() { DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Review"); List<Customer> list = new List<Customer>(); if (dt.Rows.Count <= 0) { return null; } else { foreach (DataRow dr in dt.Rows) { Customer cust = new Customer(); cust.Id = (int)dr["Id"]; cust.Name = (string)dr["Name"]; cust.Birthday = (DateTime?)DBValue.FromDBValue(dr["Birthday"]); cust.Address = (string)dr["Address"]; cust.TelNum = (string)dr["TelNum"]; cust.CustLevel = (int)dr["CustLevel"]; list.Add(cust); } return list; } }
◇这时候“后台”就算是写好了,接下来就是开始调用了。
④新建一个窗口,名为ListWindow.xaml,在这个窗口中加入一个ToolBar和一个DataGrid,分别用来存放按钮和显示数据的,具体的xaml代码如下:
<Grid> <ToolBar Height="26" HorizontalAlignment="Left" Name="toolBar1" VerticalAlignment="Top" Width="278" > <Button Content="添加" Name="addBtn" Click="addBtn_Click"> </Button> <Button Content="修改" Name="updateBtn" Click="updateBtn_Click"> </Button> <Button Content="删除" Name="deleteBtn" Click="deleteBtn_Click"> </Button> <Button Content="查询" Name="selectBtn" Click="selectBtn_Click"> </Button> </ToolBar> <DataGrid AutoGenerateColumns="False" Height="600" HorizontalAlignment="Left" Margin="0,23,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="600" FlowDirection="LeftToRight"> <DataGrid.Columns> <DataGridTextColumn Width="100" Header="编号" Binding="{Binding Id}"/> <DataGridTextColumn Width="100" Header="姓名" Binding="{Binding Name}"/> <DataGridTextColumn Width="100" Header="生日" Binding="{Binding Birthday}"/> <DataGridTextColumn Width="100" Header="住址" Binding="{Binding Address}"/> <DataGridTextColumn Width="100" Header="电话" Binding="{Binding TelNum}"/> <DataGridTextColumn Width="100" Header="等级" Binding="{Binding CustLevel}"/> </DataGrid.Columns> </DataGrid> </Grid>
⑤然后再创建一个EditUIWindow.xaml窗口,这个窗口用来添加或者修改ListWindow.xaml中的DataGrid所绑定的数据源得数据:
⑥接下来先贴上ListUIWindow.cs的代码:
public partial class ListUIWindow : Window { public ListUIWindow() { InitializeComponent(); } private void addBtn_Click(object sender, RoutedEventArgs e) { AddCustWindow addCW = new AddCustWindow(); addCW.ShowDialog(); } private void Window_Loaded(object sender, RoutedEventArgs e) { CustomerDAL cDAL = new CustomerDAL(); dataGrid1.ItemsSource = cDAL.GetAll(); } private void updateBtn_Click(object sender, RoutedEventArgs e) { Customer customer = (Customer)dataGrid1.SelectedItem; AddCustWindow addCW = new AddCustWindow(); addCW.nameTxt.Text = customer.Name; addCW.datePicker1.SelectedDate = customer.Birthday; addCW.addrTxt.Text = customer.Address; addCW.telnumTxt.Text = customer.TelNum; addCW.levelTxt.Text = customer.CustLevel.ToString(); addCW.addBtn.Content = "保存"; addCW.ShowDialog(); } private void deleteBtn_Click(object sender, RoutedEventArgs e) { Customer customer=(Customer)dataGrid1.SelectedItem; int SelectionId = customer.Id; CustomerDAL cDAL = new CustomerDAL(); cDAL.DeleteById(SelectionId); MessageBox.Show("删除成功"); dataGrid1.ItemsSource = cDAL.GetAll(); } private void selectBtn_Click(object sender, RoutedEventArgs e) { CustomerDAL cDAL = new CustomerDAL(); dataGrid1.ItemsSource = cDAL.GetAll(); } }
⑦最后便是,EditUIWindow.cs的代码:
public partial class AddCustWindow : Window { public AddCustWindow() { InitializeComponent(); } private void addBtn_Click(object sender, RoutedEventArgs e) { Customer customer = new Customer(); customer.Name = nameTxt.Text; customer.Birthday = (DateTime?)datePicker1.SelectedDate; customer.Address = addrTxt.Text; customer.TelNum = telnumTxt.Text; customer.CustLevel = Convert.ToInt32(levelTxt.Text); CustomerDAL cDAL = new CustomerDAL(); if ((string)addBtn.Content == "添加") { cDAL.InsertCustomer(customer); } else { cDAL.UpdateById(customer); } MessageBox.Show(addBtn.Content + "成功"); ListUIWindow LUIW = new ListUIWindow(); LUIW.dataGrid1.ItemsSource = cDAL.GetAll(); } private void button2_Click(object sender, RoutedEventArgs e) { this.Close(); } }
今天好累。。。怎么变成贴代码了,代码很简单,大家都能看得懂,一起共勉。。。头晕的要命,抱歉。。。。