Return Result from Select Query in stored procedure to a List

前端 未结 8 1999
猫巷女王i
猫巷女王i 2021-01-01 10:10

I\'m writing a stored procedure that currently contains only a SELECT query. It will be expanded to do a number of other things, which is why it has to be a sto

相关标签:
8条回答
  • 2021-01-01 10:40

    I had the same question, took me ages to find a simple solution.

    Using ASP.NET MVC 5 and EF 6:

    When you add a stored procedure to your .edmx model, the result of the stored procedure will be delivered via an auto-generated object called yourStoredProcName_result.

    This _result object contains the attributes corresponding to the columns in the database that your stored procedure selected.

    The _result class can be simply converted to a list:

    yourStoredProcName_result.ToList()
    
    0 讨论(0)
  • 2021-01-01 10:41

    May be this will help:

    Getting rows from DB:

    public static DataRowCollection getAllUsers(string tableName) 
    {
         DataSet set = new DataSet();
         SqlCommand comm = new SqlCommand();
         comm.Connection = DAL.DAL.conn;
         comm.CommandType = CommandType.StoredProcedure;
         comm.CommandText = "getAllUsers";
         SqlDataAdapter da = new SqlDataAdapter();
         da.SelectCommand = comm;
         da.Fill(set,tableName);
    
         DataRowCollection usersCollection = set.Tables[tableName].Rows;
         return usersCollection;
    }
    

    Populating DataGridView from DataRowCollection :

    public static void ShowAllUsers(DataGridView grdView,string table, params string[] fields) 
    {
        DataRowCollection userSet = getAllUsers(table);
        foreach (DataRow user in userSet)
        {
             grdView.Rows.Add(user[fields[0]],
             user[fields[1]],
             user[fields[2]],
             user[fields[3]]);
        }
    }
    

    Implementation :

    BLL.BLL.ShowAllUsers(grdUsers,"eusers","eid","euname","eupassword","eposition");
    
    0 讨论(0)
提交回复
热议问题