作为 .NET 框架代码访问安全性(请参阅代码访问安全性)的一部分,.NET 数据提供程序为开发人员提供了附加的安全性。您可以为 SQL Server .NET 数据提供程序和 OLE DB .NET 数据提供程序指定禁止使用空白密码的权限。此外,使用代码访问安全性,还可以将 OLE DB .NET 数据提供程序配置为只允许使用特定的 OLE DB 提供程序。(请注意,如果没有指定提供程序,则将启用所有提供程序。)可以使用 OLE DBSQL Client 权限集来设置这些权限。有关设置权限的信息,请参阅管理安全策略。

您可以将代码中的特定方法标识为需要特定的代码访问安全性特权。如果不允许您的代码使用该特权,那么在运行代码之前将引发异常。

例如,以下代码显示一个标识为需要空白密码的方法。如果该权限已禁用,则将引发异常。

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Security
Imports System.Security.Permissions

Public Class Sample

  <SqlClientPermissionAttribute(SecurityAction.Demand, AllowBlankPassword := True)> _
  Private Shared Sub OpenConnWithBlankPassword()
    Dim testConn As SqlConnection = New SqlConnection("Data Source=localhost;User Id=testblank;Password=;Initial Catalog=Northwind;")
    testConn.Open()
    Console.WriteLine("The calling method has been granted sufficient permission to access the database using a blank password.")
    testConn.Close()
  End Sub

  Public Shared Sub Main() 
    Try
      OpenConnWithBlankPassword()
    Catch e As SecurityException
      Console.WriteLine("The calling method has not been granted sufficient permission to access the database using a blank password.")
    End Try
  End Sub
End Class

using System;
using System.Data;
using System.Data.SqlClient;
using System.Security;
using System.Security.Permissions;

public class Sample
{

  [SqlClientPermissionAttribute(SecurityAction.Demand, AllowBlankPassword = true)]
  private static void OpenConnWithBlankPassword()
  {
    SqlConnection testConn = new SqlConnection("Data Source=localhost;User Id=testblank;Password=;Initial Catalog=Northwind;");
    testConn.Open();
    Console.WriteLine("The calling method has been granted sufficient permission to access the database using a blank password.");
    testConn.Close();
  }

  public static void Main() 
  {
    try
    {
      OpenConnWithBlankPassword();
    }
    catch (SecurityException)
    {
      Console.WriteLine("The calling method has not been granted sufficient permission to access the database using a blank password.");
    }
  }
}

以下代码示例确保 Microsoft Jet 4.0 OLE DB 提供程序在运行之前被启用。

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Security
Imports System.Security.Permissions

Public class Sample

  <OleDbPermissionAttribute(SecurityAction.Demand, Provider:="Microsoft.Jet.OLEDB.4.0")> _
  Private Shared Sub OpenConnWithJet40()
    Dim testConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                                        "Data Source=c:\Program Files\Microsoft Office\Office\Samples\northwind.mdb;")
    testConn.Open()
    Console.WriteLine("The calling method has been granted sufficient permission to access the Microsoft Jet 4.0 OLE DB Provider.")
    testConn.Close()
  End Sub

  Public Shared Sub Main()
    Try
      OpenConnWithJet40()
    Catch e As SecurityException
      Console.WriteLine("The calling method has not been granted sufficient permission to access the Microsoft Jet 4.0 OLE DB Provider.")
    End Try
  End Sub
End Class

using System;
using System.Data;
using System.Data.OleDb;
using System.Security;
using System.Security.Permissions;

public class Sample
{

  [OleDbPermissionAttribute(SecurityAction.Demand, Provider="Microsoft.Jet.OLEDB.4.0")]
  private static void OpenConnWithJet40()
  {
    OleDbConnection testConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
                                                   @"Data Source=c:\Program Files\Microsoft Office\Office\Samples\northwind.mdb;");
    testConn.Open();
    Console.WriteLine("The calling method has been granted sufficient permission to access the Microsoft Jet 4.0 OLE DB Provider.");
    testConn.Close();
  }

  public static void Main()
  {
    try
    {
      OpenConnWithJet40();
    }
    catch (SecurityException)
    {
      Console.WriteLine("The calling method has not been granted sufficient permission to access the Microsoft Jet 4.0 OLE DB Provider.");
    }
  }
}