如何将数据列表从数据库存储到ArrayList或C#中的列表

表:员工


code|Name|Left

----+----+----

1   | A  | Y

2   | B  | N

3   | C  | N

4   | D  | Y

5   | E  | N

6   | F  | Y

现在我在做,


        SqlConnection cn=new SqlConnection();

        SqlCommand cmd = new SqlCommand();

        cn.ConnectionString="<CONNECTION STRING>"

        List<string> str = new List<string>();

        cmd.Connection=cn;

        cmd.Connection.Open();

        cmd.CommandText="Select code from employee where Left='Y'";

        SqlDataReader dr=cmd.executeReader();

        while(dr.Read())

        {

                       str.Add(dr1.GetValue(0).ToString());


        }


        foreach (string p in str)

        {

                 Response.Write(p);

        }

此代码仅获取1个数据,我如何获取Left ='Y'的所有数据


qq_遁去的一_1
浏览 272回答 3
3回答

浮云间

我认为如果您在sql server中使用存储过程会更好...您可以为查询创建一个USE [DBName]GO/****** Object:&nbsp; StoredProcedure [dbo].[GetCode]&nbsp; &nbsp; Script Date: 03/05/2018 10:51:19 a.m. ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[GetCode](@Left char)AS&nbsp;BEGINSET NOCOUNT ON;SELECT Code FROM Employee WHERE Employee.Left in (@Left)&nbsp;END...然后,您可以通过以下方式从c#调用它:private List<Type> GetCode(string Left)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Type> List = new List<Type>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DbConnection();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; con.Open();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DynamicParameters Parm = new DynamicParameters();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Parm.Add("@Left", Left, DbType.String, ParameterDirection.Input, 2);//Parm.Add(nameOfParameter,valueOfParameter,typeOfParameter,directionOfParameter,sizeOfParameter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List = SqlMapper.Query<Type>(con, "Entities.GetCode", Parm, commandType: CommandType.StoredProcedure).ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; con.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return List;&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP