📄 frm-computeadd.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 计算机及IP管理
{
public partial class frm_computeAdd : Form
{
UserCls cls = new UserCls();
DataSet myds;
public frm_computeAdd()
{
InitializeComponent();
}
//画页载入
private void frm_computeAdd_Load(object sender, EventArgs e)
{
//窗体的启动位置
this.StartPosition = FormStartPosition.WindowsDefaultLocation;
//给下拉列赋值
string strdepart = "select distinct name from t_department order by name";
string strnetwork = "select distinct network from t_switch where network<>'' order by network";
bool isdepart, isnetwork;
//部门
isdepart = cls.GetDataToComboBox(strdepart, txtdepartment, "t_department", "name", "name");
//网段
isnetwork = cls.GetDataToComboBox(strnetwork, txtnetwork, "t_switch", "network", "network");
txtnetwork.Text = "192.168.8";
for (int i = 1; i < 27; i++)
{
txtport.Items.Add(i);
}
txtport.Text = "";
//区域
cls.GetDataToComboBox("select distinct scope from t_switch where scope <>'' order by scope", txtscope, "t_switch", "scope", "scope");
txtscope.Text = "";
}
//关闭
private void bntCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (txtcomputemodel.Text.Trim() == "")
{
MessageBox.Show("计算机型号不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (txtcharge.Text.Trim() == "")
{
MessageBox.Show("负责人不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (txtdepartment.Text.Trim() == "")
{
MessageBox.Show("请选择部门名称", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (txtisbind.Text.Trim() == "请选择")
{
MessageBox.Show("请选择绑定与否", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
StringBuilder strsql = new StringBuilder();
strsql.Append("insert into t_computer_ip(network,ip,mac,scope,port,computermodel,department,charge,isbind,remarks)");
strsql.Append("values('");
strsql.Append(txtnetwork.Text.Trim());
strsql.Append("','");
strsql.Append(txtIP.Text.Trim());
strsql.Append("','");
strsql.Append(txtMAC.Text.Trim());
strsql.Append("','");
strsql.Append(txtscope.Text.Trim());
strsql.Append("','");
strsql.Append(txtport.Text.Trim());
strsql.Append("','");
strsql.Append(txtcomputemodel.Text.Trim());
strsql.Append("','");
strsql.Append(txtdepartment.Text.Trim());
strsql.Append("','");
strsql.Append(txtcharge.Text.Trim());
strsql.Append("','");
strsql.Append(txtisbind.Text.Trim());
strsql.Append("','");
strsql.Append(txtRemark.Text.Trim());
strsql.Append("')");
if (cls.ExecSqlString(strsql.ToString()))
{
MessageBox.Show("添加成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else
{
MessageBox.Show("添加失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//自动获取IP
private void btnAccessIP_Click(object sender, EventArgs e)
{
StringBuilder strSql = new StringBuilder("select ip from t_computer_ip where network ='");
strSql.Append(txtnetwork.Text.Trim());
strSql.Append("'");
myds = cls.GetDataSet(strSql.ToString(), "t_computer_ip");//执行查询
if (myds.Tables["t_computer_ip"].Rows.Count <= 0)//说明此网段没有分配IP
{
MessageBox.Show("恭喜您,您是此网段的第一个用户!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
if (txtnetwork.Text.Trim() == "192.168.8")
{
txtIP.Text = "31";
}
else
{
txtIP.Text = "10";
}
}
else//说明此网段已经有IP分配,那么就在此网段内寻找还没有使用的IP
{
StringBuilder strIp = new StringBuilder();
for (int j = 0; j < myds.Tables["t_computer_ip"].Rows.Count;j++ )
{
strIp.Append(myds.Tables["t_computer_ip"].Rows[j]["ip"].ToString());
strIp.Append(";");
}
//IP地址的分配分两种情况 1、是按自然数的顺序分配的,是间隔分配的。
//2、是间隔随意分配的。要综合这两种情把这个网段中没有分配过的IP地址找出来。
int i = 31;//因为每个网段的可分配IP是从31开始的
for (; i <= myds.Tables["t_computer_ip"].Rows.Count + 31; i++)//此部分是从间隔分配的地址中把没有用到的IP找出来。
{
if (strIp.ToString().IndexOf(i.ToString()) >= 0)//说这个数值的IP已经分配
{
continue;
}
else
{
if (i <= 255)//说明没有超过一个网段的最大IP分配数量
{
txtIP.Text = i.ToString();//IP已经找到
return;
}
else//此网段内的有效IP已经分配完毕,没有可用的IP
{
MessageBox.Show("很抱歉此网段已没有可用的IP地址!请您选择其它网段。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
if (i-1 == myds.Tables["t_computer_ip"].Rows.Count && i< 255)//这说明已经分配的IP是按自然数的排列顺序分配的,由小到大没有间隔。
{
txtIP.Text = i.ToString();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -