Aracılığıyla paylaş


SQL Server örneği için bağlanma

İlk programlama adım bir SQL ServerManagement Objects (smo) uygulamasıdır örneğini oluşturmak için Servernesne örneği ile bağlantı kurmak için Microsoft   SQL Server.

Bir örneğini oluşturabileceğiniz Servernesne ve örneği bağlantı kurmak SQL Serverüç yolla. İlk kullanan bir ServerConnectionbağlantı bilgilerini sağlamak için nesne değişkenini. İkinci bağlantı bilgilerini açıkça belirleyerek sağlamaktır Servernesne özellikleri. Üçüncü adı geçmektir SQL Serverde örnek Servernesne yapıcısı.

ServerConnection nesnesi kullanma

Kullanmanın avantajı ServerConnectionnesne değişkeni olan bağlantı bilgilerini kullanılabilmesinden. Beyan bir Servernesne değişkeni. Sonra ilan bir ServerConnectionnesne ve özellikler ile örneği adı gibi bağlantı bilgilerini ayarlayın SQL Serverve kimlik doğrulama modunu. Daha sonra geçiş ServerConnectionnesne değişkeni bir parametre olarak Servernesne yapıcısı. Aynı anda farklı sunucu nesneleri arasındaki bağlantıları paylaşmak için önerilmez. Kullanım Copyyöntem-e doğru almak a kopya-in varolan bağlantı ayarlarını.

Sunucu nesnesi özelliklerini açıkça ayarlama

Alternatif olarak, ilan edebilir Servernesne değişkeni ve varsayılan Oluşturucu çağrısı. ServerNesne varsayılan örneğine bağlanmaya SQL Servertüm varsayılan bağlantı ayarlarıyla.

Sunucu nesnesi Oluşturucu SQL Server örneğinin adı sağlama

İlan Servernesne değişkeni ve geçmek SQL Serverörnek adı Oluşturucu dize parametresi olarak. ServerNesne örneği ile bağlantı kurar SQL Servervarsayılan bağlantı ayarlarıyla.

Bağlantı havuzu oluşturma

Bu genellikle arama için gerekli değildir Connectyöntemi ServerConnectionnesnesini. smo otomatik olarak gerektiğinde bağlantı kurmak ve işlemi bittikten sonra bağlantıyı bağlantı havuzu bırakın. Ne zaman Connectyöntemi olarak adlandırılan, bağlantı havuza serbest. Açık bir çağrı Disconnectyöntemi, bağlantı havuzuna serbest bırakmak için gereklidir. Ayrıca, sen-ebilmek rica havuzlu bağlantı ayarlayarak NonPooledConnectionözelliği ServerConnectionnesnesini.

Çok iş parçacıklı uygulamalar

Çok iş parçacıklı uygulamalar, ayrı bir ServerConnectionher iş parçacığı nesnesi kullanılmalıdır.

rmo için SQL Server örneği bağlanma

Çoğaltma Yönetimi Nesneleri'ni (rmo), çoğaltma sunucuya bağlanmak için smo den biraz farklı bir yöntem kullanır.

rmo programlama nesneleri gerektiren örneği bağlantısını SQL Serverkullanılarak yapılan ServerConnectionnesne tarafından uygulanan Microsoft.SqlServer.Management.Commonad. Bu sunucuya rmo programlama nesnenin bağımsız olarak yapılır. rmo nesnesi örneğini oluşturma sırasında veya atamaya geçirilir sonra ConnectionContextözelliği nesnenin. Bu şekilde oluşturulan ve ayrı ayrı yönetilen bir rmo programlama nesnesi ve bağlantı nesne örnekleri ve tek bağlantı nesnesi ile birden fazla rmo programlama nesneleri yeniden kullanılabilir. Bir çoğaltma sunucuya bağlantılar için aşağıdaki kurallar geçerlidir:

  • Bağlantı için tüm özellikleri tanımlanmamış belirtilen için ServerConnectionnesne.

  • Her bağlantı için bir örneği SQL Serverkendi olmalıdır ServerConnectionnesnesini.

  • Bağlantıyı yapmak ve başarıyla sunucuya oturum açmak için tüm kimlik bilgileri içinde verilir ServerConnectionnesnesini.

  • Varsayılan olarak, Microsoft Windows kimlik doğrulaması kullanarak bağlantı yapılır. Kullanmak için SQL Serverkimlik LoginSecureFalse olarak ayarlanmış olması gerekir ve Loginve Passwordgeçerli bir ayarlanmalıdır SQL Serveroturum açma ve parola. Güvenlik kimlik bilgileri gerekir her zaman depolanabilir ve güvenli bir şekilde ele ve çalışma zamanında mümkün olduğunca sağlanan.

  • ConnectYöntemi olmalıdır denilen bağlantı herhangi rmo programlama nesnesi geçirmeden önce.

