将存储过程中SelectQuery的结果返回到List我正在写一个存储过程,目前只包含SELECT查询。它将被扩展以做许多其他事情,这就是为什么它必须是一个存储过程,但现在,它只是一个简单的查询。像这样:SELECTname,occupation,positionFROMjobsWHERE...我希望返回此查询的结果以在C#中使用。我想将它添加到列表中,以便我可以将它绑定到GridView组件。但是,我不知道发生了什么。如果我必须在返回所有选定数据后将其插入到列表中,那没关系,我只需要知道如何正确返回数据,这样我就可以做到这一点。如果我能以一种可以直接弹出到列表中的格式取回它,那将是理想的。在存储过程中,你只需要这样写一个select查询:CREATEPROCEDURETestProcedureASBEGINSELECTID,NameFROMTestEND在C#端,你可以使用Reader、datatable、a??dapter进行访问。SusannaFloora刚刚解释了如何使用适配器。使用Reader:SqlConnectionconnection=newSqlConnection(ConnectionString);command=newSqlCommand("TestProcedure",connection);命令.CommandType=System.Data.CommandType.StoredProcedure;connection.Open();阅读器=命令.ExecuteReader();ListTestList=newList();测试一下;while(reader.Read()){test=newTest();test.ID=int.Parse(reader["ID"].ToString());test.Name=reader["姓名"].ToString();TestList.Add(测试);}gvGrid.DataSource=测试列表;gvGrid.DataBind();使用数据表:SqlConnectionconnection=newSqlConnection(ConnectionString);command=newSqlCommand("TestProcedure",connection);命令.CommandType=System.Data.CommandType.StoredProcedure;connection.Open();DataTabledt=newDataTable();dt.Load(command.ExecuteReader());gvGrid.DataSource=dt;gvGrid.DataBind();我希望它能为你提供帮助。?SqlConnectioncon=newSqlConnection("DataSource=DShp;InitialCatalog=abc;IntegratedSecurity=True");SqlDataAdapterda=newSqlDataAdapter("数据",con);da.SelectCommand.CommandType=CommandType.StoredProcedure;数据集ds=new数据集();da.Fill(ds,"数据");GridView1.DataSource=ds.Tables["数据"];GridView1.DataBind();SqlConnection连接=newSqlConnection(ConnectionString);command=newSqlCommand("TestProcedure",connection);命令.CommandType=System.Data.CommandType.StoredProcedure;connection.Open();DataTabledt=newDataTable();dt.Load(command.ExecuteReader());gvGrid.DataSource=dt;gvGrid.DataBind();我遇到了同样的问题,我花了很长时间才找到一个简单的解决方案。使用ASP.NETMVC5和EF6:将存储过程添加到.edmx模型时,存储过程的结果将通过名为yourStoredProcName_result的自动生成的对象传递。此_result对象包含与存储过程选择的数据库中的列相对应的属性。_result类可以简单地转换为列表:yourStoredProcName_result.ToList()withparameters?SqlConnectionconn=newSqlConnection(func.internalConnection);varcmd=newSqlCommand("usp_CustomerPortalOrderDetails",conn);cmd.CommandType=System.Data.CommandType.StoredProcedure;cmd.Parameters.Add("@CustomerId",SqlDbType.Int).Value=customerId;cmd.Parameters.Add("@Qid",SqlDbType.VarChar).Value=qid;conn.Open();//填充生产面板数据表listCustomerJobDetails=newDataTable();listCustomerJobDetails.Load(cmd.ExecuteReader());conn.Close();可能有帮助:从数据库中获取行:publicstaticDataRowCollectiongetAllUsers(stringtableName){DataSetset=newDataSet();SqlCommandcomm=newSqlCommand();通信。连接=DAL.DAL.conn;comm.CommandType=CommandType.StoredProcedure;comm.CommandText="getAllUsers";SqlDataAdapterda=newSqlDataAdapter();da.SelectCommand=comm;da.Fill(set,tableName);DataRowCollectionusersCollection=set.Tables[表名].Row;返回用户集合;}从DataRowCollection填充DataGridView:publicstaticvoidShowAllUsers(DataGridViewgrdView,stringtable,paramsstring[]fields){foreach(用户集中的数据行用户){grdView.Rows.Add(用户[字段[0]],用户[字段[1]],用户[字段[2]],用户[字段[3]]);}}实现:以上是C#学习教程:存储过程中的SelectQuery的结果返回List共享的所有内容。如果对你有用,需要进一步了解C#学习教程,希望大家多加关注—BLL.BLL.ShowAllUsers(grdUsers,"eusers","eid","euname","eupassword","eposition");本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
