更改DataTables架构 [英] Changing a DataTables Schema

查看:73
本文介绍了更改DataTables架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用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);

解决方案

Jon,


是的但不是结果你想要。


只更新未更改的名称。


我想你想用这个更改数据库名称是什么不是

可能。


我希望这对此有帮助吗?


Cor


乔恩,<无线电通信/>

这是可能的,但您必须更改Update,Insert和

DeleteCommand属性以反映执行相关的命令
$ b另一个桌子上你想要更新的$ b操作。


另外,你必须确保你在

数据集中做出的任何改变(如你想要更新的新表中的数据,而不是架构)必须有意义(例如,行的编辑需要预先存在

行)。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Jon Brunson" < JonBrunson @ NOSPAMinnovationsoftwareDOTcoPERIODu k>写在

消息新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...

是吗可以使用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(&S) ELECT 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);



所以确认:


我可以*通过下载来改变数据库中表格的名称。 (使用

一个DataAdapter)将它放入DataTable,更改TableName属性,并且

upload它回到数据库(使用相同的DataAdapter的

Update()方法)


我*可以*在表中添加新列,以及有那些上传的进入

数据库以及


我可以*重命名表中的现有列,并将它们重命名为

数据库


我*可以*更改表格中列的数据类型,并在数据库中反映出



如果是这样,怎么样?由于我发布的代码不起作用。


Nicholas Paldino [.NET / C#MVP]写道:

Jon,
有可能,但您必须更改Update,Insert和
DeleteCommand属性,以反映在要更新的其他表上执行关联的
操作的命令。

此外,您必须确保在
数据集中做出的任何更改(就数据而非架构而言)必须在您想要的新表中有意义更新(例如,行的编辑需要有一个预先存在的行)。

希望这会有所帮助。



Is it possible to use a DataAdapter to fill a DataTable, change the
DataColumns of that DataTable (and maybe even it''s name) and then commit
those 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.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",
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);

解决方案

Jon,

Yes however not with the result you want.

Only the names that are not changed will be updated.

I think you want to change the database names with this what is not
possible.

I hope this helps anyway?

Cor


Jon,

It is possible, but you will have to change the Update, Insert, and
DeleteCommand properties to reflect the commands to perform the associated
operations on the other table that you want to update.

Also, you have to make sure that whatever changes you make in the
dataset (as far as data, not schema) have to make sense in the new table you
want to update (for example, an edit of a row needs to have a pre-existing
row).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Brunson" <JonBrunson@NOSPAMinnovationsoftwareDOTcoPERIODu k> wrote in
message news:%2****************@TK2MSFTNGP11.phx.gbl...

Is it possible to use a DataAdapter to fill a DataTable, change the
DataColumns of that DataTable (and maybe even it''s name) and then commit
those 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.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",
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);



So to confirm:

I *can* change the name of a table in a database by "downloading" (with
a DataAdapter) it into a DataTable, changing the TableName property, and
"uploading" it back to the database (using the same DataAdapter''s
Update() method)

I *can* add new columns to said table, and have those "uploaded" into
the database as well

I *can* rename existing columns in the table, and have them renamed in
the database

I *can* change the data type of a column in the table, and have that
reflected in the database

If so, how? As the code I orginally posted does not work.

Nicholas Paldino [.NET/C# MVP] wrote:

Jon,

It is possible, but you will have to change the Update, Insert, and
DeleteCommand properties to reflect the commands to perform the associated
operations on the other table that you want to update.

Also, you have to make sure that whatever changes you make in the
dataset (as far as data, not schema) have to make sense in the new table you
want to update (for example, an edit of a row needs to have a pre-existing
row).

Hope this helps.



这篇关于更改DataTables架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