• ListView


    <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;
        }
  • 相关阅读:
    深入理解MyBatis中的一级缓存与二级缓存
    Spring-mvc文件的上传和下载
    Spring-mvc的拦截器和异常通知
    各种配置文件
    设计模式---代理模式
    dom4j读取xml和dtd的使用方式
    几种不同的路径
    常用正则表达式
    请求转发和重定向的对比
    跨浏览器检测某个节点是不是另一个节点的后代
  • 原文地址:https://www.cnblogs.com/huangj/p/7698371.html
Copyright © 2020-2023  润新知