Tag: datarow

C#reflection:从类型化数据集中获取DataRow的字段

我目前正在构建一个方法,该方法从类型化的DataSet中获取DataRow类型的对象,然后返回DataRow中字段的JSON格式的字符串(用于Web服务)。 通过使用System.Reflection ,我正在做这样的事情: public string getJson(DataRow r) { Type controlType = r.GetType(); PropertyInfo[] props = controlType.GetProperties(); foreach (PropertyInfo controlProperty in props) { } return “”; } 然后在foreach语句中,我将迭代每个字段并获取字段名称和值,并将其格式化为JSON。 问题是当迭代props (类型为PropertyInfo[] )时,我得到的属性我不想被迭代: 替代文字http://img88.imageshack.us/img88/2001/datarowreflectionht0.gif 从上图中可以看出,我只需要props数组中0 – 11的字段,因为这些是这个特定类型行的“真实字段”。 所以我的问题是, 如何只获取Typed DataRow的字段,而不是其他’元数据’? [更新解决方案] 正如Mehrdad Afshari建议的那样,我使用的是Table.Columns数组,而不是使用Reflection 。 这是完成的function: public string GetJson(DataRow r) { int index = 0; StringBuilder json = new […]

比较两个数据行

我有一个数据表,里面有多行。 我还有一行,我想检查这一行是否与数据表中现有行重复。 所以我尝试过: DataTable dataTable = GetTable(); if (dataTable.Rows.Count > 1) { for (int i = 0; i < dataTable.Rows.Count; i++) { var dataRow = dataTable.Rows[i]; if (dt.Rows.Contains(dataRow) && dt.Rows.Count != 0) // Giving error continue; dt.ImportRow(dataRow); return dataRow; } } 这里,我的dataTable也是第一次为null / empty。 但它给出的错误是: 表没有主键。 任何人都可以帮助我。 如果需要其他代码,请注释。

如果数据表的主键是两列,可以使用DataTable.Contains(对象键)吗?

如果是这样的话?

在C#中更新DataRow有问题

我有一个非常简单的C#DataTable问题,我似乎无法解决这个问题。 它看起来如此紧张,但我必须遗漏一些东西。 我希望有人可以向我解释为什么我无法更新DataTable中的单元格值,如下所示: 码: DataTable t = new DataTable(); t.Columns.Add(“MyCol”); t.Rows.Add(“old value”); t.Rows[0].ItemArray[0] = “new value”; t.AcceptChanges(); dataGridView1.DataSource = t; //did not work. still reads “old value” 任何帮助,将不胜感激! 谢谢!

使用C#var进行System.Data.Datarow的隐式类型

foreach (var row in table.Rows) { DoSomethingWith(row); } 假设我正在使用标准的System.Data.DataTable (它有一个System.Data.DataRow对象的集合),上面的变量’row’解析为object类型,而不是System.Data.DataRow 。 foreach (DataRow row in table.Rows) { DoSomethingWith(row); } 像我期望的那样工作。 这有什么特别的原因吗? 谢谢。

如何使用searchstring搜索数据表中的行?

嗨,我想在我的DataTable中搜索行,我试试这个: protected void imggastsuche_Click(object sender, EventArgs e) { string searchstring = txtgastsuche.Text; DataTable tb = DataBaseManager.GetDataTable(mysqlconnectionstring); DataRow[] foundRows = tb.Select(“FIRSTNAME,LASTNAME,NAME,COMPANY,TIMEFROM,TIMETO,CREATOR Like ‘%” + searchstring + “%'”); tb = foundRows.CopyToDataTable(); this.ListView.DataSource = tb; this.ListView.DataBind(); } 但我的字符串中有错误。 如果我想搜索这个列,我该怎么办?

如何循环数据表中特定列的值?

我想循环遍历datatable中特定列的值? 任何人都可以请给C#编码?

具有单引号冲突的DataTable select方法C#

我最近发现当我在包含撇号的字段下进行LINQ选择时,它会使我的应用程序抛出exception。 DataRow[] newDr = this.ds.Tables[“TableName”].Select(“Name = ‘” + drData[“Name”].ToString() + “‘”); 如果drData [“姓名”] =“商家的钱” I got an exception of “Syntax error: Missing operand after ‘S’ operator ” 任何人都可以告诉我如何保存它,不要更换它或删除它,好吗?

使用AutoMapper将DataRow转换为Object

我可以成功地从IDataReader映射到对象列表,但是当我想要一个DataRow它似乎没有按预期工作。 我错过了一些简单的东西吗? [TestFixture] public class AutomapperTest { [Test] public void TestMethod1() { DataTable dt = new DataTable(“contact”); dt.Columns.Add(“FirstName”); dt.Columns.Add(“LastName”); dt.Columns.Add(“Line1”); dt.Columns.Add(“Line2”); dt.Columns.Add(“Line3”); dt.Columns.Add(“Suburb”); dt.Columns.Add(“State”); dt.Columns.Add(“Postcode”); DataRow row = dt.NewRow(); row.ItemArray = new [] { “Little”, “Johnny”, “1 Random Place”, “”, “”, “Windsor”, “Qld”, “4030” }; var dest = Mapper.DynamicMap(row); Assert.AreEqual(row[“FirstName”], “Little”); Assert.IsNotNull(dest); Assert.AreEqual(dest.FirstName, “Little”); } […]

无法更改DataRow值

我想更改Datarow值。 我想改变DT2[0].ItemArray[3]我尝试了这段代码,但它没有用。 private void Func(DataRow[] DtRowTemp) { DataRow[] DT2 ; DT2 = DtRowTemp; // It work and DTRowTemp value has set to DT2 // above code don’t work DT2[0].ItemArray[3] = 3; // Last Value Was 2 and I want to change this value to 3 }