【问题标题】:Adding IP Address on Access Database C#在 Access 数据库 C# 上添加 IP 地址
【发布时间】:2015-11-30 14:58:23
【问题描述】:

如何完全按照我想要的方式去做?

我真正想要的:添加 IP 范围 ex。起始 IP 文本框 [192.168.1.1] -> 网络掩码文本框 [24] = (192.168.1.1 - 192.168.1.254) 数据库中添加的 IP。

我还希望它检查用户是否输入了正确的 IP 语法,而不仅仅是一个随机数,如果错误则显示一条消息。

我只设法用 2 个文本框 [start ip] -> [end ip] 然后 [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");
            }

【问题讨论】:

  • 你的问题是什么?
  • 如何完全按照我想要的方式进行操作?因为我设法做到的与我真正想要的不同。
  • 我认为,该链接会有所帮助。我一回家就试试。我家它支持至少 32 个倒计时的所有子网。谢谢
  • 只是想分享一下我最终使用了这段代码,它解决了我的问题。

标签: c#


【解决方案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();

         }

【讨论】:

    猜你喜欢
    • 2015-05-22
    • 2011-08-16
    • 2014-02-21
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2013-05-10
    相关资源
    最近更新 更多