Aracılığıyla paylaş


Oluşturma, değiştirme ve kaldırma bakıldı

İçinde SQL ServerManagement Objects ((SMO)), SQL Serverkez gösterilir Viewnesnesini.

TextBodyÖzelliği Viewnesnesi tanımlar görünümü. Eşdeğer olduğunu Transact-SQLbir görünüm oluşturmak için select deyimi.

Örnek

Sunulan kod örneklerinden herhangi birini kullanmak için, programlama ortamını, programlama şablonunu ve uygulamanızı oluşturacağınız programlama dilini seçmeniz gerekecektir. Daha fazla bilgi için, bkz. Visual Studio'da Visual Basic smo proje oluşturun.NET veya Visual Studio'da Visual C# smo proje oluşturun.NET.

Oluşturma, değiştirme ve Visual Basic'te bir görünüm kaldırma

Bu kod örneği, nasıl bir iç birleşim kullanarak iki tablo görünümünü oluşturmak için gösterir. Metin modu kullanarak görünüm oluşturulduğunda çok TextHeaderözelliği ayarlanmalıdır.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2012 2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2012")
'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()

Oluşturma, değiştirme ve bir Visual C# içinde kaldırma

Bu kod örneği, nasıl bir iç birleşim kullanarak iki tablo görünümünü oluşturmak için gösterir. Metin modu kullanarak görünüm oluşturulduğunda çok TextHeaderözelliği ayarlanmalıdır.

{
        //Connect to the local, default instance of SQL Server. 
        Server srv; 
        srv = new Server(); 
        //Reference the AdventureWorks2012 database. 
        Database db; 
        db = srv.Databases["AdventureWorks2012"]; 
        //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(); 
        }

{
        //Connect to the local, default instance of SQL Server. 
        Server srv; 
        srv = new Server(); 
        //Reference the AdventureWorks2012 database. 
        Database db; 
        db = srv.Databases["AdventureWorks2012"]; 
        //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(); 
        }

Oluşturma, değiştirme ve PowerShell içinde görünüm kaldırma

Bu kod örneği, nasıl bir iç birleşim kullanarak iki tablo görünümünü oluşturmak için gösterir. Metin modu kullanarak görünüm oluşturulduğunda çok TextHeaderözelliği ayarlanmalıdır.

# Set the path context to the local, default instance of SQL Server and get a reference to AdventureWorks2012
CD \sql\localhost\default\databases
$db = get-item Adventureworks2012

# Define a View object variable by supplying the parent database, view name and schema in the constructor. 
$myview  = New-Object -TypeName Microsoft.SqlServer.Management.SMO.View `
-argumentlist $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();

# Set the path context to the local, default instance of SQL Server and get a reference to AdventureWorks2012
CD \sql\localhost\default\databases
$db = get-item Adventureworks2012

# Define a View object variable by supplying the parent database, view name and schema in the constructor. 
$myview  = New-Object -TypeName Microsoft.SqlServer.Management.SMO.View `
-argumentlist $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();

Ayrıca bkz.

Başvuru

View