您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何从存储过程中返回表?

如何从存储过程中返回表?

你的问题在哪里?

对于存储过程,只需创建:

CREATE PROCEDURE dbo.ReadEmployees @EmpID INT
AS
   SELECT *  -- I would *strongly* recommend specifying the columns EXPLICITLY
   FROM dbo.Emp
   WHERE ID = @EmpID

这就是全部。

在您的ASP.NET应用程序中,只需创建一个sqlConnection一个sqlCommand(不要忘记设置CommandType = CommandType.StoredProcedure

DataTable tblEmployees = new DataTable();

using(sqlConnection _con = new sqlConnection("your-connection-string-here"))
using(sqlCommand _cmd = new sqlCommand("ReadEmployees", _con))
{
    _cmd.CommandType = CommandType.StoredProcedure;

    _cmd.Parameters.Add(new sqlParameter("@EmpID", sqlDbType.Int));
    _cmd.Parameters["@EmpID"].Value = 42;

    sqlDataAdapter _dap = new sqlDataAdapter(_cmd);

    _dap.Fill(tblEmployees);
}

YourGridView.DataSource = tblEmployees;
YourGridView.DataBind();

然后DataTable用该数据填充例如a并将其绑定到例如GridView。

其他 2022/1/1 18:39:57 有455人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