<body> <form id="form1" runat="server"> <div> <table> <thead> <tr> <th> 工号 </th> <th> 姓名 </th> <th> 性别 </th> <th> 部门 </th> <th> 角色 </th> </tr> </thead> <tbody> <asp:ListView ID="LV" runat="server" ItemPlaceholderID="itemPlaceHolder"> <ItemTemplate> <tr> <td> <%# Eval("id") %> </td> <td> <%# Eval("name") %> </td> <td> <%# Eval("sex") %> </td> <td> <%# Eval("dep") %> </td> <td> <%# Eval("character")%> </td> </tr> </ItemTemplate> </asp:ListView> </tbody> </table> </div> </form> </body>
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } private void BindData(){ string sql = "select * from employee"; LV.DataSource = new OraSqlHelper().GetEmployees(sql); LV.DataBind(); }
public List<Employee> GetEmployees(string sql) { List<Employee> employees = new List<Employee>(); OracleDataReader result = null; OracleConnection conn = new OracleConnection(conStr); conn.Open(); OracleCommand cmd = new OracleCommand(sql, conn); //从数据库中读取数据流存入result中 result = cmd.ExecuteReader(); while (result.Read()) { Employee employee = new Employee(); //result.GetOrdinal("id")是得到ID所在列的index, //result.GetInt32(int n)这是将第n列的数据以Int32的格式返回 //result.GetString(int n)这是将第n列的数据以string 格式返回 //result.GetDateTime(int n)这是将第n列的数据以string 格式返回 //判断数据是否为空 //objId != DBNull.Value判断对象是否为空 //!Convert.IsDBNull(objScsj)判断DateTime是否为空 //对象.IsNull("字段") //对象.ToString() == "" var objId = result.GetValue(result.GetOrdinal("id")); if (objId != DBNull.Value) { employee.id = Convert.ToInt32(objId); } var objName = result.GetValue(result.GetOrdinal("name")); if (objName != DBNull.Value) { employee.name = objName.ToString(); } var objWjjid = result.GetValue(result.GetOrdinal("sex")); if (objWjjid != DBNull.Value) { employee.sex = objWjjid.ToString(); } var objDep = result.GetValue(result.GetOrdinal("dep")); if (objDep != DBNull.Value) { employee.dep = objDep.ToString(); } var objCharacter = result.GetValue(result.GetOrdinal("character")); if (objCharacter != DBNull.Value) { employee.character = objCharacter.ToString(); } //格式化输出数据 //Console.Write("ID:{0},Name:{1},PWD:{2},Age:{3},Sex:{4},Phone{5},Address:{6} ", id, name, pwd, age, sex, phone, address); employees.Add(employee); } conn.Close(); conn.Dispose(); return employees; }