在单个文本框中显示查询值

时间:2011-10-30 08:02:31

标签: c# asp.net sql

如何在文本框中显示SQL查询结果,以便对于包含三个答案的查询,我想在三个文本框中显示它们?我可以使用ExecuteScalar或Listbox或记录集吗?我怎样才能做到这一点?我想我应该使用循环但是如何?

1 个答案:

答案 0 :(得分:1)

If you are executing a SQL command that returns a result, such as executing a SELECT statement you will have to use a different method. The SqlCommand's ExecuteReader method returns a SqlDataReader object that contains all of the records retrieved after executing the SQL command

try
{
  SqlDataReader dr;
  dbCon.Open();

 //write your select statement here.....
  dr = sqlcom.ExecuteReader();
  if(dr.HasRows == True)
  {
    txt_clientID.Text = ((Integer) dr["cID"]).ToString();
    txt_clientAddress.Text = (String) dr["cAddress"];
    txt_clientPhoneNumber.Text = (String) dr["cPhoneNumber"];
  }
  dr.Close();
  dbCon.Close();
}
catch(Exception ex)
{}