本文整理汇总了C#中System.Data.SqlClient.SqlDependency.AddCommandDependency方法的典型用法代码示例。如果您正苦于以下问题:C# SqlDependency.AddCommandDependency方法的具体用法?C# SqlDependency.AddCommandDependency怎么用?C# SqlDependency.AddCommandDependency使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlDependency
的用法示例。
在下文中一共展示了SqlDependency.AddCommandDependency方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsInMaintenanceMode
public static bool IsInMaintenanceMode()
{
bool inMaintenanceMode;
string connStr = "Data Source=KIM-MSI\\KIMSSQLSERVER;Initial Catalog=MVWDataBase;User ID=sa;Password=mis123;MultipleActiveResultSets=True";
if (MemoryCache.Default["MaintenanceMode"] == null)
{
Console.WriteLine("Hitting the database...");
CacheItemPolicy policy = new CacheItemPolicy();
SqlDependency.Start(connStr);
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand command = new SqlCommand("Select MaintenanceMode From dbo.MaintenanceMode", conn))
{
command.Notification = null;
SqlDependency dep = new SqlDependency();
dep.AddCommandDependency(command);
conn.Open();
inMaintenanceMode = (bool)command.ExecuteScalar();
SqlChangeMonitor monitor = new SqlChangeMonitor(dep);
policy.ChangeMonitors.Add(monitor);
dep.OnChange += Dep_OnChange;
}
}
MemoryCache.Default.Add("MaintenanceMode", inMaintenanceMode, policy);
}
else
{
inMaintenanceMode = (bool)MemoryCache.Default.Get("MaintenanceMode");
}
return inMaintenanceMode;
}
示例2: SomeMethod
public DataTable SomeMethod(Action<object, SqlNotificationEventArgs> handler)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
// notificationas bus iskvieciamas visuomet, kai pasikeis bent vienas duomuo, is sio SELECTO grazinamu duomenu set`o (bus atliktas UPDATE, DELETE, INSERT)
using (SqlCommand command = new SqlCommand("SELECT StudentasID, Vardas, Pavarde, IBAN, AK FROM dbo.tblIseiviaiStudentai", connection))
{
dependency = new SqlDependency();
command.Notification = null;
dependency.AddCommandDependency(command);
dependency.OnChange += new OnChangeEventHandler(handler);
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
// kad veiktu notificationas butini sie conection`o nustatymai, kitaip eventas bus kvieciamas nuolatos
var ON_PARAMETERS = @"SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER, ARITHABORT ON";
var OFF_PARAMETERS = @"SET NUMERIC_ROUNDABORT OFF";
var cmd_ON_PARAMETERS = new SqlCommand(ON_PARAMETERS, connection);
var cmd_OFF_PARAMETERS = new SqlCommand(OFF_PARAMETERS, connection);
cmd_ON_PARAMETERS.ExecuteNonQuery();
cmd_OFF_PARAMETERS.ExecuteNonQuery();
dataTable.Clear();
dataTable.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
dependency = null;
return dataTable;
}
}
}
示例3: SetMonitor
public static void SetMonitor()
{
string connStr = ConfigurationManager.ConnectionStrings["lab"].ConnectionString;
CacheItemPolicy policy = new CacheItemPolicy();
SqlDependency.Start(connStr);
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand command = new SqlCommand("Select MaintenanceMode From dbo.MaintenanceMode", conn))
{
command.Notification = null;
SqlDependency dep = new SqlDependency();
dep.AddCommandDependency(command);
conn.Open();
bool inMaintenanceMode = (bool)command.ExecuteScalar();
SqlChangeMonitor monitor = new SqlChangeMonitor(dep);
policy.ChangeMonitors.Add(monitor);
dep.OnChange += Dep_OnChange;
MemoryCache.Default.Add("MaintenanceMode", inMaintenanceMode, policy);
}
}
}
示例4: CreateSqlDep
void CreateSqlDep(SqlCommand sqlCmd) {
_sqlYukonDep = new SqlDependency();
// Note: sqlCmd is null in output cache case.
if (sqlCmd != null) {
Debug.Trace("SqlCacheDependency", "SqlCmd added to SqlDependency object");
_sqlYukonDep.AddCommandDependency(sqlCmd);
}
_sqlYukonDep.OnChange += new OnChangeEventHandler(OnSQL9SqlDependencyChanged);
Debug.Trace("SqlCacheDependency", "SQL9 dependency created: depId=" + _sqlYukonDep.Id);
}