这个sql语句在C#中应该怎么写? 请问楼主是要ip1与ip2共同确认吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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(); to:44404(杨雪) 不是共同确认 是让输入的ip介于ip2与ip2之间 如果为真的话说明ip合法to:wj2929(*ヤRěйヤ*)是数值SqlConnection sqlcon=new SqlConnection("...");这是数据库连接吗? SqlConnection sqlcon=new SqlConnection("...");我省略了连接字符串详细点如下:SqlConnection sqlcon=new SqlConnection("server=(local);database=;uid=;pwd=;");我的这是SQLServer的 ,你要是用的其他数据库,要更改成相应的 我也是用的sql server我已经把 SqlConnection sqlcon=new SqlConnection("..."); 改成要连接的数据库了可出现了下面这样的一个问题: Form1.cs(221): 名称“slqcon”在类或命名空间“IPQuary.Form1”中不存在 这是怎么回事?? 哦拼错了不好意思slqcon-----------〉sqlcon我当然有自己的数据库类了 ,但我写上你们也不知道什么,所以就改成SqlConnection 了 建议写到Web.config里面那样方便些 /// <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(); } } }} 没有啊?我把你的代码重新运行了一遍没有发现错误啊我根据ip输入为127.0.0.1得到sql="select country,city from ip_address where ip1<='-1062731776' and ip2>='-1062731776'"你在查询分析器里 看能不能通过这句得到结果 哦上面的ip_address是我的库里面的表名改成你的为address 现在倒是没有错误了可是查询结果在哪里显示?如果我想让结果在一个label里显示应该怎么改? 我上面代码不是写的很清楚了吗if(dr.HasRows){ dr.Read(); string country=dr["country"].ToString();//<---------这里得到country string city=dr["city"].ToString();//<---------这里得到city }写到你的程序中应该是this.label5.Text =country+city;//ip定位 不明白到底是怎么回事,一运行就下面的错误:未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。其他信息: 系统错误。是不是sqlserver数据库的原因? ip1,ip2列的类型是数值的话,就可以如下:关键部分写了就得了:SELECT * FROM AddressWhere ip1 <= @IP AND ip2 > @IP dataSource转换DataView问题 接口问题,急 数据库的问题 有史以来碰到最奇怪的问题 输入字符串的格式不正确。 如何用程序控制word只读,不能拷贝,不能打印. visual studio .net 安装不上了 C#操作Excel 这样的奇葩sql语句怎么写呢? 我以前是做java现在工作需要要开发windows下的一些应用程序,请问C#怎么样? [WebMethod]中鏈結出錯!!!!!!! C#中的添加数据类型问题。。。。
应该是数值吧 如果不是用上面算法转换一下
下面取数据算法如下,这里假设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();
不是共同确认 是让输入的ip介于ip2与ip2之间 如果为真的话说明ip合法
to:wj2929(*ヤRěйヤ*)
是数值SqlConnection sqlcon=new SqlConnection("...");
这是数据库连接吗?
我省略了连接字符串
详细点如下:
SqlConnection sqlcon=new SqlConnection("server=(local);database=;uid=;pwd=;");我的这是SQLServer的 ,你要是用的其他数据库,要更改成相应的
我已经把 SqlConnection sqlcon=new SqlConnection("..."); 改成要连接的数据库了
可出现了下面这样的一个问题:
Form1.cs(221): 名称“slqcon”在类或命名空间“IPQuary.Form1”中不存在
这是怎么回事??
拼错了
不好意思
slqcon-----------〉sqlcon我当然有自己的数据库类了 ,但我写上你们也不知道什么,所以就改成SqlConnection 了
那样方便些
/// 应用程序的主入口点。
/// </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();
}
} }
}
我把你的代码重新运行了一遍
没有发现错误啊我根据ip输入为127.0.0.1
得到sql="select country,city from ip_address where ip1<='-1062731776' and ip2>='-1062731776'"
你在查询分析器里 看能不能通过这句得到结果
上面的ip_address是我的库里面的表名
改成你的为address
可是查询结果在哪里显示?
如果我想让结果在一个label里显示应该怎么改?
if(dr.HasRows)
{
dr.Read();
string country=dr["country"].ToString();//<---------这里得到country
string city=dr["city"].ToString();//<---------这里得到city
}
写到你的程序中应该是this.label5.Text =country+city;//ip定位
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
其他信息: 系统错误。是不是sqlserver数据库的原因?
SELECT *
FROM Address
Where ip1 <= @IP
AND ip2 > @IP