使用存储过程将数据从一个表复制到另一个表

时间:2016-01-26 09:15:30

标签: c# sql-server entity-framework stored-procedures

我想将数据从A_Table

复制到B_Table数据

然后最后删除B_Table数据

我刚创建了以下

CREATE PROCEDURE [dbo].[Copy_ProductStatistics]

  (@Product_ID nvarchar, @ProductName nvarchar,@CreatedDate datetime)

AS

BEGIN
        INSERT INTO A_Table(Product_ID, ProductName,CreatedDate)

        SELECT Product_ID, ProductName,CreatedDate
        FROM B_Table
        WHERE (Product_ID = @Product_ID AND ProductName = @ProductName AND CreatedDate = @CreatedDate

        DELETE FROM  B_Table
END

然后我称之为

    [HttpGet]
    public ActionResult Copy_DatatoProductStatistics()
    {
        var incomplete_product_result = db.Database.SqlQuery<ProductStatistics>("Copy_ProductStatistics");

        return RedirectToAction("FileUpload", "FileUpload");
    }

这不会突然出现任何错误,问题出在stored procedure

1 个答案:

答案 0 :(得分:0)

以下是调用带参数的存储过程的示例。 sp不返回任何数据: (ctx.ExecuteCommand是Database.ExecuteSqlCommand方法的包装器)

    const string sql = "DeleteVersion @pVersiontId";
    using (IContext ctx = DI.Container.Resolve<IContext>()) {
        try {
            ctx.ExecuteCommand(sql, new SqlParameter("@pVersiontId", versionId));
        } catch (SqlException ex) {
            LoggerFactory.CreateLog().LogError(String.Format(Resources.CouldNotDeleteDeVersion, versionId), ex);
            throw new CannotDeleteVersionExcpetion(String.Format(Resources.CouldNotDeleteDeVersion, versionId), ex);
        }
    }