C# 通过数据库填充复选框

C# 通过数据库填充复选框,c#,asp.net,data-binding,C#,Asp.net,Data Binding,我必须用来自数据库的数据填充复选框,但我的页面上没有显示任何复选框。请告诉我正确的方法。在C#中,我编写的页面加载方法如下: public partial class dbTest1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string Server = "al2222"; string Username = "hshshshsh";

我必须用来自数据库的数据填充复选框,但我的页面上没有显示任何复选框。请告诉我正确的方法。在C#中,我编写的页面加载方法如下:

public partial class dbTest1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string Server = "al2222";
        string Username = "hshshshsh";
        string Password = "sjjssjs";
        string Database = "database1";

        string ConnectionString = "Data Source=" + Server + ";";
        ConnectionString += "User ID=" + Username + ";";
        ConnectionString += "Password=" + Password + ";";
        ConnectionString += "Initial Catalog=" + Database;
        string query = "Select * from Customer_Order where orderNumber = 17";

        using (SqlConnection conn = new SqlConnection(ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    if (!IsPostBack)
                    {
                        Interests.DataSource = dr;
                        Interests.DataTextField = "OptionName";
                        Interests.DataValueField = "OptionName";
                        Interests.DataBind();
                    }
                }
                conn.Close();
                conn.Dispose();
            }
        }
    }
}
在.aspx中,我有一个:

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


请告诉我完成此操作的正确方法。

虽然您的问题已经得到了回答(通过连接字符串注释),但我想我会加入一个可能的方法来重写此内容。我一开始是作为一个评论,但它有点长和笨拙。请注意,这并不能直接回答你的问题,但是要考虑代码的清洁性和在回发时可能的(可能非常温和)性能提升。

protected void Page_Load(object sender, EventArgs e)
{
    // If we're in postback, let's not poll the database.
    if (Page.IsPostback)
        return; // Change this if you do need some postback processing here.

    // I assume that in the real world you pull this info from web.config
    string Server = "al2222";
    string Username = "hshshshsh";
    string Password = "sjjssjs";
    string Database = "database1";

    string ConnectionString = "Data Source=" + Server + ";";
    ConnectionString += "User ID=" + Username + ";";
    ConnectionString += "Password=" + Password + ";";
    ConnectionString += "Initial Catalog=" + Database;
    string query = "Select * from Customer_Order where orderNumber = 17";

    using (SqlConnection conn = new SqlConnection(ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            // Going to assume that you're only getting 1 record 
            // due to apparent key (orderNumber = 17) in query?
            // You can also consider "if (dr.Read())", but fundamentally
            // they will do the same thing.
            while (dr.Read())
            {
                Interests.DataSource = dr;
                Interests.DataTextField = "OptionName";
                Interests.DataValueField = "OptionName";
                Interests.DataBind();
            }
            // I've excised the calls to .Close() and .Dispose(),
            // as the using block covers them for you.
        }
    }
}
我们为什么要走这条路

  • 在您的原始代码中,无论您是否在回发中,您都在每一次页面加载中轮询数据库(如果我关于单记录查询的假设是错误的,那么可能会循环)。直到你进入了这个循环中,你才开始检查回发,而在这个循环中,大部分的伤害都已经造成了。在我列出的代码中,如果您处于回发状态,您将短路掉
    Page\u Load()
    。当然,您可以将其更改为
    if
    /
    else
    ,如果还需要在回发上进行一些加载事件处理,则可以将组括起来。这也简化了循环内代码
  • 您的
    使用
    模块为您处理/关闭了连接。因此,您不需要额外的代码
  • 正如OrbMan在评论中所说,希望在实际代码中,您是从web.config文件中检索所有连接字符串信息,而不是硬编码,对吗
  • 最后一点注意:这是大量数据访问代码,较新版本的.NET Framework使用Entity Framework和LINQ to SQL等工具可以大大简化这些代码。还有第三方数据访问层工具(如亚音速和ActiveRecord)可以简化这一过程。使用这样的工具将大大减少您在这里编写的代码量——我猜您在整个应用程序中也使用了相当多的类似代码,因此这些工具将为您的开发人员提供相当多的生产力提升。(而且更简单的道路维护。)


    值得深思。

    请确认您的SQL语句已执行,并且该页面上没有其他错误。你试过运行调试器吗?因为您的代码看起来很好。在您的示例中,您应该在打开db连接和查询数据之前检查回发。请注意,不要在代码中编译连接字符串,而是将其放入
    web.config
    。谢谢!你是对的,我没有给出正确的查询字符串。谢谢