Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create DB table from dataset table

Is it possible (in Vb.Net 2005), without manually parsing the dataset table properties, to create the table and add it to the database?

We have old versions of our program on some machines, which obviously has our old database, and we are looking for a way to detect if there is a missing table and then generate the table based on the current status of the table in the dataset. We were re-scripting the table every time we released a new version (if new columns were added) but we would like to avoid this step if possible.

like image 670
Justin Avatar asked Aug 19 '08 15:08

Justin


People also ask

How do you create a database from a dataset?

Choose the type of data source to which you'll be connecting. Choose the database or databases that will be the data source for your dataset. Choose the tables (or individual columns), stored procedures, functions, and views from the database that you want to be represented in the dataset. Click Finish.

How do I create a data table from an existing table?

A copy of an existing table can be created using a combination of the CREATE TABLE statement and the SELECT statement. The new table has the same column definitions. All columns or specific columns can be selected.

How do you create a table from another table in SQL?

Question: How can I create a SQL table from another table without copying any values from the old table? Answer: To do this, the SQL CREATE TABLE syntax is: CREATE TABLE new_table AS (SELECT * FROM old_table WHERE 1=2);


2 Answers

See this MSDN Forum Post: Creating a new Table in SQL Server from ADO.net DataTable.

Here the poster seems to be trying to do the same thing as you, and provides code that generates a Create Table statement using the schema contained in a DataTable.

Assuming this works as it should, you could then take that code, and submit it to the database through SqlCommand.ExecuteNonQuery() in order to create your table.

like image 93
Yaakov Ellis Avatar answered Nov 05 '22 15:11

Yaakov Ellis


Here is the code:

SqlConnection con = new SqlConnection("Data Source=.;uid=sa;pwd=sa123;database=Example1");
con.Open();
string sql = "Create Table abcd (";

foreach (DataColumn column in dt.Columns)
{
    sql += "[" + column.ColumnName + "] " + "nvarchar(50)" + ",";
}

sql = sql.TrimEnd(new char[] { ',' }) + ")";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();

using (var adapter = new SqlDataAdapter("SELECT * FROM abcd", con)) 
using(var builder = new SqlCommandBuilder(adapter))
{
    adapter.InsertCommand = builder.GetInsertCommand();
    adapter.Update(dt);
}
con.Close();

I hope you got the problem solved.
Here dt is the name of the DataTable.
Alternatively you can replace:

adapter.update(dt);

with

//if you have a DataSet
adapter.Update(ds.Tables[0]); 
like image 27
Prahalad Gaggar Avatar answered Nov 05 '22 17:11

Prahalad Gaggar