Udostępnij przez


Tworzenie, zmienianie i usuwanie widoków

W SQL Server Obiekty zarządzania (obiekty SMO) SQL Server widoki są reprezentowane przez View obiekt.

The TextBody() właściwość of the View object defines the view. Jest to równoważne Transact-SQL Instrukcja SELECT tworzenia widoku.

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 widoku w języku Visual Basic

W przykładzie kodu pokazano, jak utworzyć widok dwóch tabel za pomocą łączyć wewnętrzne.Widok jest tworzony w trybie tekstowym, to TextHeader() należy ustawić właściwość.

Tworzenie, zmienianie i usuwanie widoku w środowisku Visual C#

W przykładzie kodu pokazano, jak utworzyć widok dwóch tabel za pomocą łączyć wewnętrzne.Widok jest tworzony w trybie tekstowym, to TextHeader() należy ustawić właściwość.

{ 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//Reference the AdventureWorks database. 
Database db; 
db = srv.Databases("AdventureWorks"); 
//Declare a Table object variable and reference the Product table. 
Table tb; 
tb = db.Tables("Product", "Production"); 
//Define a Rule object variable by supplying the parent database, name, and schema in the constructor. 
//Note that the full namespace must be given for the Rule type to differentiate it from other Rule types. 
Microsoft.SqlServer.Management.Smo.Rule ru; 
ru = new Rule(db, "TestRule", "Production"); 
//Set the TextHeader and TextBody properties to define the rule. 
ru.TextHeader = "CREATE RULE [Production].[TestRule] AS"; 
ru.TextBody = "@value BETWEEN GETDATE() AND DATEADD(year,4,GETDATE())"; 
//Create the rule on the instance of SQL Server. 
ru.Create(); 
//Bind the rule to a column in the Product table by supplying the table, schema, and 
//column as arguments in the BindToColumn method. 
ru.BindToColumn("Product", "SellEndDate", "Production"); 
//Unbind from the column before removing the rule from the database. 
ru.UnbindFromColumn("Product", "SellEndDate", "Production"); 
ru.Drop(); 
//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 View object variable by supplying the parent database, view name and schema in the constructor. 
View myview; 
myview = new View(db, "Test_View", "Sales"); 
//Set the TextHeader and TextBody property to define the view. 
myview.TextHeader = "CREATE VIEW [Sales].[Test_View] AS"; 
myview.TextBody = "SELECT h.SalesOrderID, d.OrderQty FROM Sales.SalesOrderHeader AS h INNER JOIN Sales.SalesOrderDetail AS d ON h.SalesOrderID = d.SalesOrderID"; 
//Create the view on the instance of SQL Server. 
myview.Create(); 
//Remove the view. 
myview.Drop(); 
}

See Also

Reference

View