日期:2014-05-17 浏览次数:20493 次
1. <%@ Page Language="C#" %>
2. <%@ Import Namespace="System.Data.SqlClient" %>
3. <%@ Import Namespace="System.Data" %>
4. <%@ Import Namespace="System.Text" %>
5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6. <script runat="server">
7. //注:为了方便没有使用代码页面方式,实际开发中这种做大很少见
8. protected void Page_Load(object sender, EventArgs e)
9. {
10.
11.
12. }
13. public void ShowData()
14. {
15. //实例化Connection对象
16. SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa");
17. //实例化Command对象
18. SqlCommand command = new SqlCommand("select * from UserInfo where sex=1", connection);
19. //打开Connection对象
20. connection.Open();
21. //得到DataReader的实例,注意使用了CommandBehavior这个参数,以便同时关闭Connection
22. SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
23. //如果当前记录还有下一条记录,则循环不会终止
24. while (reader.Read())
25. {
26. Response.Write("<tr><td>" + reader.GetInt32(0) + "</td>");//按照列顺序和对应类型直接读取值
27. Response.Write("<td>" + reader.GetString(1) + "</td>");//按照列顺序和对应类型直接读取值
28. Response.Write("<td>" + reader.GetString(2) + "</td>");//按照列顺序和对应类型直接读取值
29. Response.Write("<td>" + reader.GetByte(3) + "</td>");//按照列顺序和对应类型直接读取值
30. //下面是按照列顺序直接读取值,并且根据值来判断最终显示结果
31. Response.Write("<td>" + (reader.GetBoolean(4)==true?"男":"女") + "</td>");
32. //根据列顺序读,列的值需要做相应转换
33. Response.Write("<td>" + reader[5].ToString() + "</td>");
34. //根据列名来读取,列的值需要做相应转换
35. Response.Write("<td>" + reader["Phone"] + "</td>");
36. Response.Write("<td>