//右侧公司右键单击事件 //双击一次,传该公司的ID //到公司页面; private void btn_click2(object sender, RoutedEventArgs e) { TextBlock tx = sender as TextBlock; company company = new company(); //string connetStr = "server=127.0.0.1;port=3306;user=root;database=displayptf"; MySqlConnection conn = new MySqlConnection(connectStr); try { conn.Open(); string CP_name_sql = string.Format("select CP_ID from company where CP_name=('{0}')", tx.Text.ToString()); MySqlCommand cmd = new MySqlCommand(CP_name_sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { reader.Read(); company.cp_id = reader["CP_ID"].ToString();//返回该公司ID给company主页 } } catch (MySqlException ex) //数据库连接异常处理 { switch (ex.Number) { case 0: MessageBox.Show("Cannot connect to server. Contact administrator"); break; default: MessageBox.Show("其他数据库连接错误"); break; } } finally { conn.Close(); } company.cp_name = tx.Text.ToString(); this.NavigationService.Navigate(company); }
// 搜索栏点击事件 private void company_Search_Click(object sender, RoutedEventArgs e) { company company = new company(); string content = et1.Text; MySqlConnection connection = new MySqlConnection(connectStr); bool isID(string para) // 判断输入的是否是ID { Boolean flag = true; try { int.Parse(para); } catch { flag = false; } return(flag); } void searchMysql(string mysql, MySqlConnection conn) { try { conn.Open(); string sql = string.Format(mysql, content); MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { company.cp_name = reader["CP_name"].ToString(); company.cp_id = reader["CP_ID"].ToString(); this.NavigationService.Navigate(company); } else { MessageBox.Show("您查找的公司不存在,请检查后重新查询!"); } conn.Close(); } catch (MySqlException ex) { switch (ex.Number) { case 0: MessageBox.Show("连接数据库失败,请联系管理员"); break; default: MessageBox.Show("无效用户名或密码,请重试"); break; } } } if (isID(content)) { string mysql = "select * from company where CP_ID = ('{0}')"; searchMysql(mysql, connection); } else { string mysql = "select * from company where CP_name = ('{0}')"; searchMysql(mysql, connection); } }
// 公司列表点击、移入移出事件 private void TextBlock_Click(object sender, RoutedEventArgs e) { TextBlock tx = sender as TextBlock; company companyPage = new company(); companyPage.cp_name = tx.Text; this.NavigationService.Navigate(companyPage); //string connetStr = "server=127.0.0.1;port=3306;user=root;database=displayptf"; //MySqlConnection conn = new MySqlConnection(connectStr); //try //{ // conn.Open(); // string CP_name_sql = string.Format("select * FROM company WHERE EQP_ID = ('{0}')", EQP_ID.ToString()); // MySqlCommand cmd = new MySqlCommand(CP_name_sql, conn); // MySqlDataReader reader = cmd.ExecuteReader(); // EQP_list.Children.Clear(); // if (reader.HasRows) // { // int r = 0, c = 0; // while (reader.Read()&&r < 8 && c < 8) // { // //MessageBox.Show("这是一个测试"); // Image image = new Image(); // BitmapImage bi3 = new BitmapImage(); // bi3.BeginInit(); // bi3.UriSource = new Uri("/image/equipment/EQP_online.png", UriKind.Relative); // bi3.EndInit(); // image.Source = bi3; // image.Height = 108; // image.Margin = new Thickness(10, 0, 10, 0); // //image.SetValue(Grid.RowProperty, 0); // EQP_list.Children.Add(image); // Grid.SetColumn(image, c % 2); // Grid.SetRow(image, r / 2); // TextBlock tb_brand = new TextBlock(); // tb_brand.Text = "品牌"; // tb_brand.Height = 20; // tb_brand.Foreground = new SolidColorBrush(Colors.White); // tb_brand.Margin = new Thickness(118, 0,0,40); // EQP_list.Children.Add(tb_brand); // Grid.SetColumn(tb_brand, c % 2); // Grid.SetRow(tb_brand, r / 2); // TextBlock tb_category = new TextBlock(); // tb_category.Text = "类型"; // tb_category.Height = 20; // tb_category.Foreground = new SolidColorBrush(Colors.White); // tb_category.Margin = new Thickness(118, 10, 0, 0); // EQP_list.Children.Add(tb_category); // Grid.SetColumn(tb_category, c % 2); // Grid.SetRow(tb_category, r / 2); // TextBlock tb_address = new TextBlock(); // tb_address.Text = "产地"; // tb_address.Height = 20; // tb_address.Foreground = new SolidColorBrush(Colors.White); // tb_address.Margin = new Thickness(118, 55, 0, 0); // EQP_list.Children.Add(tb_address); // Grid.SetColumn(tb_address, c % 2); // Grid.SetRow(tb_address, r / 2); // ++c; // ++r; // } // } //} //catch (MySqlException ex) //数据库连接异常处理 //{ // switch (ex.Number) // { // case 0: // MessageBox.Show("Cannot connect to server. Contact administrator"); // break; // default: // MessageBox.Show("其他数据库连接错误"); // break; // } //} //finally //{ // conn.Close(); //} }