比如,录入IP操作,在文本框内输入起始IP和结束IP,点击按钮那么所有IP就录入到数据库中。主要疑问有两个:
1、文本框如何控制输入。
2、如何不用循环将IP写入数据库中,用4个FOR循环?

解决方案 »

  1.   

    MASKTEXTBOX
    正则验证
    System.Net.IPAddress ipaddress = System.Net.IPAddress.Parse("");
    long ip= ipaddress.Address;IP++判断范围
      

  2.   

    问题1 正则来判断   正则表达式\d+\.\d+\.\d+\.\d+问题2 没想到更好的办法 批量插入。
      

  3.   

            string pstr = @"^(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])$";
            string ip1str = "111.111.111.111";
            string ip2str = "111.111.112.111";
            if (!Regex.IsMatch(ip1str, pstr) || !Regex.IsMatch(ip2str, pstr))
            {
                //错误
            }
            ip1str = Regex.Replace(ip1str, @"\.", m => { n--; return "*" + Math.Pow(255, n).ToString() + "+"; });
            ip2str = Regex.Replace(ip2str, @"\.", m => { n--; return "*" + Math.Pow(255, n).ToString() + "+"; });        int n = 4;
            long ip1 = Convert.ToInt64(new DataTable().Compute(ip1str, ""));
            n = 4;
            long ip2 = Convert.ToInt64(new DataTable().Compute(ip2str, ""));        for (long i = ip1; i <= ip2; i++)
            {
                long num = i;
                int i1 = (int)(num / (255 * 255 * 255));
                num -= i1 * 255 * 255 * 255;
                long i2 = (int)(num / (255 * 255));
                num -= i2 * 255 * 255;
                long i3 = (int)(num / 255);
                num -= i3 * 255;
                long i4 = num;            string ip = string.Format("{0}.{1}.{2}.{3}", i1, i2, i3, i4);
                Response.Write(ip + "<br>");
            }
      

  4.   

    System.Net.IPAddress ipaddress = System.Net.IPAddress.Parse(textbox1.Text);
    long startip= ipaddress.Address;
    ipaddress = System.Net.IPAddress.Parse(textbox2Text);
    long endip=ipaddress.Address;
    while(startip<endip)
    {}
     或System.Net.IPAddress x = System.Net.IPAddress.Parse("startIp");
      var xl = BitConverter.ToUInt64(x.GetAddressBytes(), 0);
      

  5.   

            //详细改了一编,应该不会报错了        string pstr = @"^(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])$";
            string ip1str = "192.222.0.1";
            string ip2str = "192.224.0.5";
            if (!Regex.IsMatch(ip1str, pstr) || !Regex.IsMatch(ip2str, pstr))
            {
                //错误
            }        string[] ip1s = ip1str.Split('.');
            string[] ip2s = ip2str.Split('.');        long ip1 = Convert.ToInt64(ip1s[0]) * 256 * 256 * 256 + Convert.ToInt64(ip1s[1]) * 256 * 256 + Convert.ToInt64(ip1s[2]) * 256 + Convert.ToInt64(ip1s[3]);
            long ip2 = Convert.ToInt64(ip2s[0]) * 256 * 256 * 256 + Convert.ToInt64(ip2s[1]) * 256 * 256 + Convert.ToInt64(ip2s[2]) * 256 + Convert.ToInt64(ip2s[3]);        for (long i = ip1; i <= ip2; i++)
            {
                long num = i;            long i1 = (int)(num / (256 * 256 * 256));
                num -= (long)i1 * 256 * 256 * 256;            long i2 = (int)(num / (256 * 256));
                num -= (long)i2 * 256 * 256;            long i3 = (int)(num / 256);
                num -= i3 * 256;            long i4 = num;            //最后一位为0的不要
                if (i4 == 0) continue;            string ip = string.Format("{0}.{1}.{2}.{3}", i1, i2, i3, i4);
                Response.Write(ip + "<br>");
            }