Örnekler

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 SQL Server Boks Online'da "How to: Create a Visual Basic SMO Project in Visual Studio .NET" (Nasıl Yapılır: Visual Studio .NET içinde Visual Basic SMO Projesi Oluşturma) veya "How to: Create a Visual C# SMO Project in Visual Studio .NET" (Nasıl Yapılır: Visual Studio .NET içinde Visual C# SMO Projesi Oluşturma) konularına bakın.

Yerel SQL Server örneği için Visual Basic'te Windows kimlik doğrulaması kullanarak bağlanma

Yerel örneğine bağlama SQL Serverkadar kod gerektirmez. Bunun yerine o kimlik doğrulama yöntemi ve sunucu için varsayılan ayarları kullanır. Veri gerektiren ilk işlem bağlantı oluşturulmasına neden olur.

Bu örnek Visual Basic.net kodu yerel örneğine bağlanan SQL ServerWindows kimlik doğrulaması kullanarak.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'The connection is established when a property is requested.
Console.WriteLine(srv.Information.Version)
'The connection is automatically disconnected when the Server variable goes out of scope.

Yerel SQL Server örneği için Windows kimlik doğrulaması Visual C# içinde kullanarak bağlanma

Yerel örneğine bağlama SQL Serverkadar kod gerektirmez. Bunun yerine o kimlik doğrulama yöntemi ve sunucu için varsayılan ayarları kullanır. Veri gerektiren ilk işlem bağlantı oluşturulmasına neden olur.

Bu, Visual C# örneğidir.net kodu yerel örneğine bağlanan SQL ServerWindows kimlik doğrulaması kullanarak.

{ 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//The connection is established when a property is requested. 
Console.WriteLine(srv.Information.Version); 
} 
//The connection is automatically disconnected when the Server variable goes out of scope.

{ 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//The connection is established when a property is requested. 
Console.WriteLine(srv.Information.Version); 
} 
//The connection is automatically disconnected when the Server variable goes out of scope.

Uzak bir SQL Server örneği için Visual Basic'te Windows kimlik doğrulaması kullanarak bağlanma

Örneğine bağlanırken SQL ServerWindows kimlik doğrulaması kullanarak, kimlik doğrulama türünü belirtmek zorunda değilsiniz. Windows kimlik doğrulaması, varsayılan değerdir.

Bu örnek Visual Basic.net kodu uzak örneğine bağlanan SQL ServerWindows kimlik doğrulaması kullanarak. Dize değişkeni strServerUzaktan örneği içerir.

'Connect to a remote instance of SQL Server.
Dim srv As Server
'The strServer string variable contains the name of a remote instance of SQL Server.
srv = New Server(strServer)
'The actual connection is made when a property is retrieved. 
Console.WriteLine(srv.Information.Version)
'The connection is automatically disconnected when the Server variable goes out of scope.

Uzak bir SQL Server örneği için Windows kimlik doğrulaması Visual C# içinde kullanarak bağlanma

Örneğine bağlanırken SQL ServerWindows kimlik doğrulaması kullanarak, kimlik doğrulama türünü belirtmek zorunda değilsiniz. Windows kimlik doğrulaması, varsayılan değerdir.

Bu, Visual C# örneğidir.net kodu uzak örneğine bağlanan SQL ServerWindows kimlik doğrulaması kullanarak. Dize değişkeni strServerUzaktan örneği içerir.

{ 
//Connect to a remote instance of SQL Server. 
Server srv; 
//The strServer string variable contains the name of a remote instance of SQL Server. 
srv = new Server(strServer); 
//The actual connection is made when a property is retrieved. 
Console.WriteLine(srv.Information.Version); 
} 
//The connection is automatically disconnected when the Server variable goes out of scope.

