VariableDispenser.LockForWrite(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将变量添加到要锁定用于读/写访问的变量列表。
public:
void LockForWrite(System::String ^ variable);
public void LockForWrite (string variable);
member this.LockForWrite : string -> unit
Public Sub LockForWrite (variable As String)
参数
- variable
- String
要添加到列表中以锁定用于读/写访问的变量的名称。
示例
下面的代码示例创建一个系统变量,并将两个 VariableDispenser 系统变量添加到锁定以供读取的列表,并将一个变量添加到锁定以供写入的列表。 然后,调用该 GetVariables 变量来锁定集合中的所有三个变量,并释放列表并可用于新变量。
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime;
namespace Microsoft.SqlServer.SSIS.Sample
{
class Program
{
static void Main(string[] args)
{
Package pkg = new Package();
Variables vars = null;
VariableDispenser variableDispenser = pkg.VariableDispenser;
variableDispenser.LockForRead("System::PackageName");
variableDispenser.LockForRead("System::OfflineMode");
variableDispenser.LockForWrite("User:MyVariable");
variableDispenser.GetVariables(ref vars);
// Verify that the variable is locked before unlocking.
Console.WriteLine("Variables are locked? {0}", vars.Locked);
foreach (Variable myVar in vars)
{
Console.WriteLine("Name {0}", myVar.Name);
Console.WriteLine("Description {0}", myVar.Description);
Console.WriteLine();
}
// Use Contains to determine whether indexing can be used.
Boolean pkgName = variableDispenser.Contains("PackageName");
String qName = variableDispenser.GetQualifiedName("PackageName");
Console.WriteLine("Contains is valid? {0}", pkgName);
Console.WriteLine("Fully qualified name is: {0}", qName);
vars.Unlock();
Console.WriteLine("Variables are locked? {0}", vars.Locked);
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.SqlServer.Dts.Runtime
Namespace Microsoft.SqlServer.SSIS.Sample
Class Program
Shared Sub Main(ByVal args() As String)
Dim pkg As Package = New Package()
Dim vars As Variables = Nothing
Dim variableDispenser As VariableDispenser = pkg.VariableDispenser
variableDispenser.LockForRead("System::PackageName")
variableDispenser.LockForRead("System::OfflineMode")
variableDispenser.LockForWrite("User:MyVariable")
variableDispenser.GetVariables( vars)
' Verify that the variable is locked before unlocking.
Console.WriteLine("Variables are locked? {0}", vars.Locked)
Dim myVar As Variable
For Each myVar In vars
Console.WriteLine("Name {0}", myVar.Name)
Console.WriteLine("Description {0}", myVar.Description)
Console.WriteLine()
Next
' Use Contains to determine whether indexing can be used.
Dim pkgName As Boolean = variableDispenser.Contains("PackageName")
Dim qName As String = variableDispenser.GetQualifiedName("PackageName")
Console.WriteLine("Contains is valid? {0}", pkgName)
Console.WriteLine("Fully qualified name is: {0}", qName)
vars.Unlock()
Console.WriteLine("Variables are locked? {0}", vars.Locked)
End Sub
End Class
End Namespace
示例输出:
Variables are locked? True
Name MyVariable
Description
Name OfflineMode
Description The offline mode currently set for the package
Name PackageName
Description The package name
Contains is valid? True
Fully qualified name is: System::PackageName
Variables are locked? False
注解
此方法在首次调用时创建一个列表,并将给定变量添加到列表中。 在后续调用中,变量将添加到现有列表中。 此方法不会锁定变量,而只会创建列表。 创建所需变量的列表后,调用 GetVariables 以锁定此列表中找到的变量。 调用之前LockForWriteGetVariables可以进行任意数量的调用LockForRead。
注意
一次只能有一个客户端获取任何给定变量的读/写锁。 因此,如果只需要读取权限,最好使用 LockForRead 它来帮助避免变量访问中的冲突。
如果锁成功,请通过调用 Unlock将其清除。