Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 试图在C中运行更新存储过程,但它';他什么也没做_C#_Asp.net_Sql - Fatal编程技术网

C# 试图在C中运行更新存储过程,但它';他什么也没做

C# 试图在C中运行更新存储过程,但它';他什么也没做,c#,asp.net,sql,C#,Asp.net,Sql,当我手动运行存储过程时,它工作正常。然而,每当我试图用下面的代码调用它时,没有任何东西在更新。我注意到,在我的存储过程中,我将“修改日期”字段更改为当前时间/日期,这就是更新;然而,我的其他价值观都不是 以下是我使用的方法: public void EditProduct(int productID, string productName, string productNumber, string productColor, string productSubcategory) {

当我手动运行存储过程时,它工作正常。然而,每当我试图用下面的代码调用它时,没有任何东西在更新。我注意到,在我的存储过程中,我将“修改日期”字段更改为当前时间/日期,这就是更新;然而,我的其他价值观都不是

以下是我使用的方法:

public void EditProduct(int productID, string productName, string productNumber, string productColor, string productSubcategory)
    {
        ConfigureDataAccess();
        myCommand = new SqlCommand("[dbo].[UpdateProductInfo]", myConnection);
        myCommand.CommandType = CommandType.StoredProcedure;
        myCommand.Parameters.AddWithValue("@ProductID", productID);
        myCommand.Parameters.AddWithValue("@ProductName", String.IsNullOrWhiteSpace(productName) ? (object)DBNull.Value : (object)productName);
        myCommand.Parameters.AddWithValue("@ProductNumber", String.IsNullOrWhiteSpace(productNumber) ? (object)DBNull.Value : (object)productNumber);
        myCommand.Parameters.AddWithValue("@ProductColor", String.IsNullOrWhiteSpace(productColor) ? (object)DBNull.Value : (object)productColor);
        myCommand.Parameters.AddWithValue("@ProductSubcategory", String.IsNullOrWhiteSpace(productSubcategory) ? (object)DBNull.Value : (object)productSubcategory);

        try
        {
            try
            {
                myConnection.Open();
                myCommand.ExecuteNonQuery();
            }
            catch (SqlException exception)
            {
                exception.Data.Add("cstring", myConfiguration);
                throw;
            }
            catch (InvalidOperationException ex)
            {
                ex.Data.Add("cStatus", myConnection.State);
                throw;
            }
        }
        finally
        {
            myConnection.Close();
        }
我不认为
ConfigureDataAccess()
是一个问题,因为其他方法正在使用它,并且运行良好。有没有什么东西能立即突出

编辑:这是我的存储过程:

ALTER PROCEDURE [dbo].[UpdateProductInfo]

@ProductID as int,
@ProductName as varchar(50),
@ProductNumber as varchar(25),
@ProductColor as varchar(15),
@ProductSubcategory as varchar(50)

AS
BEGIN

SET NOCOUNT ON;

UPDATE P
SET P.Name = @ProductName, P.ProductNumber = @ProductNumber, P.Color = @ProductColor, P.ModifiedDate = GetDate(), 
P.ProductSubcategoryID = (SELECT PS.ProductSubcategoryID FROM Production.ProductSubcategory PS WHERE PS.Name = @ProductSubcategory)
FROM Production.Product as P
LEFT OUTER JOIN  Production.ProductSubcategory as PS ON P.ProductSubcategoryID = PS.ProductSubcategoryID
WHERE P.ProductID = @ProductID
END
下面是我的
ConfigureDataAccess()
方法:

    private string myConfiguration;
    private SqlConnection myConnection;
    private SqlCommand myCommand;
    private SqlDataReader myReader;
    private List<Product> myProducts;

    public void ConfigureDataAccess()
    {
        myConfiguration = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
        myConnection = new SqlConnection(myConfiguration);
        myReader = null;
        myProducts = new List<Product>();
    }    
私有字符串配置;
私有连接myConnection;
私有SqlCommand-myCommand;
私有SqlDataReader-myReader;
私人产品清单;
public void ConfigureDataAccess()
{
myConfiguration=ConfigurationManager.ConnectionString[“myConnection”]。ConnectionString;
myConnection=新的SqlConnection(myConfiguration);
myReader=null;
myProducts=新列表();
}    

我会尝试删除SQL语句的左连接短语

LEFT OUTER JOIN  Production.ProductSubcategory as PS ON P.ProductSubcategoryID = PS.ProductSubcategoryID 

我认为sql没有它也可以正常工作。

是的,我看不到左外连接的用法。删除并尝试。另一件可能有帮助的事情是在开发过程中停止抑制异常。注释try-catch以查看异常(如果有)。

我解决了问题。我需要将
if(!IsPostBack)
包含在我的
页面加载
事件中。我没有意识到,当我在我的网络表单上单击“保存”按钮时,它正在发回并将我的文本框(其中
EditProduct()
方法中的参数正在获取其值)设置回原始值。

请发布您的ConfigureDataAccess()方法和存储过程,以便人们可以复制该问题。这有点猜测,但请将commandtext更改为exec[dbo].[UpdateProductInfo]。可能是ADO在调用之前放了一些东西,如果exec是批处理中的第一个命令,那么您只能在没有exec的情况下离开。Ingore,读一下日期变更,我最初有一个稍微不同的UPDATE语句,其中左连接是必要的。你说得对,我不需要它。