使用Ui层和数据层最佳实践asp.net C#填充下拉列表和checkboxlist

时间:2016-07-28 02:34:31

标签: c# asp.net drop-down-menu checkboxlist

  

我通过以下代码获得了我想要的结果。   但我的问题是我做得对吗?   如果我的代码需要修改,请发布一些修改   我是c#的新手,并试图学习如何将数据从Ui层传递到数据层

  
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:DropDownList ID="ddList" runat="server"></asp:DropDownList>

        <asp:CheckBoxList ID="cbList" runat="server"></asp:CheckBoxList>

    </div>
    </form>
</body>
</html>

using System.Data.SqlClient;
using System.Data;


namespace test2
{
    public class Data
    {
        string connection = "Data Source=Test\\SQLEXPRESS; Initial Catalog = RegistrationDB;Integrated Security=SSPI";

        SqlConnection conn = new SqlConnection();

        public Data()
        {

            conn.ConnectionString = connection;

        }


        public SqlConnection getConnection()
        {

            return conn;

        }

        public DataTable LoadCountry()
        {

            DataTable dtable = new DataTable();

            string select = "SELECT ID,country FROM dbo.Country";

            using (SqlConnection conn = new SqlConnection(connection))
            {

                SqlDataAdapter adapter = new SqlDataAdapter(select, conn);

                adapter.Fill(dtable);

                return dtable;

            }

        }


        public DataTable LoadChkboxList()
        {

            DataTable position = new DataTable();

            using (SqlConnection conn = new SqlConnection(connection))
            {

                string select = "SELECT ID,chkPosition FROM dbo.checkPosition";

                SqlDataAdapter adapter = new SqlDataAdapter(select, conn);

                adapter.Fill(position);

                return position;

            }


        }
    }
}




using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace test2
{
    public partial class UI : System.Web.UI.Page
    {
        Data datalayer = new Data();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                ddList.DataSource = datalayer.LoadCountry();
                ddList.DataTextField = "country";
                ddList.DataValueField = "ID";              
                ddList.DataBind();

                cbList.DataSource = datalayer.LoadChkboxList();
                cbList.DataTextField = "chkPosition";
                cbList.DataValueField = "ID";
                cbList.DataBind();

            }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

稍作修改

public DataTable getData(string query)
{
     DataTable dtable = new DataTable();
     using (SqlConnection con = new SqlConnection(YourConnectionString)){
         using (SqlCommand cmd = new SqlCommand(query,con){
              SqlDataAdapter adapter = new SqlDataAdapter(cmd);
              return adapter.Fill(dt);
         } 
     }
}

注意:您可以将上述功能放在一个类中并在需要的地方调用。也可以修改为接受参数。为了避免SQL注入,void内联查询,使用存储过程(最好是)。

DataTable dt Country = getData(YourQuery);

请注意:Parameterized Query and Stored Procedures