0

Is it possible, without writing any SQL or using any 3rd party libraries to create a database schema from several ADO.NET DataTables?

3
  • Ultimately you will have to execute a CREATE TABLE statement, dont you think? You can create a class that encapsulates the process using metadata from the DataTable, of course. Commented Jul 11, 2011 at 23:22
  • I would not like to write any SQL. Commented Jul 11, 2011 at 23:27
  • How do you expect any table(s) to be created then? I think essentially your question should be -- "Does the .NET Framework come with any functions that automatically create an sql table from a DataTable?" To which the answer is no, as far as I know. Commented Jul 11, 2011 at 23:28

3 Answers 3

2

Not sure how "not sql" this is, but you can use SQL Server Management Objects.

Sign up to request clarification or add additional context in comments.

Comments

0

Its easy to make a function that writes the SQL for you. However if you don't want to use SQL at all I would think you're out of luck:

    private string GetSqlCommand(Dictionary<string, Type> schema, string tableName, bool includeIdentity)
    {
        string sql = "CREATE TABLE [" + tableName + "] (\n";

        if (includeIdentity)
            sql += "[ID] int not null Identity(1,1) primary key,\n";

        foreach (KeyValuePair<string, Type> info in schema)
        {
            sql += "[" + info.Key + "] " + SQLGetType(info.Value) + ",\n";
        }

        sql = sql.TrimEnd(new char[] { ',', '\n' }) + "\n";

        return sql + ")";
    }

Comments

0

Here is the code

SqlConnection con = connection string;
        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();
con.close();

I am giving a predefined table-name abcd.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.