Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Creating Tables in Run time using c#

Creating Tables in Run time using c#

Scheduled Pinned Locked Moved C#
databasecsharpsql-serversysadmintutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Raghu_M
    wrote on last edited by
    #1

    I am doing Project in OPC.In that project,when ever user create Group means,it has to create in Database also in runtime.I am using Sql Server 2005 and C#.My Question is how to create table with four columns in database at run time using c#.I am waiting for your reply. Thanks and Regards M.Raghu

    P 1 Reply Last reply
    0
    • R Raghu_M

      I am doing Project in OPC.In that project,when ever user create Group means,it has to create in Database also in runtime.I am using Sql Server 2005 and C#.My Question is how to create table with four columns in database at run time using c#.I am waiting for your reply. Thanks and Regards M.Raghu

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You could always just write the SQL to create a table and execute it using a SqlCommand, but a better way to do it would be to use Sql Server Management Objects (SMO).

      private void MakeMyTable()
      {
      string connectionString = "...";
      using (SqlConnection connection = new SqlConnection(connectionString))
      {
      Server server =
      new Server(new ServerConnection(connection));

      // Create table in my personal database
      Database db = server.Databases\["MyDatabase"\];
      
      // Create new table, called TestTable
      Table newTable = new Table(db, "MyTestTable");
      
      // Create the primary key
      Column pk = GetColumn(newTable, "id", DataType.Int, false);
      pk.Identity = true;
      pk.IdentitySeed = 1;
      pk.IdentityIncrement = 1;
      
      newTable.Add(pk);
      
      DataType varchar = new DataType(SqlDataType.NVarChar, 50);
      newTable.Add(GetColumn(newTable, "Name", varchar, false));
      newTable.Add(GetColumn(newTable, "Nickname", varchar, true));
      newTable.Add(GetColumn(newTable, "Title", varchar, false));
      
      // Create a PK Index for the table
      Index index = new Index(newTable, "PK\_MyTestTable");
      index.IndexKeyType = IndexKeyType.DriPrimaryKey;
      
      // The PK index will consist of 1 column, "ID"
      index.IndexedColumns.Add(new IndexedColumn(index,"id"));
      
      // Add the new index to the table.
      newTable.Indexes.Add(index);
      
      // Physically create the table in the database
      newTable.Create();
      

      }
      }

      private Column GetGolumn(Table tb, string columnName, DataType type, bool isNullable)
      {
      Column column = new Column(tb, columnName);
      column.DataType = type;
      column.Nullable = nullable;
      }

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups