简体   繁体   中英

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. Starting IP textbox [192.168.1.1] -> Netmask textbox [24] = (192.168.1.1 - 192.168.1.254) IPs added in database.

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.

I've only managed to do is this with 2 textbox [start ip] -> [end ip] then [add button]

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

         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM