C#(ASP.NET) DataList数据库数据绑定

DataList数据绑定 1.连接数据库 (1)创建连接字符串 (2)创建连接对象 2.获取存储过程 (1)创建存储过程 (2)获取存储过程 3.获取数据表 (1)创建SqlDataAdapter对象 (2)填充SqlDataAdapter对象 (3)返回DataTable对象 4.数据绑定 (1)设置DataList.DataSource属性 (2)编写前端DataList控件及子控件 (3)给子控件赋值 <%# Eval("数据列名称")%>   //Web.Config文件配置   <configuration>
  <appSettings>
    <add key="connectionString" value="server=WIN-V30CUN3PJB1\SQLEXPRESS; database=yanirui; user=sa; pwd=FRP+757721728;"/>
  </appSettings>   //数据库连接对象类源码     public class Database
    {
        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <returns>连接对象</returns>
        public SqlConnection GetConnection()
        {
            return new SqlConnection(ConfigurationManager.AppSettings["connectionString"]);
        }

        /// <summary>
        /// 执行管理命令
        /// </summary>
        /// <param name="command">命令对象</param>
        public void ExecuteNonQuery(SqlCommand command)
        {
            try
            {
                if (command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                if (command.Connection.State == ConnectionState.Open)
                {
                    command.Connection.Close();
                }
            }
        }

        /// <summary>
        /// 执行单行单列查询命令
        /// </summary>
        /// <param name="command">命令对象</param>
        /// <returns>单行单列查询结果</returns>
        public string ExecuteScalar(SqlCommand command)
        {
            try
            {
                if (command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                return command.ExecuteScalar().ToString();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                if (command.Connection.State == ConnectionState.Open)
                {
                    command.Connection.Close();
                }
            }
        }

        /// <summary>
        /// 执行单行查询命令
        /// </summary>
        /// <param name="command">命令对象</param>
        /// <returns>单行查询结果</returns>
        public SqlDataReader ExecuteReader(SqlCommand command)
        {
            try
            {
                if (command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                return command.ExecuteReader();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                if (command.Connection.State == ConnectionState.Open)
                {
                    command.Connection.Close();
                }
            }
        }

        /// <summary>
        /// 查询表
        /// </summary>
        /// <param name="command">命令</param>
        /// <param name="tableName">表名称</param>
        /// <returns>表</returns>
        public DataTable GetTable(SqlCommand command, string tableName)
        {
            try
            {
                if (command.Connection == null)
                {
                    command.Connection = GetConnection();
                }
                if (command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                DataTable dt = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                adapter.Fill(dt);
                return dt;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 查询存储过程
        /// </summary>
        /// <param name="procName">存储过程名称</param>
        /// <returns>命令</returns>
        public SqlCommand GetCommand(string procName)
        {
            using (SqlConnection connection = GetConnection())
            {
                using (SqlCommand command = new SqlCommand())
                {
                    connection.Open();
                    command.CommandText = procName;
                    command.CommandType = CommandType.StoredProcedure;
                    return command;
                }
            }
        }
    }   //数据管理类 public class ProductManager
    {
        Database db = new Database();

        /// <summary>
        /// 获取产品信息列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetProductInfoList()
        {
            return db.GetTable(db.GetCommand("proc_GetProductInfoList"),
                "ProductInfoTable");
            //DataList dl = new DataList();
            //dl.DataSource = dt;
            //dl.DataKeyField = dt.Columns[0].ToString();
            //dl.DataBind();
        }

        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <returns></returns>
        public object GetProductInfo(int productId)
        {
            SqlDataReader reader = db.ExecuteReader(db.GetCommand("proc_GetProductInfo"));
            if (reader.Read())
            {
                return reader.GetValue(productId);
            }
            return null;
        }

        /// <summary>
        /// 添加产品信息
        /// </summary>
        public void AddProductInfo()
        {
            db.ExecuteNonQuery(db.GetCommand("proc_AddProductInfo"));
        }

        /// <summary>
        /// 更新产品信息列表
        /// </summary>
        public void UpdateProductInfoList()
        {
            db.ExecuteNonQuery(db.GetCommand("proc_UpdateProductInfoList"));
        }

        /// <summary>
        /// 编辑产品信息
        /// </summary>
        public void EditProductInfo()
        {
            db.ExecuteNonQuery(db.GetCommand("proc_EditProductInfo"));
        }

        /// <summary>
        /// 删除产品信息
        /// </summary>
        public void DeleteProductInfo()
        {
            db.ExecuteNonQuery(db.GetCommand("proc_DeleteProductInfo"));
        }

        /// <summary>
        /// 清空产品信息列表
        /// </summary>
        public void ClearProductInfoList()
        {
            db.ExecuteNonQuery(db.GetCommand("proc_ClearProductInfoList"));
        }
    }   //调用数据绑定 public partial class productlist : System.Web.UI.Page
    {
        public ProductManager pm;

        protected void Page_Load(object sender, EventArgs e)
        {
            pm = new ProductManager();
            DataTable dt = pm.GetProductInfoList();
            productDataList.DataSource = dt;
            productDataList.DataKeyField = dt.Columns[0].ToString();
            productDataList.DataBind();
        }
    }   <!--数据显示-->     <div class="productbox">
        <asp:DataList ID="productDataList" runat="server" Width="500"
            BorderColor="Red" BorderWidth="1" Height="500" DataSourceID="" DataKeyField="productId">
            <HeaderTemplate>
                <ul>
                    <li>产品名称</li>
                    <li>产品图片</li>
                    <li>产品价格</li>
                </ul>
            </HeaderTemplate>
            <ItemTemplate>
                <table>
                    <tr>
                        <td style="border:1px inset red; width:50px; height:50px;">
                            产品名称:<%# Eval("productName") %>
                            产品图片:<img src="<%# Eval("productImgPath") %>" />
                            产品价格:<%# Eval("productPrice") %>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
            <EditItemTemplate>
                <table>
                    <tr>
                        <td style="border:1px inset red; width:50px; height:50px;">
                            产品名称:<%# Eval("productName") %>
                            产品图片:<img src="<%# Eval("productImgPath") %>" />
                            产品价格:<%# Eval("productPrice") %>
                        </td>
                    </tr>
                </table>
            </EditItemTemplate>
        </asp:DataList>
    </div>
上一篇:Android中适配器的notifyDataSetChanged()为何有时不刷新


下一篇:html中datalist 是什么??????