Tworzenie, zmienianie i usuwanie tabel
W SQL Server Obiekty zarządzania obiekty (SMO), tabele są reprezentowane przez Table obiekt. W hierarchii obiektów za pomocą obiektów SMO Table obiekt jest poniżej Database obiekt.
Przykład
Aby używać dostarczonych przykładów kodu źródłowego, należy wybrać środowisko, szablon oraz język programowania, które będą używane do tworzenia aplikacji.Aby uzyskać więcej informacji zobacz Jak Tworzenie obiektów SMO projektu Visual Basic w programie Visual Studio .NET lub Jak Tworzenie projektu programu Visual C# obiekty SMO w programie Visual Studio .NET.
Tworzenie, zmienianie i usuwanie tabela z poziomu języka Visual Basic
Ten przykładowy kod tworzy tabela, która ma kilka kolumn z różnymi typami i celów.Kod zawiera również przykłady sposobów Utwórz pole tożsamości, utworzenie klucz podstawowy oraz zmienić właściwości tabela.
Tworzenie, zmienianie i usuwanie tabela w środowisku Visual C#
Ten przykładowy kod tworzy tabela, która ma kilka kolumn z różnymi typami i celów.Kod zawiera również przykłady sposobów Utwórz pole tożsamości, utworzenie klucz podstawowy oraz zmienić właściwości tabela.
{
//Connect to the local, default instance of SQL Server.
Server srv;
srv = new Server();
//Reference the AdventureWorks database.
Database db;
db = srv.Databases("AdventureWorks");
//Define a Table object variable by supplying the parent database and table name in the constructor.
Table tb;
tb = new Table(db, "Test_Table");
//Add various columns to the table.
Column col1;
col1 = new Column(tb, "Name", DataType.NChar(50));
col1.Collation = "Latin1_General_CI_AS";
col1.Nullable = true;
tb.Columns.Add(col1);
Column col2;
col2 = new Column(tb, "ID", DataType.Int);
col2.Identity = true;
col2.IdentitySeed = 1;
col2.IdentityIncrement = 1;
tb.Columns.Add(col2);
Column col3;
col3 = new Column(tb, "Value", DataType.Real);
tb.Columns.Add(col3);
Column col4;
col4 = new Column(tb, "Date", DataType.DateTime);
col4.Nullable = false;
tb.Columns.Add(col4);
//Create the table on the instance of SQL Server.
tb.Create();
//Add another column.
Column col5;
col5 = new Column(tb, "ExpiryDate", DataType.DateTime);
col5.Nullable = false;
tb.Columns.Add(col5);
//Run the Alter method to make the change on the instance of SQL Server.
tb.Alter();
//Remove the table from the database.
tb.Drop();
}
See Also
Reference
Table