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
值,但我传递它。我也将字符串转换为浮动。我的数据库中的数据字段为nvarchar
lat
,lng
,store_lat
和store_long
。我不知道为什么。SQL查询中无效的列名称(lat,lat,lng,lat,lat,lng)
是否商店定位器有lat和液化天然气领域?为什么ddl_distance值在引号中?为什么不首先使用**参数开始**哦,似乎你已经[警告它](http://stackoverflow.com/questions/8878026/error-converting-data-type-nvarchar-to-float) – V4Vendetta 2012-01-17 06:12:19
您的代码易受Sql注入攻击。 – 2012-01-17 06:12:54
你确定你的sql查询是正确的吗?并且可能存在一些转换问题! – Sukanya 2012-01-17 06:21:43