2010-04-15 106 views
0

我必须使用来自数据库的数据填充复选框,但是在我的页面上没有显示复选框。请让我知道正确的方法来做到这一点。在C#中,我写的Page_Load方法是这样的:通过数据库填充复选框

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> 

请告诉我做到这一点的正确方法。

+0

请验证您的SQL语句是否执行,并且该页面上没有其他错误。你有没有试过运行调试器?因为你的代码看起来很好。 – 2010-04-15 15:34:06

+0

在您的示例中,您应该在打开数据库连接和查询数据之前检查回传。 – 2010-04-15 15:35:00

+1

在一个不相关的说明中,不是在代码中编译连接字符串,而是将其放在'web.config'中。 – RedFilter 2010-04-15 15:37:09

回答

1

虽然你的问题已经被回答了(通过连接字符串评论),但我还是想用一种可能的方式来重写这个。我是以评论开始的,但它有点长,笨重。请注意,这并不能直接回答你的问题,但是对于代码清洁和回发可能(可能非常温和)的性能提升,这是需要考虑的事情。

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. 
     } 
    } 
} 

我们为什么要走这条路?

  1. 在你的原代码,你是查询数据库(和潜在的循环,如果我的约,作为一个单记录查询的假设是错误的)每个页面负载,不论你是在回发。你没有检查回传,直到你进入循环,那里的伤害大部分已经完成。在我列出的代码中,如果您在回发中,则完全将Page_Load()短路。当然,您可以将其更改为if/else,并将括号括在组中,如果您还需要对回发进行一些加载事件处理。这也简化了你的循环代码。
  2. 您的using区块涵盖了为您处理/关闭连接。因此,你不需要额外的代码。
  3. OrbMan在评论中指出,希望在您的实际代码中,您可以从web.config文件中检索所有连接字符串信息,而不是对其进行硬编码,对吗?

最终最终无关注:这是很多数据访问代码,新版本的.NET Framework使用Entity Framework和LINQ to SQL等工具大大简化了数据访问代码。还有第三方数据访问层工具(如SubSonic和ActiveRecord)可以简化这个工作。使用这些工具会大大减少你在这里编写的代码数量 - 而且我猜你在整个应用程序中都使用了相当多的类似代码,所以这些工具将为你提供开发者相当提高生产力。 (而且更简单的道路维护。)

只是思考的食物。