在asp app中运行存储过程后数据库没有更新

时间:2017-03-27 15:35:37

标签: c# asp.net sql-server stored-procedures

我有一个asp应用程序应该能够让用户更新sql server数据库中的条目。当我在我的方法中运行我的存储过程时,数据库没有任何反应。

public void updateFeature(Feature updatedFeature)
{
    using (SqlConnection sqlConnection = new SqlConnection(GetConnectionString(updatedFeature.Environment)))
    using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
    {
        sqlCommand.CommandType = CommandType.StoredProcedure;
        sqlCommand.CommandText = "usp_UpdateFeature";
        sqlCommand.Parameters.AddWithValue("@FeatureName", updatedFeature.FeatureName);
        sqlCommand.Parameters.AddWithValue("@FeatureID", updatedFeature.FeatureID);
        sqlCommand.Parameters.AddWithValue("@FeatureDescription", updatedFeature.Description);
        sqlCommand.Parameters.AddWithValue("@FieldName", updatedFeature.FieldName);
        sqlCommand.Parameters.AddWithValue("@OnOffSwitch", updatedFeature.IsOn);
        sqlCommand.Parameters.AddWithValue("@ChannelID", updatedFeature.ChannelID);
        sqlCommand.Parameters.AddWithValue("@ProductID", updatedFeature.ProductID);                

        sqlConnection.Open();
        int numberOfRowsChanged =  sqlCommand.ExecuteNonQuery();
    }
}

,我的存储过程看起来像这样

ALTER PROCEDURE [dbo].[usp_UpdateFeature]  
(  
 @FeatureID int,  
 @FeatureName varchar(30),  
 @FeatureDescription varchar(200) = null,  
 @FieldName varchar(40),  
 @OnOffSwitch bit,
 @ChannelID varchar(3),
 @ProductID varchar(2)
)  
AS  

SET NOCOUNT ON  

UPDATE FM_Feature   
SET FeatureName = @FeatureName,   
 FeatureDescription = @FeatureDescription,   
 FieldName = @FieldName,   
 OnOffSwitch = @OnOffSwitch, 
 ProductID = @ProductID,
 ChannelID = @ChannelID 
WHERE FeatureID = @FeatureID

numberOfRowsChanged参数返回-1但我没有得到任何异常或错误。有什么东西我缺少或不理解?

1 个答案:

答案 0 :(得分:0)

我认为您需要在创建命令之前打开连接,我希望这对您有所帮助。

using (SqlConnection sqlConnection = new SqlConnection(GetConnectionString(updatedFeature.Environment))

sqlConnection.Open();

using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
{
    sqlCommand.CommandType = CommandType.StoredProcedure;
    sqlCommand.CommandText = "usp_UpdateFeature";
    sqlCommand.Parameters.AddWithValue("@FeatureName", updatedFeature.FeatureName);
    sqlCommand.Parameters.AddWithValue("@FeatureID", updatedFeature.FeatureID);
    sqlCommand.Parameters.AddWithValue("@FeatureDescription", updatedFeature.Description);
    sqlCommand.Parameters.AddWithValue("@FieldName", updatedFeature.FieldName);
    sqlCommand.Parameters.AddWithValue("@OnOffSwitch", updatedFeature.IsOn);
    sqlCommand.Parameters.AddWithValue("@ChannelID", updatedFeature.ChannelID);
    sqlCommand.Parameters.AddWithValue("@ProductID", updatedFeature.ProductID);                

    int numberOfRowsChanged =  sqlCommand.ExecuteNonQuery();
}

享受它。