本文整理汇总了C#中System.Data.SqlClient.SqlDependency类的典型用法代码示例。如果您正苦于以下问题:C# SqlDependency类的具体用法?C# SqlDependency怎么用?C# SqlDependency使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SqlDependency类属于System.Data.SqlClient命名空间,在下文中一共展示了SqlDependency类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SqlChangeMonitor
public SqlChangeMonitor (SqlDependency dependency)
{
if (dependency == null)
throw new ArgumentNullException ("dependency");
throw new NotImplementedException ();
}
示例2: GetData
public IEnumerable<JobInfo> GetData()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["KPIConnectionString"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT [JobID],[Name],[LastExecutionDate],[Status] FROM [dbo].[JobInfo]", connection))
{
// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
using (var reader = command.ExecuteReader())
return reader.Cast<IDataRecord>()
.Select(x => new JobInfo()
{
JobID = x.GetInt32(0),
Name = x.GetString(1),
LastExecutionDate = x.GetDateTime(2),
Status = x.GetString(3)
}).ToList();
}
}
}
示例3: UpdateGrid
private static void UpdateGrid()
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
//依赖是基于某一张表的,而且查询语句只能是简单查询语句,不能带top或*,同时必须指定所有者,即类似[dbo].[]
using (SqlCommand command = new SqlCommand("select ID,UserID,[Message] From [dbo].[Messages] where id>0", connection))
{
command.CommandType = CommandType.Text;
connection.Open();
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
using (SqlDataReader sdr = command.ExecuteReader())
{
Console.WriteLine();
while (sdr.Read())
{
Console.WriteLine(
"Id:{0}\tUserId:{1}\tMessage:{2}",
sdr["ID"].ToString(), sdr["UserId"].ToString(),
sdr["Message"].ToString()
);
}
}
}
}
}
示例4: RegisterForChanges
public string[] RegisterForChanges()
{
string[] result = null;
using (SqlConnection conn = new SqlConnection(mSubscriberConnectionString))
{
if (conn.State != ConnectionState.Open) conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT [BilgilendirmeTipi],[Baslik],[Icerik] FROM [dbo].[Bilgilendirmeler]", conn))
{
SqlDependency dependency = new SqlDependency(cmd);
dependency.OnChange += new OnChangeEventHandler(OnNotificationChange);
using (SqlDataAdapter adap = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
adap.Fill(dt);
if (dt.Rows.Count > 0)
{
result = new string[3];
result[0] = (string)dt.Rows[0][0];
result[1] = (string)dt.Rows[0][1];
result[2] = (string)dt.Rows[0][2];
}
}
}
}
}
return result;
}
示例5: Enlist
public ChangeMonitor Enlist()
{
using (var connection = new SqlConnection(connectionString))
{
using (var exeCommand = new System.Data.SqlClient.SqlCommand(command, connection))
{
//is the command a sproc
if (isStoredProcedure)
{
exeCommand.CommandType = CommandType.StoredProcedure;
}
if (commandTimeout.HasValue)
exeCommand.CommandTimeout = this.commandTimeout.Value;
var dependency = new SqlDependency(exeCommand);
var monitor = new SqlChangeMonitor(dependency);
connection.Open();
exeCommand.ExecuteNonQuery();
return monitor;
}
}
}
示例6: DataBase
public DataBase()
{
ConnectToDB();
sqlDependency = new SqlDependency();
//sqlDependency.AddCommandDependency(new SqlCommand("INSERT", conn));
sqlDependency.OnChange += new OnChangeEventHandler(onChange);
}
示例7: GetPersons
public IEnumerable<Person> GetPersons()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
connection.Open();
using (var command = new SqlCommand(@"SELECT [ID],[FirstName],[LastName] FROM [dbo].[Person]", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += (x, y) => _hubContext.Clients.All.displayStatus(GetPersons());
using (var reader = command.ExecuteReader())
{
return reader.Cast<IDataRecord>()
.Select(x => new Person
{
ID = x.GetInt64(0),
FirstName = x.GetString(1),
LastName = x.GetString(2),
}).ToList();
}
}
}
}
示例8: GetAllMessages
public IEnumerable<Messages> GetAllMessages()
{
var messages = new List<Messages>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(@"SELECT [MessageID], [Message], [EmptyMessage], [Date] FROM [dbo].[Messages]", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
messages.Add(item: new Messages { MessageID = (int)reader["MessageID"], Message = (string)reader["Message"], EmptyMessage = reader["EmptyMessage"] != DBNull.Value ? (string) reader["EmptyMessage"] : "", MessageDate = Convert.ToDateTime(reader["Date"]) });
}
}
}
return messages;
}
示例9: GetAllMessagesLog
public IEnumerable<MessageLog> GetAllMessagesLog()
{
string conStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection connection = new SqlConnection(conStr);
string query = "SELECT Message,EventID,LL.Name as LogLevelID,OC.Name as OperationCodeID,ML.ServerName,ML.ComponentName,ML.SubComponentName FROM [dbo].[MessageLog] ML inner join [dbo].[LogLevel] LL on ML.LogLevelID = LL.ID inner join [dbo].[OperationCode] OC on ML.OperationCodeID = OC.ID";
SqlDependency.Start(conStr);
SqlCommand command = new SqlCommand(query, connection);
SqlDependency dependency = new SqlDependency(command);
//If Something will change in database and it will call dependency_OnChange method.
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
connection.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
List<MessageLog> messageList = new List<MessageLog>();
for (int i = 0; i < dt.Rows.Count; i++)
{
MessageLog ml = new MessageLog();
ml.Name = dt.Rows[i]["Message"].ToString();
ml.EventID = Convert.ToInt32(dt.Rows[i]["EventID"].ToString());
ml.LogLevelName = dt.Rows[i]["LogLevelID"].ToString();
ml.OperationCodeName = dt.Rows[i]["OperationCodeID"].ToString();
ml.ServerName = dt.Rows[i]["ServerName"].ToString();
ml.ComponentName = dt.Rows[i]["ComponentName"].ToString();
ml.SubComponentName = dt.Rows[i]["SubComponentName"].ToString();
messageList.Add(ml);
}
return messageList;
}
示例10: RegisterDiscussMessages
public List<DiscussMessageModel> RegisterDiscussMessages()
{
var messageChat = new List<DiscussMessageModel>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(@"SELECT *
FROM [dbo].[Comments]", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
messageChat.Add(item: new DiscussMessageModel
{
Id = reader["Id"].ToString(),
Username = reader["Username"].ToString(),
Message = reader["Message"].ToString(),
FilePath = reader["FilePath"].ToString(),
Avatar = "dist/img/avatar.png",
Status = reader["Status"].ToString(),
CreatedDate = DateTime.Parse(reader["CreatedDate"].ToString())
});
}
}
}
return messageChat;
}
示例11: GetData
public IEnumerable<Message> GetData()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("SELECT [ID], [Date], [Message] FROM [dbo].[Messages]", connection))
{
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += DependencyOnChange;
if (connection.State == ConnectionState.Closed)
connection.Open();
using (var reader = command.ExecuteReader())
return reader.Cast<IDataRecord>()
.Select(x => new Message()
{
ID = x.GetInt32(0),
Date = x.GetDateTime(1),
Text = x.GetString(2)
}).ToList();
}
}
}
示例12: RegisterDiscussMessages
public void RegisterDiscussMessages()
{
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(@"SELECT [Id]
,[Username]
,[Message]
,[FilePath]
,[Status]
,[CreatedDate]
FROM [dbo].[Discuss] ORDER BY [dbo].[Discuss].CreatedDate desc", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
}
}
}
示例13: 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;
}
}
}
示例14: SqlDependencyCacheWatcher
public SqlDependencyCacheWatcher(string tableName)
{
this.connectionString = connectionString;
this.sqlQueue = sqlQueue;
this.listenerQuery = listenerQuery;
this.dependency = null;
}
示例15: 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;
}