如何按我想要的方式完成?
我真正想要的是什么:添加IP范围ex。启动IP文本框[192.168.1.1] - >网络掩码文本框[24] =(192.168.1.1 - 192.168.1.254)在数据库中添加了IP。
此外,我希望它检查用户是否使用正确的IP语法而不是随机数字,如果错误则显示消息。
我只是设法用2个文本框[start ip] - > [结束ip]然后[添加按钮]
if (CheckIPValid(txtstartip.Text)&&(CheckIPValid(txtendip.Text)))
{
if (!(txtstartip.Text.StartsWith("0"))&&(!(txtstartip.Text.StartsWith("0"))))
{
string startip = txtstartip.Text;
string endip = txtendip.Text;
string insertinip = "";
string usingip = startip.Substring(0, startip.LastIndexOf(".") + 1);
startip = startip.Substring(startip.LastIndexOf(".") + 1);
endip = endip.Substring(endip.LastIndexOf(".") + 1);
int endipCount = Convert.ToInt16(endip);
int startipCount = Convert.ToInt16(startip);
if (endipCount > startipCount)
{
int totalIpAdding = endipCount - startipCount;
int actualAddingIps = 0;
for (int i = startipCount; i <= endipCount; i++)
{
insertinip = "";
insertinip = usingip + "" + i.ToString();
if(checkDuplicateIP(insertinip))
{
MessageBox.Show("The IP "+ insertinip + " is already in Database");
}
else
{
query = "insert into tblIPAddress(IP_Address) values('" + insertinip + "')";
OleDbCommand cmd = new OleDbCommand(query);
cmd.Connection = myConn;
myConn.Open();
cmd.ExecuteNonQuery();
actualAddingIps++;
myConn.Close();
}
}
if(actualAddingIps==totalIpAdding )
MessageBox.Show("New IP Range Added");
else if(actualAddingIps > 0)
{
MessageBox.Show("IP Range Added");
}
else
{
MessageBox.Show("No IP Added");
}
}
else
{
MessageBox.Show("Invalid IP Range");
}
}
else
{
MessageBox.Show("InValid IP");
}
}
else
{
MessageBox.Show("InValid IP");
}
答案 0 :(得分:1)
搜索后我最终使用此代码,它解决了我的问题。
IPSegment ip = new IPSegment(txtip.Text.ToString(), SubNetMask());
Console.WriteLine(ip.NumberOfHosts);
Console.WriteLine(ip.NetworkAddress.ToIpString());
Console.WriteLine(ip.BroadcastAddress.ToIpString());
Console.WriteLine("===");
foreach (var host in ip.Hosts())
{
string query = "insert into tblIPAddress(IP_Address) values('" + host.ToIpString() + "')";
OleDbCommand cmd = new OleDbCommand(query);
cmd.Connection = myConn;
myConn.Open();
cmd.ExecuteNonQuery();
myConn.Close();
}