473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Changing a DataTables Schema

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);
Nov 16 '05 #1
9 1913
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
Nov 16 '05 #2
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);

Nov 16 '05 #3
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.

Nov 16 '05 #4
Jon,

I think you misunderstand. See inline:
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)
You can change the name of the data set/data table on the client side.
This has no effect on the server side. If you change the table name, then
you have to change the data adapter so that it recognizes the new table you
are trying to update. You can call Update again, but it will fail because
the table mapping is off (I believe). Also, the new columns will be
ignored. If you want to update the data into another table, then that table
must already exist, and have a schema compatable with the changes you have
made to your data table.

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
The changes you make are to the client side data set only. Any changes
you make to that do not affect the underlying DB. You will have to issue
DB-specific commands in order to modify table structures in the DB itself.
ADO.NET does not provide this for you.

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

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.


Nov 16 '05 #5
So basically I need to do it all with "ALTER TABLE" queries?

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

I think you misunderstand. See inline:

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)

You can change the name of the data set/data table on the client side.
This has no effect on the server side. If you change the table name, then
you have to change the data adapter so that it recognizes the new table you
are trying to update. You can call Update again, but it will fail because
the table mapping is off (I believe). Also, the new columns will be
ignored. If you want to update the data into another table, then that table
must already exist, and have a schema compatable with the changes you have
made to your data table.

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

The changes you make are to the client side data set only. Any changes
you make to that do not affect the underlying DB. You will have to issue
DB-specific commands in order to modify table structures in the DB itself.
ADO.NET does not provide this for you.

Nov 16 '05 #6
Jon,

Yes, or use a library like ADOX (through COM interop).

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

"Jon Brunson" <JonBrunson@NOSPAMinnovationsoftwareDOTcoPERIODu k> wrote in
message news:eG**************@TK2MSFTNGP11.phx.gbl...
So basically I need to do it all with "ALTER TABLE" queries?

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

I think you misunderstand. See inline:

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)

You can change the name of the data set/data table on the client
side. This has no effect on the server side. If you change the table
name, then you have to change the data adapter so that it recognizes the
new table you are trying to update. You can call Update again, but it
will fail because the table mapping is off (I believe). Also, the new
columns will be ignored. If you want to update the data into another
table, then that table must already exist, and have a schema compatable
with the changes you have made to your data table.

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

The changes you make are to the client side data set only. Any
changes you make to that do not affect the underlying DB. You will have
to issue DB-specific commands in order to modify table structures in the
DB itself. ADO.NET does not provide this for you.

Nov 16 '05 #7
> So basically I need to do it all with "ALTER TABLE" queries?

Yes however that is very easy to do, this is OledB the only difference is
that you for that where is OleDb.OleDb have to place SQLClient.Sql and
another connectionstring.

I hope this helps?

Cor

\\\
Public Class clsUpdate
Public Sub New()
Dim conn As New
Data.OleDb.OleDbConnection("Provider=Microsoft.Jet .OLEDB.4.0;Data
Source="C:\myAcces.mdb")
conn.Open()
Dim cmd As New OleDb.OleDbCommand("ALTER TABLE Persons " & _
"ADD myText text", conn)
doCmd(cmd)
conn.Close()
End Sub
Private Sub doCmd(ByVal cmd As Data.OleDb.OleDbCommand)
Try
cmd.ExecuteNonQuery()
Catch ex As OleDb.OleDbException
If ex.ErrorCode = -2147217887 Then Exit Sub
MessageBox.Show(ex.Message, "OleDbException")
Exit Sub
Catch ex As Exception
MessageBox.Show(ex.Message, "GeneralException")
Exit Sub
End Try
End Sub
End Class
///
Nov 16 '05 #8
Nicholas Paldino [.NET/C# MVP] wrote:
Jon,

Yes, or use a library like ADOX (through COM interop).


Thanks for your help
Nov 16 '05 #9
Cor Ligthert wrote:
So basically I need to do it all with "ALTER TABLE" queries?

Yes however that is very easy to do, this is OledB the only difference is
that you for that where is OleDb.OleDb have to place SQLClient.Sql and
another connectionstring.

I hope this helps?

Cor

\\\
Public Class clsUpdate
Public Sub New()
Dim conn As New
Data.OleDb.OleDbConnection("Provider=Microsoft.Jet .OLEDB.4.0;Data
Source="C:\myAcces.mdb")
conn.Open()
Dim cmd As New OleDb.OleDbCommand("ALTER TABLE Persons " & _
"ADD myText text", conn)
doCmd(cmd)
conn.Close()
End Sub
Private Sub doCmd(ByVal cmd As Data.OleDb.OleDbCommand)
Try
cmd.ExecuteNonQuery()
Catch ex As OleDb.OleDbException
If ex.ErrorCode = -2147217887 Then Exit Sub
MessageBox.Show(ex.Message, "OleDbException")
Exit Sub
Catch ex As Exception
MessageBox.Show(ex.Message, "GeneralException")
Exit Sub
End Try
End Sub
End Class
///


Thanks for the info. I'll go look up the ALTER TABLE syntax in the MSDN
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jade | last post by:
Hi, I just wanted to ask a quick question regarding datasets. I am creating 3 tables using a dataadapter. what i want to know is that is the relationship created between these datatables...
9
by: Jon Brunson | last post by:
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...
2
by: David Elliott | last post by:
I can create this: ?xml version="1.0" standalone="yes" ?> <ConfigOpt> <record> <Field_1>Text # 1</Field_1> <Field_2>Text # 2</Field_2> </record> </ConfigOpt>
1
by: bernardpace | last post by:
Hi, I have two DataTables with same schema(2 columns: ID, value). These 2 tables are full of rows. Now I require to check if any data(value) in table 1 already exits in table 2. Is there a...
2
by: Francisco Reyes | last post by:
Is there a way to change a schema owner other than dump/restore? How about changing the nspowner in pg_namespace? Will that do the trick without any negative consecuences? ...
0
by: Ido Flatow | last post by:
As it seems, if I define a nested class in the WS, the client that references the WS get's a generated code where the nested class is defined outside the class. I can say - "Ok, I'll play along"...
0
by: wapsiii | last post by:
How is it possible to combine/join/merge two identical (schema) datatables into a new datatable? /M
1
by: JonJon | last post by:
I'm currently using .NET Framework 2.0 with WSE 3.0. I've read many articles that DataTables are now serializable and can be passed via web services. However when I have a datatable as my return...
3
by: Joe Kovac | last post by:
Hi! I have got a very complex database schema. For example a customer is constructed within a Customer table and a Person table. I have both tables as DataTables in a DataSet. When I want to...
7
by: John Wright | last post by:
I have two datatables that I load. One I load from LDAP, the other gets loaded from Excel. I need to check to see which names are in the LDAP that are not in the Excel table and vica versa. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.