我在代碼中收到以下錯誤。將數據類型nvarchar轉換爲浮點時出錯
將數據類型nvarchar轉換爲float時出錯。
我就按一下按鈕的代碼是
protected void btnFind_Click(object sender, EventArgs e)
{
SqlParameter LatParam;
SqlParameter LngParam;
if (zipcode.Text != "")
{
litAddress.Text = "";
litAddress1.Text = "";
string addressstring = zipcode.Text;
string connstring = "Data Source=win2008-2;Initial Catalog=h1tm11;User ID=sa;Password=#1cub3123*;Persist Security Info=True;";
string SQL1 = "SELECT *, 6371.01 * ACOS(SIN(@lat*PI()/180) * SIN(store_lat*PI()/180) + COS(@lat*PI()/180) * COS(store_lat*PI()/180) * COS((store_long*PI()/180) - (@lng*PI()/180))) AS distance from storelocator where 6371.01 * ACOS(SIN(@lat*PI()/180) * SIN(store_lat*PI()/180) + COS(@lng*PI()/180) * COS(store_lat*PI()/180) * COS((store_long*PI()/180) - (@lng*PI()/180))) < '" + ddl_distance.SelectedItem.Value + "' order by distance asc;";
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
SqlCommand comm = new SqlCommand(SQL1, conn);
LatParam = new SqlParameter();
LatParam.ParameterName = "@lat";
LatParam.SqlDbType = SqlDbType.NVarChar;
LatParam.Value = "select lat from tbl_pincode where codes='" + zipcode.Text + "';";
LngParam = new SqlParameter();
LngParam.ParameterName = "@lng";
LngParam.SqlDbType = SqlDbType.NVarChar;
LngParam.Value ="select lat from tbl_pincode where codes='" + zipcode.Text + "';";
comm.Parameters.Add(LatParam);
comm.Parameters.Add(LngParam);
SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
string area = reader["lat"].ToString();
string codes =reader["lng"].ToString();
litAddress.Text += area;
litAddress1.Text += codes;
}
}
表中的數據類型是緯度,經度和pincodes爲nvarchar。
謝謝,
與nvarchar/float無關,但您應該真正使用參數而不是串聯 - 您可能在這裏暴露於SQL注入這裏 – 2012-01-16 09:38:47
請仔細閱讀:[SQL注入攻擊](http:///msdn.microsoft.com/en-us/library/ms161953.aspx) – musefan 2012-01-16 09:39:03
請指出哪一行引發異常 – 2012-01-16 09:39:28