[ADO.NET] Tworzenie tabeli w bazie danych

0

Szukam info na temat w jaki sposob moge stworzyć Tabele w bazie danych sql z poziomu kodu?
Prosił bym o jakis przykład, bądź link. Znalazłem w necie takie coś, ale mi to amło mówi bo nie ma namespaców i nie wiem dokładnie skąd pochodzą używane klasy.

// Establish the database server
string connectionString = "...";
SqlConnection connection =
     new SqlConnection(connectionString);
Server server =
     new Server(new ServerConnection(connection));

// Create table in my personal database
Database db = server.Databases["davidhayden"];

// Create new table, called TestTable
Table newTable = new Table(db, "TestTable");

// Add "ID" Column, which will be PK
Column idColumn = new Column(newTable, "ID");
idColumn.DataType = DataType.Int;
idColumn.Nullable = false;
idColumn.Identity = true;
idColumn.IdentitySeed = 1;
idColumn.IdentityIncrement = 1;

// Add "Title" Column
Column titleColumn = new Column(newTable, "Title");
titleColumn.DataType = DataType.VarChar(50);
titleColumn.Nullable = false;

// Add Columns to Table Object
newTable.Columns.Add(idColumn);
newTable.Columns.Add(titleColumn);

// Create a PK Index for the table
Index index = new Index(newTable, "PK_TestTable");
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();
0

To jest słaby przykład - wykorzystanie DataTable aby stworzyć tabelę w bazie SQL - na pewno w komercyjnym produkcie u przyszłego pracodawcy nikt nie będzie tak robił.

Wystarczy użyć obiektu SQLCommand i wykonać ExecuteNonQuery().

Tutaj przykład:
http://www.codeproject.com/KB/database/adodotnetprogrammatically.aspx

1 użytkowników online, w tym zalogowanych: 0, gości: 1