Add a runtime table to the database

I hav a datatable dt that I create at runtime, now I want to insert this datatable into my database, and this table does not exist in my database. I just want to add this table to the collection of database tables.

+3
source share
2 answers

You can create a table in the database by issuing the correct DML for it .

If you create a team that creates a new table for you, you can fill it out.

For instance:

CREATE TABLE dbo.myNewTable
   id INT NOT NULL IDENTITY (1,1),
   text VARCHAR (50) NOT NULL

Not knowing which database and how you connect to it, it is difficult to give more detailed information.

0
source

You need to create a SQL script that will create the table manually. Something like that:

DataTably myTable = GetTable();
var sql = new StringBuilder();
sql.Append( "CREATE TABLE [").Append( myTable.TableName ).AppendLine( "] (");
foreach( DataColumn clm in myTable.Columns ) {
    sql.Append( "[" ).Append( clm.ColumnName ).Append( "] " );
    if ( clm.DataType == typeof( int ) ) { sql.Append( "int" ); }
    else if ( clm.DataType == ... ) { ... }

    ...
}
sql.Append( ")" );
0
source

Source: https://habr.com/ru/post/1758685/


All Articles