Simply adding a table

  • Thread starter Thread starter Thomas Krause
  • Start date Start date
T

Thomas Krause

What is the best way to add a new table to a database?

If possible I don't want to use SQL, but ADO.Net directly.

I'm a Ado-Newbie and tried the following:

dataSet.Tables.Add("xyz");
adapter.Update(dataSet);

but this does not work. I believe that Adapter.Update() only works for
Tables that already exist in the Database. But what is the alternative if I
don't like the SQL solution (CREATE TABLE ...)?

I cannot create the tables directly. I have to make that at runtime.

Thanks,
Thomas Krause
 
Hi,

When you call dataSet.Tables.Add it adds the DataTable to the DataSet, not
to the database. Since there is no built-in .NET managed library, that
allows to create table in a database, I would suggest to use CREATE TABLE
statement or search for some third-party library that does this for you. In
a case of SQL Server you could also use COM-based SQL DMO library and in a
case of Access database use COM-based ADOX
 
You have no other option. You need to use DDL (such as CREATE TABLE)
statements to modify the structure of a database.

Even if you were to perform this with a SqlCommand object, you'd still have
to set the CommandText property to the DDL statement.

HTH
 
Thanks for your answers

My solution is now to make an createTable function that takes a DataTable as
an argument and adds it by building an SQL statement.

So I can now construct DataTables and then add them to the database with a
single call.

Thanks,
Thomas Krause
 
Back
Top