Share via


Création, modification et suppression de vues

Dans SMO (SQL Server Management Objects), les vues SQL Server sont représentées par l'objet View.

La propriété TextBody de l'objet View définit la vue. Cela revient à utiliser l'instruction Transact-SQL SELECT pour créer une vue.

Exemple

Pour utiliser un exemple de code fourni, vous devez sélectionner l'environnement, le modèle et le langage de programmation à utiliser pour créer votre application. Pour plus d'informations, consultez Procédure : créer un projet SMO Visual Basic dans Visual Studio .NET ou Procédure : créer un projet SMO Visual C# dans Visual Studio .NET.

Création, modification et suppression d'une vue en Visual Basic

Cet exemple de code montre comment créer une vue de deux tables en utilisant une jointure interne. La vue est créée en utilisant le mode texte, donc la propriété TextHeader doit être définie.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Define a View object variable by supplying the parent database, view name and schema in the constructor.
Dim myview As View
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()

Création, modification et suppression d'une vue en Visual C#

Cet exemple de code montre comment créer une vue de deux tables en utilisant une jointure interne. La vue est créée en utilisant le mode texte, donc la propriété TextHeader doit être définie.

{ 
//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(); 
}

Voir aussi

Référence