正在Access数据库C#上添加IP地址



如何按照我想要的方式进行?

我真正想要的:添加IP范围,例如启动IP文本框[1192.168.1.1]->Netmask文本框[24]=(192.168.1.1-192.168.1.254)在数据库中添加的IP。

此外,我希望它能检查用户是否输入了正确的IP语法,而不仅仅是一个随机数,并在错误时显示消息。

我只想用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");
            }

经过搜索,我最终使用了这段代码,它解决了我的问题。

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();
         }

最新更新