2012-01-17 92 views
-2
protected void btnFind_Click(object sender, EventArgs e) 
{ 
    if (zipcode.Text != "") 
    { 
     litAddress.Text = ""; 
     litAddress1.Text = ""; 
     string addressstring = zipcode.Text; 

     SqlConnection conn1 = new SqlConnection("Data Source=win2008-2;Initial Catalog=h1tm11;User ID=sa;Password=password;Persist Security Info=True;"); 
     SqlCommand cmd = new SqlCommand("Select lat,lng from tbl_pincode where codes='" + addressstring + "'", conn1); 
     DataTable table = new DataTable(); 
     SqlDataAdapter adapter = new SqlDataAdapter(cmd); 
     adapter.Fill(table); 

     foreach (DataRow row in table.Rows) 
     { 
      string lat = row["lat"].ToString(); 
      string lng = row["lng"].ToString(); 

      string connstring = "Data Source=win2008-2;Initial Catalog=h1tm11;User ID=sa;Password=password;Persist Security Info=True;"; 
      SqlConnection conn = new SqlConnection(connstring); 
      string SQL1 = "SELECT *, 6371.01 * ACOS(SIN(CAST((lat) AS float)*PI()/180) * SIN(CAST((store_lat) AS float)*PI()/180) + COS(CAST((lat) AS float)*PI()/180) * COS(CAST((store_lat) AS float)*PI()/180) * COS((CAST((store_long) AS float)*PI()/180) - (CAST((lng) AS float)*PI()/180))) AS distance from storelocator where 6371.01 * ACOS(SIN(CAST((lat) AS float)*PI()/180) * SIN(CAST((store_lat) AS float)*PI()/180) + COS(CAST((lat) AS float)*PI()/180) * COS(CAST((store_lat) AS float)*PI()/180) * COS((CAST((store_long) AS float)*PI()/180) - (CAST((lng) AS float)*PI()/180))) < '" + ddl_distance.SelectedItem.Value + "' order by distance asc;"; 
      conn.Open(); 
      SqlCommand comm = new SqlCommand(SQL1, conn); 
      SqlDataReader reader = comm.ExecuteReader(); 
      while (reader.Read()) 
      { 
       string area = reader["store_name"].ToString(); 
       string codes = reader["store_address1"].ToString(); 
       litAddress.Text += area + "<br>"; 
       litAddress1.Text += codes + "<br>"; 
      } 
     } 
    } 
} 

我得到一个无效的列名lat,lat,lng,lat,lat,lng error.I认为它不是在sql查询中取lat,lng值,但我传递它。我也将字符串转换为浮动。我的数据库中的数据字段为nvarcharlat,lng,store_latstore_long。我不知道为什么。SQL查询中无效的列名称(lat,lat,lng,lat,lat,lng)

+0

是否商店定位器有lat和液化天然气领域?为什么ddl_distance值在引号中?为什么不首先使用**参数开始**哦,似乎你已经[警告它](http://stackoverflow.com/questions/8878026/error-converting-data-type-nvarchar-to-float) – V4Vendetta 2012-01-17 06:12:19

+0

您的代码易受Sql注入攻击。 – 2012-01-17 06:12:54

+0

你确定你的sql查询是正确的吗?并且可能存在一些转换问题! – Sukanya 2012-01-17 06:21:43

回答

-1

你交叉检查一次,全部列名。并尝试使用良好的命名约定进行重命名,以免导致错误。

这不是向您的查询添加参数的正确方式,它将容易出错。Exploits of SqlInjectio, 您应该使用参数化查询。也不要使用

SELECT * from table name , 

你使用多少列来显示你的逻辑,这将有助于加快处理速度。

 SqlCommand cmd = new SqlCommand("Select lat,lng from tbl_pincode [email protected]", conn1); 
     cmd.Parameters.AddWithValue("@address", addressstring); 
     DataTable table = new DataTable(); 
     SqlDataAdapter adapter = new SqlDataAdapter(cmd); 
     adapter.Fill(table); 
+0

谢谢,我明白了。 – asifa 2012-01-17 08:46:54

+0

你需要执行它。并阅读使用SqlDatareader,检查更新ans – 2012-01-17 08:48:49

+0

为什么downvote ??? – 2012-01-17 10:09:19

0

你应该写SQL查询一样,

string lat = row["lat"].ToString(); 
    string lng = row["lng"].ToString(); 
    string SQL1 = "SELECT *, 6371.01 * CAST("+lat+" AS float)*PI()/180 From YourTable order by distance asc;"; 
+0

我跟随你的建议,但我得到错误附近AS – asifa 2012-01-17 08:46:01

+1

对不起,延迟的答复。 请尝试这个 “SELECT *,6371.01 *转换(十进制(12,2),”+ lat +“)* PI()/ 180 FromTable order by distance asc”; – 2012-01-17 09:48:12

相关问题