0
我的項目工作正常,但很奇怪。我寫了一個記錄存儲過程如果記錄已經不存在然後它插入並返回1 else返回0 在asp.net中.cs代碼我把一個條件,如果結果返回1然後返回true否則返回false,它這樣做,完美但實際上SP不返回1,它在兩種情況下都返回0,但對於1和0情況,程序工作正常,爲什麼?我不明白。存儲過程返回意外的值,但工作正常
SP:
ALTER PROCEDURE [dbo].[AddCoordinates]
-- Add the parameters for the stored procedure here
@AddedDateTime varchar(50),
@IMEI varchar(50),
@RecordedDateTime varchar(50),
@Latitude varchar(50),
@Longitude varchar(50),
@IsParking bit,
@result int output
AS
BEGIN
--set @result = (select count(*) from dbo.Coordinates where IMEI [email protected])
If Not exists(select IMEI from dbo.Coordinates where IMEI [email protected])
Begin
insert into dbo.Coordinates
values (@AddedDateTime, @IMEI, @RecordedDateTime, @Latitude, @Longitude, @IsParking)
Select @result=1
End
Else If exists(select IMEI from dbo.Coordinates where IMEI [email protected])
Begin
Select @result=0
End
END
.BLL
public class BLL
{
String conStr;
String query;
SqlConnection sqlCon;
SqlCommand sqlCom;
public BLL()
{
conStr = WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
query = "";
sqlCon = new SqlConnection(conStr);
sqlCom = new SqlCommand(query, sqlCon);
}
public bool InsertCoordinates(string[] parts)
{
sqlCom.CommandText = "AddCoordinates";
sqlCom.CommandType = CommandType.StoredProcedure;
int result;
sqlCom.Parameters.Add("@AddedDateTime", SqlDbType.VarChar).Value = parts[0].ToString();
sqlCom.Parameters.Add("@IMEI", SqlDbType.VarChar).Value = parts[1].ToString();
sqlCom.Parameters.Add("@RecordedDateTime", SqlDbType.VarChar).Value = parts[2].ToString();
sqlCom.Parameters.Add("@Latitude", SqlDbType.VarChar).Value = parts[3].ToString(); ;
sqlCom.Parameters.Add("@Longitude", SqlDbType.VarChar).Value = parts[4].ToString(); ;
sqlCom.Parameters.Add("@IsParking ", SqlDbType.Bit).Value = ((parts[5].ToString().Trim()).Equals("0")) ? true : false;
SqlParameter sqlParam = new SqlParameter("@result", SqlDbType.Int);
sqlCom.Parameters.Add(sqlParam);
sqlCom.Parameters["@result"].Direction = ParameterDirection.Output;
try
{
sqlCon.Open();
result = Convert.ToInt32(sqlCom.ExecuteNonQuery());
}
catch (Exception ex)
{
throw ex;
}
finally
{
sqlCon.Close();
}
if(result == 1)
return true;
else
return false;
}
}
.Webservice
public class GPSWebservice : System.Web.Services.WebService {
public GPSWebservice() {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public bool insertCoordinates(string[] parts)
{
BLL bll = new BLL();
return bll.InsertCoordinates(parts);
}
}
.default.cs
protected void btnInsertCoordinates_Click(object sender, EventArgs e)
{
GPSWebservice gpsws = new GPSWebservice();
string[] parts = "2013-03-31,ALFALAH-8000,2013-03-31,1099,9888, 0".Split(',');
Response.Write(gpsws.insertCoordinates(parts));
}
我把結果= convert.toint32(sqlCom.executenonquery()),我試着ExecuteScalar()但同樣的問題。
但爲什麼我的代碼在0,1條件下工作,即使返回的值不是0,1 ..只是0, – user3432348
您的意思是?你的SP按原樣初始化'@ result'。 –
我的意思是什麼時候記錄存在然後.cs代碼應該返回假的基於0或真如果記錄不存在,這取決於從SP返回值,但如果SP返回0所有的時間,如何如果其他.cs工作? – user3432348