asp.net 如何更改DataTable列名称?

46qrfjad  于 2023-10-21  发布在  .NET
关注(0)|答案(7)|浏览(151)

我有一个DataTable,它有四列,如

StudentID        CourseID          SubjectCode            Marks    
------------     ----------        -------------          --------
    1               100              MT400                  80
    2               100              MT400                  79
    3               100              MT400                  88

在这里,我通过将此数据表作为XML表传递,将此数据表插入到SQL Server表中。
我只是想将DataTable列名“Marks”更改为“SubjectMarks”,并将此DataTable作为XML Table传递。
我知道如何将DataTable作为XML Table传递。但我不知道,如何改变数据表列名称“标记”为“主题标记”。

raogr8fs

raogr8fs1#

试试这个:

dataTable.Columns["Marks"].ColumnName = "SubjectMarks";
8gsdolmq

8gsdolmq2#

通过执行以下操作对色谱柱进行扫描:

dataTable.Columns["ColumnName"].ColumnName = "newColumnName";
hm2xizp9

hm2xizp93#

用途:

dt.Columns["Name"].ColumnName = "xyz";
dt.AcceptChanges();

dt.Columns[0].ColumnName = "xyz";
dt.AcceptChanges();
8iwquhpp

8iwquhpp4#

dtTempColumn.Columns["EXCELCOLUMNS"].ColumnName = "COLUMN_NAME";                        
 dtTempColumn.AcceptChanges();
ulydmbyx

ulydmbyx5#

试试这个

"columns": [
{data: "id", name: "aaa", sortable: false},
{data: "userid", name: "userid", sortable: false},
{data: "group_id", name: "group_id", sortable: false},
{data: "group_name", name: "group_name", sortable: false},
{data: "group_member", name: "group_member"},
{data: "group_fee", name: "group_fee"},
{data: "dynamic_type", name: "dynamic_type"},
{data: "dynamic_id", name: "dynamic_id"},
{data: "content", name: "content", sortable: false},
{data: "images", name: "images", sortable: false},
{data: "money", name: "money"},
{data: "is_audit", name: "is_audit", sortable: false},
{data: "audited_at", name: "audited_at", sortable: false}

]

g9icjywg

g9icjywg6#

在生成XML之后,你可以用<SubjectMarks>... content here </SubjectMarks>tag替换你的XML <Marks>... content here </Marks>标签。并将更新后的XML传递到数据库。
您的XML生成如下。

<NewDataSet>
      <StudentMarks> 
          <StudentID>1</StudentID>
          <CourseID>100</CourseID>
          <SubjectCode>MT400</SubjectCode>
          <Marks>80</Marks>
      </StudentMarks>
      <StudentMarks> 
          <StudentID>1</StudentID>
          <CourseID>100</CourseID>
          <SubjectCode>MT400</SubjectCode>
          <Marks>79</Marks>
      </StudentMarks>
      <StudentMarks> 
          <StudentID>1</StudentID>
          <CourseID>100</CourseID>
          <SubjectCode>MT400</SubjectCode>
          <Marks>88</Marks>
      </StudentMarks>
  </NewDataSet>

在这里,您可以将XML分配给字符串变量,如

string strXML = DataSet.GetXML();

strXML = strXML.Replace ("<Marks>","<SubjectMarks>");
strXML = strXML.Replace ("<Marks/>","<SubjectMarks/>");

然后将strXML传递给数据库。

snvhrwxg

snvhrwxg7#

使用此

dataTable.Columns["OldColumnName"].ColumnName = "NewColumnName";

相关问题