{ 
//Connect to a remote instance of SQL Server. 
Server srv; 
//The strServer string variable contains the name of a remote instance of SQL Server. 
srv = new Server(strServer); 
//The actual connection is made when a property is retrieved. 
Console.WriteLine(srv.Information.Version); 
} 
//The connection is automatically disconnected when the Server variable goes out of scope.

SQL Server örneği için Visual Basic'te SQL Server kimlik doğrulaması kullanarak bağlanma

Örneğine bağlanırken SQL Serverkullanarak SQL Serverkimlik doğrulama, kimlik doğrulama türünü belirtmeniz gerekir. İlan alternatif bir yöntem bu örnek gösterir bir ServerConnectionbağlantı bilgilerini yeniden kullanılmasını sağlayan nesne değişkeni.

Örnek Visual Basic.net kodu için uzaktan bağlanmak gösterilmiştir ve vPasswordoturum açma ve parola içeren.

' compile with: 
' /r:Microsoft.SqlServer.Smo.dll
' /r:Microsoft.SqlServer.ConnectionInfo.dll
' /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll 

Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common

Public Class A
   Public Shared Sub Main()
      Dim sqlServerLogin As [String] = "user_id"
      Dim password As [String] = "pwd"
      Dim instanceName As [String] = "instance_name"
      Dim remoteSvrName As [String] = "remote_server_name"


      ' Connecting to an instance of SQL Server using SQL Server Authentication
      Dim srv1 As New Server()   ' connects to default instance
      srv1.ConnectionContext.LoginSecure = False   ' set to true for Windows Authentication
      srv1.ConnectionContext.Login = sqlServerLogin
      srv1.ConnectionContext.Password = password
      Console.WriteLine(srv1.Information.Version)   ' connection is established

      ' Connecting to a named instance of SQL Server with SQL Server Authentication using ServerConnection
      Dim srvConn As New ServerConnection()
      srvConn.ServerInstance = ".\" & instanceName   ' connects to named instance
      srvConn.LoginSecure = False   ' set to true for Windows Authentication
      srvConn.Login = sqlServerLogin
      srvConn.Password = password
      Dim srv2 As New Server(srvConn)
      Console.WriteLine(srv2.Information.Version)   ' connection is established

      ' For remote connection, remote server name / ServerInstance needs to be specified
      Dim srvConn2 As New ServerConnection(remoteSvrName)
      srvConn2.LoginSecure = False
      srvConn2.Login = sqlServerLogin
      srvConn2.Password = password
      Dim srv3 As New Server(srvConn2)
      Console.WriteLine(srv3.Information.Version)   ' connection is established
   End Sub
End Class

' compile with: 
' /r:Microsoft.SqlServer.Smo.dll
' /r:Microsoft.SqlServer.ConnectionInfo.dll
' /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll 

Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common

Public Class A
   Public Shared Sub Main()
      Dim sqlServerLogin As [String] = "user_id"
      Dim password As [String] = "pwd"
      Dim instanceName As [String] = "instance_name"
      Dim remoteSvrName As [String] = "remote_server_name"


      ' Connecting to an instance of SQL Server using SQL Server Authentication
      Dim srv1 As New Server()   ' connects to default instance
      srv1.ConnectionContext.LoginSecure = False   ' set to true for Windows Authentication
      srv1.ConnectionContext.Login = sqlServerLogin
      srv1.ConnectionContext.Password = password
      Console.WriteLine(srv1.Information.Version)   ' connection is established

      ' Connecting to a named instance of SQL Server with SQL Server Authentication using ServerConnection
      Dim srvConn As New ServerConnection()
      srvConn.ServerInstance = ".\" & instanceName   ' connects to named instance
      srvConn.LoginSecure = False   ' set to true for Windows Authentication
      srvConn.Login = sqlServerLogin
      srvConn.Password = password
      Dim srv2 As New Server(srvConn)
      Console.WriteLine(srv2.Information.Version)   ' connection is established

      ' For remote connection, remote server name / ServerInstance needs to be specified
      Dim srvConn2 As New ServerConnection(remoteSvrName)
      srvConn2.LoginSecure = False
      srvConn2.Login = sqlServerLogin
      srvConn2.Password = password
      Dim srv3 As New Server(srvConn2)
      Console.WriteLine(srv3.Information.Version)   ' connection is established
   End Sub
