2010-12-03 2 views
0

저장 프로 시저에 매개 변수를 전달하려고합니다. 코드에서이SQL Server 2008에 매개 변수 전달

@p_latitude='',@p_longitude='',@p_miles='',@p_searchtext='',@p_maptownid=182,@p_regionid=0 

가 나는 오류 cannot convert navarchar to float납니다

cmd.Parameters.Add("@p_latitude", SqlDbType.NVarChar).Value="''"; 
cmd.Parameters.Add("@p_longitude", SqlDbType.NVarChar).Value="''"; 
cmd.Parameters.AddWithValue("@p_miles",0); 
cmd.Parameters.Add("@p_searchtext",SqlDbType.NVarChar).Value="''"; 
cmd.Parameters.AddWithValue("@p_maptownid",maptownid); 
cmd.Parameters.AddWithValue("@p_regionid",0); 

같은 매개 변수를 전달하고처럼 그들은이다. 다른 방법으로 null, string.empty를 보내려고했습니다. 그러나 그것을 찾을 수는 없다.

+1

매개 변수의 유형은 무엇입니까? 저장 프로 시저 매개 변수를 게시 할 수 있습니까? –

답변

0

대신 SqlDbType.NVarChar

의 DbType.String를 사용해보십시오

(난 당신이 동일한 작업을 수행하는 것이 좋습니다 그래서 그들은 다른 레이어에있는 모든 있었다) 그런데 사실이 내가 그것을 할 방법은 다음과 같습니다

List<SqlParameter> parameters = new List<SqlParameter> { }; 

string status = "dummystatus"; 
string date = Datetime.Now; 
parameters.Add(new SqlParameter("@Status", status)); 
parameters.Add(new SqlParameter("@date", date)); 

SqlCommand cmd = new SqlCommand(); 
command.Connection = connection; 

//set the command text (stored procedure name or SQL statement) 
command.CommandText = commandText; 
command.CommandType = commandType; 
foreach (SqlParameter p in parameters) 
{ 
    command.Parameters.Add(p); 
} 
SqlDataAdapter da = new SqlDataAdapter(command); 
DataSet ds = new DataSet(); 

//fill the DataSet using default values for DataTable names, etc. 
da.Fill(ds); 

// detach the SqlParameters from the command object, so they can be used again.   
cmd.Parameters.Clear(); 
5

빈 문자열은 ""이 아닌 "''"으로 전달되어야합니다. 아포스트로피 (')는 문자열 값으로 전달되며 문자열 구분 기호로 전달되지 않습니다. 오류 메시지는 nvarchar를 전달하는 float 열이 있다고 표시합니다. 나는 @p_latitude와 @p_longtitude 매개 변수가 문제라고 생각합니다. 값을 DBNull.Value로 직접 설정하십시오.

2

나는 위도, 경도 및 마일이 부동 소수점 숫자라고 생각합니다. 그렇다면 다음과 같이 전달해야합니다.

cmd.Parameters.AddWithValue("@p_latitude", 0.0); 
cmd.Parameters.AddWithValue("@p_longitude", 0.0); 
cmd.Parameters.AddWithValue("@p_miles", 0.0);