[英]Adding IP Address on Access Database C#
How to do it exactly the way I wanted it? 如何按照我想要的方式做呢?
What I really wanted : Adding IP Range ex. 我真正想要的是 :添加IP Range ex。 Starting IP textbox [192.168.1.1] -> Netmask textbox [24] = (192.168.1.1 - 192.168.1.254) IPs added in database. 起始IP文本框[192.168.1.1]-> Netmask文本框[24] =(192.168.1.1-192.168.1.254)IP已添加到数据库中。
Also I wanted it to check if the user put the right IP syntax not just a random numbers and show a message if it's wrong. 我还希望它检查用户是否输入了正确的IP语法,而不仅仅是一个随机数,如果错误,则显示一条消息。
I've only managed to do is this with 2 textbox [start ip] -> [end ip] then [add button] 我只能用2个文本框来做到这一点:[开始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");
}
After searching around I end up using this code and it solved my problem. 搜索后,我最终使用此代码,它解决了我的问题。
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.