【问题标题】:How can I get my return value from stored procedure to display on GridView?如何从存储过程中获取返回值以显示在 GridView 上?
【发布时间】:2017-09-22 16:01:31
【问题描述】:

我有一个来自存储过程GetTeam 的返回值,我希望它显示在Gridview 中。这就是我所拥有的,但它没有显示在 gridview 中:

protected void getTeam()
{
    SqlConnection con;
    string CS = Configuration.Manager.ConnectionStrings["TEAM"].ConnectionString;
    DataTable dt = new DataTable();

    using(con = new SqlConnection(CS))
    {
        con.Open();

        SqlCommand cmd = new SqlCommand("GetTeam",con);

        SqlDataAdapter da = new SqlAdapter(cmd);
        da.fill(dt);

        Gridview1.Datasource = dt;
        Gridview1.Databind();
    }
}

有人可以帮忙吗?

【问题讨论】:

  • 请分享您的存储过程代码
  • 真的很长,但是我有一个 return (at)return_value,我希望 @return_value 显示在 gridview 上。
  • 如果您使用调试器单步执行此代码,您能否看到您的数据是否填充到内存中的数据表中?这将帮助我们缩小问题是否是存储过程的问题,或者是否是您使用 gridview 的方式。
  • @return_value 中有什么?一个值,多个值?
  • 如果是标量值,请使用 out 参数。在 Datatable 中创建一个空白行并根据需要附加值。 blogs.msdn.microsoft.com/spike/2009/05/07/…

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


【解决方案1】:

在下面尝试这个解决方案,在这个解决方案中,我们正在发送一个参数来帮助将我们的结果缩小到特定的团队。

using (SqlConnection conn = new SqlConnection())
 {
            //Connection string
            conn.ConnectionString = ConnectionString.DataSourceString;

            //Create adapter and assign store procedure name
            SqlCommand cmmd = new SqlCommand()
            {
                CommandType = CommandType.StoredProcedure
            };

            // Assign to Command type
            //exception is caught because it cannot find Stored Procedure

            //Insert parameters into row from input text
            cmmd.CommandText = "GetTeam";
    // Send parameter            
cmmd.Parameters.AddWithValue("@PGetDayTeam",
 ShiftParameterTextBox.Text.Trim());

            cmmd.Connection = conn;
            try
            {
                conn.Open();
                //If we do not receive any records 
                GridView2.EmptyDataText = "No Records Found";
                GridView2.DataSource = cmmd.ExecuteReader();
                GridView2.DataBind();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                // Close and dispose connection
                conn.Close();
                conn.Dispose();
            }
 // select from grid
protected void GridView2_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{

GridViewRow row = GridView2.Rows[e.NewSelectedIndex];


        string GetTeam = row.Cells[2].Text;
       // You can use it in viewstate or what you choose.
        ViewState["GetTeam"] = GetTeam;

}

【讨论】:

    【解决方案2】:

    Fill 操作然后将行添加到目标 DataTable 对象 在 DataSet 中,如果尚未创建 DataTable 对象,则创建它们 存在。创建DataTable对象时,Fill操作正常 仅创建列名元数据。但是,如果 MissingSchemaAction 属性设置为 AddWithKey,适当的主键和 约束也被创建。

    您正在使用 sql 程序。所以你已经有了一个 Colums 的名字。您不需要使用 DataTable。 Sql 适配器也可以填充数据集。您需要先使用 DataSet。

    public static DataSet ExecuteDataset(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
            {
                //create a command and prepare it for execution
                SqlCommand cmd = new SqlCommand();
                PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters);
    
                //create the DataAdapter & DataSet
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
    
                //fill the DataSet using default values for DataTable names, etc.
                da.Fill(ds);
    
                //return the dataset
                return ds;
            }
    

    然后您可以将您的 Gridview 与那个绑定。

    grdView.DataSource = ds;
    grdView.DataBind();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      相关资源
      最近更新 更多