End Class

SQL Server örneği için SQL Server kimlik doğrulaması Visual C# içinde kullanarak bağlanma

Örneğine bağlanırken SQL Serverkullanarak SQL Serverkimlik doğrulama, kimlik doğrulama türünü belirtmeniz gerekir. İlan alternatif bir yöntem bu örnek gösterir bir ServerConnectionbağlantı bilgilerini yeniden kullanılmasını sağlayan nesne değişkeni.

Visual C# örnektir.net kodu için uzaktan bağlanmak gösterilmiştir ve vPasswordoturum açma ve parola içeren.

// compile with: 
// /r:Microsoft.SqlServer.Smo.dll
// /r:Microsoft.SqlServer.ConnectionInfo.dll
// /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll 

using System;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

public class A {
   public static void Main() { 
      String sqlServerLogin = "user_id";
      String password = "pwd";
      String instanceName = "instance_name";
      String remoteSvrName = "remote_server_name";

      // Connecting to an instance of SQL Server using SQL Server Authentication
      Server srv1 = new Server();   // connects to default instance
      srv1.ConnectionContext.LoginSecure = false;   // set to true for Windows Authentication
      srv1.ConnectionContext.Login = sqlServerLogin;
      srv1.ConnectionContext.Password = password;
      Console.WriteLine(srv1.Information.Version);   // connection is established

      // Connecting to a named instance of SQL Server with SQL Server Authentication using ServerConnection
      ServerConnection srvConn = new ServerConnection();
      srvConn.ServerInstance = @".\" + instanceName;   // connects to named instance
      srvConn.LoginSecure = false;   // set to true for Windows Authentication
      srvConn.Login = sqlServerLogin;
      srvConn.Password = password;
      Server srv2 = new Server(srvConn);
      Console.WriteLine(srv2.Information.Version);   // connection is established


      // For remote connection, remote server name / ServerInstance needs to be specified
      ServerConnection srvConn2 = new ServerConnection(remoteSvrName);
      srvConn2.LoginSecure = false;
      srvConn2.Login = sqlServerLogin;
      srvConn2.Password = password;
      Server srv3 = new Server(srvConn2);
      Console.WriteLine(srv3.Information.Version);   // connection is established
   }
}

// compile with: 
// /r:Microsoft.SqlServer.Smo.dll
// /r:Microsoft.SqlServer.ConnectionInfo.dll
// /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll 

using System;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

public class A {
   public static void Main() { 
      String sqlServerLogin = "user_id";
      String password = "pwd";
      String instanceName = "instance_name";
      String remoteSvrName = "remote_server_name";

      // Connecting to an instance of SQL Server using SQL Server Authentication
      Server srv1 = new Server();   // connects to default instance
      srv1.ConnectionContext.LoginSecure = false;   // set to true for Windows Authentication
      srv1.ConnectionContext.Login = sqlServerLogin;
      srv1.ConnectionContext.Password = password;
      Console.WriteLine(srv1.Information.Version);   // connection is established

      // Connecting to a named instance of SQL Server with SQL Server Authentication using ServerConnection
      ServerConnection srvConn = new ServerConnection();
      srvConn.ServerInstance = @".\" + instanceName;   // connects to named instance
      srvConn.LoginSecure = false;   // set to true for Windows Authentication
      srvConn.Login = sqlServerLogin;
      srvConn.Password = password;
      Server srv2 = new Server(srvConn);
      Console.WriteLine(srv2.Information.Version);   // connection is established


      // For remote connection, remote server name / ServerInstance needs to be specified
      ServerConnection srvConn2 = new ServerConnection(remoteSvrName);
      srvConn2.LoginSecure = false;
      srvConn2.Login = sqlServerLogin;
      srvConn2.Password = password;
      Server srv3 = new Server(srvConn2);
      Console.WriteLine(srv3.Information.Version);   // connection is established
   }
}

Ayrıca bkz.

Başvuru

Server

ServerConnection