ASP.Net和存储过程连接

时间:2018-05-28 15:35:42

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

我是第一次在ASP.Net中......

我创建了一个存储过程,现在我想把这个存储过程放在GridView中并进行连接,我的问题是我不知道如何为我的存储过程做因为StatementType的连接。当我选择,更新选择或删除我的StatementType时,请转到正确的操作。我的问题是我在哪里可以在文本框,Gridview中进行连接......?

我的存储过程:

CREATE PROCEDURE SP_Employees 
    (@EmployeeID INT,
     @EmployeeName VARCHAR(255),
     @EmployeeUsername VARCHAR(255),
     @EmployeeEmail VARCHAR(255),
     @GroupID INT,
     @Password VARCHAR(255),
     @StatementType NVARCHAR(20) = ''
    ) 
AS 
BEGIN
    IF @StatementType = 'INSERT'
    BEGIN
        INSERT INTO [Employees]([EmployeeID], [EmployeeName], [EmployeeUsername], [EmployeeEmail], [GroupID], [Password])
        VALUES (@EmployeeID, @EmployeeName, @EmployeeUsername, @EmployeeEmail, @GroupID, @Password);
    END
    ELSE IF @StatementType = 'SELECT'
    BEGIN
        SELECT *
        FROM [Employees];
    END
    ELSE IF @StatementType = 'Update'
    BEGIN
        UPDATE [Employees]
        SET [EmployeeName] = @EmployeeName,
            [EmployeeUsername] = @EmployeeUsername,
            [EmployeeEmail] = @EmployeeEmail,
            [GroupID] = @GroupID,
            [Password] = @Password
        WHERE
            [EmployeeID] = @EmployeeID;
    END
    ELSE IF @StatementType = 'DELETE'
    BEGIN
        DELETE FROM [Employees]
        WHERE [EmployeeID] = @EmployeeID;
    END;
END

1 个答案:

答案 0 :(得分:0)

GridView易用性的全部概念是您根本不需要存储过程。

如果您的DBA只允许您访问使用存储过程,那么您可以指定自定义SelectMethod,UpdateMethod和DeleteMethod,然后手动映射从GridView获取的值以调用存储过程,但它是一条很长的路径做GridView本可以很容易地完成的工作。

    // The id parameter name should match the DataKeyNames value set on the control
    public void EmployeeTable_UpdateItem(int id)
    {
        InvMS.Models.Employee item = null;
        // Load the item here, e.g. item = MyDataLayer.Find(id);
        if (item == null)
        {
            // The item wasn't found
            ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
            return;
        }
        TryUpdateModel(item);
        if (ModelState.IsValid)
        {
            // Create a connection to your database here, 
            // Map the attributes to your stored procedure and call the procedure
        }
    }

    // The id parameter name should match the DataKeyNames value set on the control
    public void EmployeeTable_DeleteItem(int id)
    {

    }