请问楼主是要ip1与ip2共同确认吗?

解决方案 »

  1.   

    ip1和ip2是如:192.168.0.1形式的 还是(192*256^3+168*256^2+0*256+1)-1数值 ??
    应该是数值吧 如果不是用上面算法转换一下
    下面取数据算法如下,这里假设ip1和ip2的数据是上面的第二种情况
    string ip=textbox.Text;
    if(ip=="127.0.0.1")
    ip="192.168.0.1";
    char[] p=new char[]{'.'};
    string[] ipstr=new string[4];
    ipstr=ip.Split(p);
    long Lip=0;
    for(int j=0;j<ipstr.Length;j++)
    {
    Lip +=Convert.ToInt32(ipstr[j])*Convert.ToInt32(Math.Pow(256,ipstr.Length-1-j));
    }
    Lip -=1;
    string sql="select country,city from address where ip1<='"+Lip.ToString()+"' and ip2>='"+Lip.ToString()+"'";
    SqlConnection sqlcon=new SqlConnection("...");
    sqlcon.Open();
    SqlCommand sqlcom=new SqlCommand(sql,slqcon);
    SqlDataReader dr= sqlcom.ExecuteReader();
    if(dr.HasRows)
    {
    dr.Read();
    string country=dr["country"].ToString();//<---------这里
    string city=dr["city"].ToString();//<---------这里
    }
    dr.Close();
    sqlcon.Close();
      

  2.   

    to:44404(杨雪) 
    不是共同确认 是让输入的ip介于ip2与ip2之间 如果为真的话说明ip合法
    to:wj2929(*ヤRěйヤ*)
    是数值SqlConnection sqlcon=new SqlConnection("...");
    这是数据库连接吗?
      

  3.   

    SqlConnection sqlcon=new SqlConnection("...");
    我省略了连接字符串
    详细点如下:
    SqlConnection sqlcon=new SqlConnection("server=(local);database=;uid=;pwd=;");我的这是SQLServer的 ,你要是用的其他数据库,要更改成相应的
      

  4.   

    我也是用的sql server
    我已经把 SqlConnection sqlcon=new SqlConnection("..."); 改成要连接的数据库了
    可出现了下面这样的一个问题:

    Form1.cs(221): 名称“slqcon”在类或命名空间“IPQuary.Form1”中不存在
      
    这是怎么回事??
      

  5.   


    拼错了
    不好意思
    slqcon-----------〉sqlcon我当然有自己的数据库类了 ,但我写上你们也不知道什么,所以就改成SqlConnection 了
      

  6.   

    建议写到Web.config里面
    那样方便些
      

  7.   

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {   
    //进行非空验证
    if(textBox1.Text =="")
    {
    MessageBox.Show ("您还没有输入IP地址,请输入!","小鱼游游提示");
    textBox1.Focus ();
    return;
    }
    else
    {
    string ip=textBox1.Text;
    if(ip=="127.0.0.1")
    ip="192.168.0.1";
    char[] p=new char[]{'.'};
    string[] ipstr=new string[4];
    ipstr=ip.Split(p);
    long Lip=0;
    for(int j=0;j<ipstr.Length;j++)
    {
    Lip +=Convert.ToInt32(ipstr[j])*Convert.ToInt32(Math.Pow(256,ipstr.Length-1-j));
    }
    Lip -=1;
    string sql="select country,city from address where ip1<='"+Lip.ToString()+"' and ip2>='"+Lip.ToString()+"'";
    SqlConnection sqlcon=new SqlConnection("server=localhost;uid=sa;pwd=yoyo;database=IPaddress;Integrated Security=true");
    sqlcon.Open();
    SqlCommand sqlcom=new SqlCommand(sql,sqlcon);
    SqlDataReader dr= sqlcom.ExecuteReader();
    if(dr.HasRows)
    {
    dr.Read();
    string country=dr["country"].ToString();//<---------这里
    string city=dr["city"].ToString();//<---------这里
    }
    dr.Close();
    sqlcon.Close();

    }
    } }
    }
      

  8.   

    没有啊?
    我把你的代码重新运行了一遍
    没有发现错误啊我根据ip输入为127.0.0.1
    得到sql="select country,city from ip_address where ip1<='-1062731776' and ip2>='-1062731776'"
    你在查询分析器里 看能不能通过这句得到结果
      

  9.   


    上面的ip_address是我的库里面的表名
    改成你的为address
      

  10.   

    现在倒是没有错误了
    可是查询结果在哪里显示?
    如果我想让结果在一个label里显示应该怎么改?
      

  11.   

    我上面代码不是写的很清楚了吗
    if(dr.HasRows)
    {
    dr.Read();
    string country=dr["country"].ToString();//<---------这里得到country
    string city=dr["city"].ToString();//<---------这里得到city

    }
    写到你的程序中应该是this.label5.Text =country+city;//ip定位
      

  12.   

    不明白到底是怎么回事,一运行就下面的错误:
    未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
    其他信息: 系统错误。是不是sqlserver数据库的原因?
      

  13.   

    ip1,ip2列的类型是数值的话,就可以如下:关键部分写了就得了:
    SELECT * 
    FROM Address
    Where ip1 <= @IP
      AND ip2 > @IP