读取自定义行,datareader(C#)

时间:2014-03-19 06:05:39

标签: sqldatareader

此代码仅添加第一行,如何添加“mail”列的第二行

var cs = ConfigurationManager.ConnectionStrings["ConnectionString"];
        var sc = new SqlConnection(cs.ConnectionString);
        sc.Open();
        string str = "Select * From [Table1]";
        var cmd = new SqlCommand(str, sc);

        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            ListBox1.Items.Add(dr["mail"].ToString());
        }
        sc.Close();

1 个答案:

答案 0 :(得分:0)

 if (dr.HasRows)
    {
    while (dr.Read())
    {
        /* your code */
    }
   }
  else
    {
     /* error message */
    }