c#.net怎么根据表中列名查出对应的列内容.就是让列名和内容一一
c#吧
全部回复
仅看楼主
level 1
凌晨炫火 楼主
#region 导出数据XML
protected void ExportXmlEntity(object sender, DirectEventArgs e)
{
// 根节点添加到文档中
XmlDocument doc = new XmlDocument();
XmlElement Root = doc.CreateElement("data");//主内容
doc.AppendChild(Root);
XmlElement Child = doc.CreateElement("ROWSET");
XmlAttribute attr = doc.CreateAttribute("tableName");
attr.Value = "jm_pre_cont";
Child.Attributes.Append(attr);
Root.AppendChild(Child);
// 获取表结构
string tableName = "JM_PRE_CONT";
string sql = "SELECT NAME AS FIELD_NAME,LENGTH AS FIELD_LENGTH FROM syscolumns WHERE id=object_id(*" + tableName + "*)";
List<FIELD_PRO> fieldList = services.ModelHelper .GetEntityListBySql<FIELD_PRO> (sql);
// 获取表数据
List<JM_PRE_CONT> actions = services.ModelHelper.GetEntityList<JM_PRE_CONT>();
DataTable actionDT = ListToDataTable(actions);
for (int i = 0; i < actions.Count;i++ )
{
XmlElement Child1 = doc.CreateElement("ROW");
XmlAttribute attr1 = doc.CreateAttribute("action");
attr1.Value = "insert";
Child1.Attributes.Append(attr1);
Child.AppendChild(Child1);
for (int j = 0; j < fieldList.Count;j++ )
{
XmlElement field = doc.CreateElement("Field");
XmlAttribute fieldName = doc.CreateAttribute("fieldName");//列名
XmlAttribute fieldLength = doc.CreateAttribute("maxLength");//列的长度
fieldName.Value = fieldList[j].FIELD_NAME ;
fieldLength.Value = fieldList[j].FIELD_LENGTH ;
field.InnerText = actionDT.Rows[i][j].ToString();
field.Attributes.Append(fieldName);
field.Attributes.Append(fieldLength);
Child1.AppendChild(field);
}
}
doc.Save("F://1.xml");//保存这个xml 网页或exe 都可以
}
#endregion
public static DataTable ListToDataTable<T>(List<T> entitys)
{
//检查实体集合不能为空
if (entitys == null || entitys.Count < 1)
{
return new DataTable();
}
//取出第一个实体的所有Propertie
Type entityType = entitys[0].GetType();
PropertyInfo[] entityProperties = entityType.GetProperties();
//生成DataTable的structure
//生产代码中,应将生成的DataTable结构Cache起来
DataTable dt = new DataTable("dt");
for (int i = 0; i < entityProperties.Length; i++)
{
//dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType);
dt.Columns.Add(entityProperties[i].Name);
}
//将所有entity添加到DataTable中
foreach (object entity in entitys)
{
//检查所有的的实体都为同一类型
if (entity.GetType() != entityType)
{
throw new Exception("要转换的集合元素类型不一致");
}
object[] entityValues = new object[entityProperties.Length];
for (int i = 0; i < entityProperties.Length; i++)
{
entityValues[i] = entityProperties[i].GetValue(entity, null);
}
dt.Rows.Add(entityValues);
}
return dt;
}
}
2014年07月25日 01点07分 1
level 1
凌晨炫火 楼主
这好似
正确的
输出
2014年07月25日 01点07分 2
level 1
凌晨炫火 楼主
这个是我的输出。明显的列名和内容没有一一对应,
2014年07月25日 01点07分 3
level 1
凌晨炫火 楼主
求大神帮帮我吧。这个我不会弄了,急死我了。
2014年07月25日 01点07分 4
level 12
看到这些代码,我只能说一句:擦!
原因很简单,我看不懂……不要打我
2014年07月25日 01点07分 5
这个是用.net生成XML特定的格式。。你不会做吗
2014年07月25日 02点07分
我用过LINQ toSQL,这个没用过。
2014年07月25日 22点07分
level 13
[汗]楼下怎么看?
2014年07月26日 17点07分 7
level 9
C#处理xml还是很简单的 度娘一下 无非就增删改查
2014年07月27日 09点07分 8
1