-1

我有这个 sql 命令:

SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection(connectionstring);
cmd = new SqlCommand("SELECT PROCEDURA,DAY_SEND,EMAIL FROM FASTREPORT_SEND WHERE.....", con);

例如,输出是这样的:

Procedura     Day_send        Email
procedure1       5        example@gmail.com
procedure2       3        example2@gmail.com

我希望工作始终与这三个值一起工作。这是我的代码:

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
    //Work with the values
}

我怎样才能将这三个值始终保存到三个不同的变量中?感谢帮助..

4

1 回答 1

3

使用一个类:

public class Procedura     
{
    public string Name { get; set; }
    public int Day_send { get; set; }
    public string Email { get; set; }
}

现在您可以List<Procedura>DataTable或直接从SqlDataReader.

var proceduras = new List<Procedura>();
foreach (DataRow row in dt.Rows)
{
    proceduras.Add(new Procedura{ Name = row.Field<string>("Procedura"), Day_send = row.Field<string>("Day_send"), Email = row.Field<string>("Email") });
}

您可以通过索引器访问各个实例:

Procedura firstProcedura = proceduras[0];
于 2013-10-29T08:30:40.767 回答