本文介绍了更改DataTables架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 是否可以使用DataAdapter填充DataTable,更改该DataTable的 DataColumns(甚至可能是它的名字),然后提交 那些对数据库的更改(在我的例子中是SQL Server 2000)? 例如: [VB.NET] Dim dc作为新的SqlCommand(SELECT TOP 0 * FROM SomeTable, SomeSqlConnection) dc.CommandType = CommandType.Text Dim da As New SqlDataAdapter(dc) Dim dt As New DataTable da.FillSchema(dt,SchemaType.Source) dt.TableName =" NewTableName" dt.Columns(" SomeColumn")。ColumnName =" NewName" dt.Columns(" AnotherColumn")。DataType = GetType(String) dt.Columns.Add(" NewColumn",GetType(Integer)) da.Update(dt) [C#] SqlCommand dc = new SqlCommand(" SELECT TOP 0 * FROM SomeTable",&br /> SomeSqlConnection); dc.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(dc); DataTable dt = new DataTable(); da.FillSchema(dt,SchemaType.Source); dt.TableName =" NewTableName" ;; dt.Columns [&SomeDolumn"]。ColumnName =" NewName"; dt.Columns [" AnotherColumn"]。DataType = Type.GetType(" ; System.String"); dt.Columns.Add(" NewColumn",Type.GetType(" System.Integer")); da.Update(dt); 解决方案 Is it possible to use a DataAdapter to fill a DataTable, change theDataColumns of that DataTable (and maybe even it''s name) and then committhose changes to the database (in my case SQL Server 2000)?Eg:[VB.NET]Dim dc As New SqlCommand("SELECT TOP 0 * FROM SomeTable",SomeSqlConnection)dc.CommandType = CommandType.TextDim da As New SqlDataAdapter(dc)Dim dt As New DataTableda.FillSchema(dt, SchemaType.Source)dt.TableName = "NewTableName"dt.Columns("SomeColumn").ColumnName = "NewName"dt.Columns("AnotherColumn").DataType = GetType(String)dt.Columns.Add("NewColumn", GetType(Integer))da.Update(dt)[C#]SqlCommand dc = new SqlCommand("SELECT TOP 0 * FROM SomeTable",SomeSqlConnection);dc.CommandType = CommandType.Text;SqlDataAdapter da = new SqlDataAdapter(dc);DataTable dt = new DataTable();da.FillSchema(dt, SchemaType.Source);dt.TableName = "NewTableName";dt.Columns["SomeColumn"].ColumnName = "NewName";dt.Columns["AnotherColumn"].DataType = Type.GetType("System.String");dt.Columns.Add("NewColumn", Type.GetType("System.Integer"));da.Update(dt); 解决方案 这篇关于更改DataTables架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-22 17:11