DataSet – 类模型 – 如何从DataSet获取Bool值

我是C#和ASP.net的新手。 我是ColdFusion程序员,但我正在接触ASP.net。

我会详细介绍然后问我的问题….

我已经能够从服务层类文件(或业务逻辑层)后面的代码调用它,然后在那个调用数据访问层类文件。 我一直在从数据访问层发送一个DataSet,然后在Code Behind中将代码移到一个表中,这样我就可以逐行读取它。

当我在编辑屏幕上时,我正在寻找一种获取布尔值的方法,以便在我找到此post时设置复选框以进行检查。

C#为DataRow赋值[“haswhatnots”] = hasWhatnots非常慢

我看到提到他们应该……

或者 – 使用类模型而不是DataTable,如果它是我认为的那么听起来很棒。

使用类文件来回退数据以及数据应该采用何种格式的CRUD屏幕的最佳方法是什么?

我习惯于在QuerySet中返回ColdFusion数据,然后我们可以遍历它。 我们可以将他的查询转换为Array of Structures或者Array of Obects,XML或JSon,但是我仍然遇到了解我如何从数据集中获取数据的问题。 我现在得到String Data很好,但也许有一个非常简单的例子可能有一页……

我想弄清楚如何立即获取布尔值是我当前的问题。

感谢任何帮助,内森

PS如果有更好的更专业的方式来做到这一点让我知道。 我想继续使用(Presentation / CodeBehind / Service / Data Access)类型的图层。

Protected void Page_Load(Object sender, EventArgs e) { if(!IsPostBack) { DataTable dt = new DataTable(); DataRow dr; PageService myPage = new PageService(); DataSet ds = myPage.getPages(); int rowCount = ds.Tables[0].Rows.Count; ds.Columns.Add(new DataColumn("PageID", typeof(Int32))); ds.Columns.Add(new DataColumn("Name", typeof(String))); ds.Columns.Add(new DataColumn("PageHTMLContent", typeof(String))); ds.Columns.Add(new DataColumn("NavigationText", typeof(String))); ds.Columns.Add(new DataColumn("TopMenu", typeof(Boolean))); ds.Columns.Add(new DataColumn("SubMenu", typeof(Boolean))); ds.Columns.Add(new DataColumn("DisplayOrder", typeof(Int32))); ds.Columns.Add(new DataColumn("Active", typeof(Boolean))); for (int i = 0; i < rowCount; i++) { dr = dt.NewRow(); dr[0] = i; dr[1] = i.ToString; dr[2] = i.ToString; dr[3] = i.ToString; dr[4] = i; // Not sure what to do here for the Boolean Value dr[5] = i; // Not sure what to do here for the Boolean Value dr[6] = i; dr[7] = i; // Not sure what to do here for the Boolean Value } ds.Tables.Add(dt); foreach (DataRow dataRow in ds.Tables[0].Rows) { PageIDHiddenField.Value = dataRow["PageID"]; NameTextBox.Text = dataRow["Name"].ToString(); PageHTMLContentTextBox.Text = dataRow["PageHTMLContent"]; NavigationTextTextBox.Text = dataRow["NavigationText"]; TopMenuCheckBox.Checked = dataRow["TopMenu"]; // Not sure what to do here for the Boolean Value SubMenuCheckBox.Checked = dataRow["SubMenu"]; // Not sure what to do here for the Boolean Value DisplayOrder.Text = dataRow["DisplayOrder"].ToString(); ActiveCheckBox.Checked = dataRow["Active"]; // Not sure what to do here for the Boolean Value } } } 

您需要将值转换为正确的类型。 因为dataRow的indexer属性将返回object类型的object ,所以需要将其强制转换为正确的类型。 因此,您需要将其BooleanBoolean如下所示:

 TopMenuCheckBox.Checked = (bool)dataRow["TopMenu"]; 

要执行一个循环来迭代DataSet的行,您可以执行以下操作:

 DataSet ds = myPage.getPages(); foreach (DataRow row in ds.Tables[0].Rows) { PageIDHiddenField.Value = row["PageID"]; NameTextBox.Text = (string)row["Name"]; ... TopMenuCheckBox.Checked = (bool)row["TopMenu"]; } 

您可以使用DataRow的indexer属性来获取特定列。