Return Result from Select Query in stored procedure to a List

前端 未结 8 2044
猫巷女王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: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");
    

提交回复
热议问题