private void InitDataGridIPinfo() { string sql = "SELECT IP_Index, IP_Address, IP_EquipID, IP_Mask, IP_GateWay, IP_Name, IP_IsDefaultIP, Equip_Name FROM IPAddress INNER JOIN Equipments ON IP_EquipID = Equip_Index"; SqlDataReader dr = App.DBHelper.returnReader(sql); if (dr.HasRows) { ipInformationList.Clear(); while (dr.Read()) { string ipAddr = dr["IP_Address"].ToString(); string ipMask = dr["IP_Mask"].ToString(); string ipGateway = dr["IP_GateWay"].ToString(); IpAddress ip = new IpAddress(ipAddr); IpAddress mask = string.IsNullOrEmpty(ipMask)?null:new IpAddress(ipMask); IpAddress gateway = string.IsNullOrEmpty(ipGateway) ? null : new IpAddress(ipGateway); bool isDefault = bool.Parse(dr["IP_IsDefaultIP"].ToString()); int equipID = Convert.ToInt32(dr["IP_EquipID"]); IPInfor4DB ipInfo = new IPInfor4DB(ip, equipID, dr["Equip_Name"].ToString(), mask, gateway, dr["IP_Name"].ToString(), isDefault); ipInfo.IpIndex = Convert.ToInt32(dr["IP_Index"]); ipInformationList.Add(ipInfo); } dr.Close(); } dgIPInfo.ItemsSource = ipInformationList; }
public IPInfor4DB(IPInfor4DB second) { ip = second.ip; ipIndex = second.ipIndex; equipIndex = second.equipIndex; equipName = second.equipName; ipMask = second.ipMask; ipGateWay = second.ipGateWay; ipName = second.ipName; isDefaultIP = second.isDefaultIP; }
private void btnAddIP_Click(object sender, RoutedEventArgs e) { string strIpAddr = tbAddIP.Text.Trim(); IpAddress ipAddr, ipMask, ipGateWay; string strIpMask = tbAddMask.Text.Trim(); string strIpGateWay = tbAddGateway.Text.Trim(); string ipName = tbAddIPName.Text.Trim(); string ipEquipName = cbbAddIPEquipName.Text; if (ipEquipName.Equals("选择设备")) { MessageBox.Show("请选择一个设备"); return; } if (!IpAddress.IsIP(strIpAddr)) { MessageBox.Show("地址格式不正确"); return; } ipAddr = new IpAddress(strIpAddr); if (string.IsNullOrEmpty(strIpMask)) { ipMask = null; } else { if (!IpAddress.IsIP(strIpMask)) { MessageBox.Show("掩码应是一个IP地址格式"); return; } ipMask = new IpAddress(strIpMask); if (!ipMask.IsValidMask()) { MessageBox.Show("掩码格式不正确"); return; } } if (string.IsNullOrEmpty(strIpGateWay)) { ipGateWay = null; } else { if (!IpAddress.IsIP(strIpGateWay)) { MessageBox.Show("网关格式错误"); return; } ipGateWay = new IpAddress(strIpGateWay); } string getIDSql = "SELECT Equip_Index FROM Equipments WHERE Equip_Name = '" + ipEquipName + "'"; int equipID = Convert.ToInt32(App.DBHelper.returnScalar(getIDSql)); if (equipID <= 0) { MessageBox.Show("数据库中未找到相应的设备号"); return; } string insertSql = string.Format("INSERT INTO IPAddress(IP_Address, IP_EquipID, IP_Mask, IP_GateWay, IP_Name) VALUES('{0}',{1},'{2}','{3}','{4}')", strIpAddr, equipID, strIpMask, strIpGateWay, ipName); if (!App.DBHelper.ExecuteReturnBool(insertSql)) { MessageBox.Show("插入IP失败"); return; } IPInfor4DB ipInfo = new IPInfor4DB(ipAddr, equipID, ipEquipName, ipMask, ipGateWay, ipName, false); string getipIDSql = string.Format("SELECT IP_Index FROM IPAddress WHERE IP_EquipID = {0} AND IP_Address = '{1}'", equipID, strIpAddr); ipInfo.IpIndex = Convert.ToInt32(App.DBHelper.returnScalar(getipIDSql)); ipInformationList.Add(ipInfo); tbAddIP.Text = ""; tbAddIPName.Text = ""; tbAddMask.Text = ""; tbAddGateway.Text = ""; }