日期:2014-05-18  浏览次数:21198 次

c#任何把数据库的多条消息一次性读取完呢?
////////连接数据库///////////
  string ysip="192.168.0.4";
  string yssjk="mdb";
  string ysuid="gen";
  string yspwd = "123012300";
  String sql = @"Data Source='" + ysip + "';database='" + yssjk+ "';uid='" + ysuid + "';pwd='" + yspwd + "';";
  SqlConnection conn = new SqlConnection(sql);
  conn.Open();
  string cx = "select top(50) * from chat where postpd='"+xxpd.Text+"' order by postsj desc";
  SqlCommand cmd = new SqlCommand(cx, conn);
  SqlDataReader sdr = cmd.ExecuteReader();
  if (sdr.Read())
  {
  xs.Text += string.Format(sdr["postnc"].ToString().Trim() + "(" + sdr["postname"].ToString().Trim() + ")" + sdr["postsj"].ToString().Trim() + "\r" + sdr["postnr"].ToString().Trim())+"\r\n";
  sdr.Close();
  conn.Close();
  }


 xs.Text 的类型是 richtexibox

现在只能读取一条。。。

------解决方案--------------------
修改如下
C# code
while(sdr.Read())
  {
  xs.Text += string.Format(sdr["postnc"].ToString().Trim() + "(" + sdr["postname"].ToString().Trim() + ")" + sdr["postsj"].ToString().Trim() + "\r" + sdr["postnr"].ToString().Trim())+"\r\n";
    }
sdr.Close();
  conn.Close();