SqlDependency.Start(connectionString, "SQLDependencyQueue") выдает исключение нулевой ссылкиC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 SqlDependency.Start(connectionString, "SQLDependencyQueue") выдает исключение нулевой ссылки

Сообщение Anonymous »

Ниже мой код

Код: Выделить всё

    using System;
using System.Data.SqlClient;

namespace asldf
{
class Program
{
private static string connectionString = "Server=.\\SQLEXPRESS;Database=Sample;User id=ad;Password=Pass1234;";
private static SqlDependency sqlDependency;

static void Main()
{
try
{
Console.WriteLine("Starting SqlDependency...");
SqlDependency.Start(connectionString, "SQLDependencyQueue"); // Start dependency first

if (string.IsNullOrEmpty(connectionString))
{
throw new ArgumentNullException("Connection string cannot be null or empty.");
}

Console.WriteLine("SqlDependency started.");

MonitorVolumeStore();

Console.WriteLine("Monitoring changes.  Press Enter to quit.");
Console.ReadLine();

Console.WriteLine("Stopping SqlDependency...");
SqlDependency.Stop(connectionString);
Console.WriteLine("SqlDependency stopped.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred in Main: {ex.Message}");
}
}

private static void MonitorVolumeStore()
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = "SELECT Date, InstrumentTocken, Volume, FullVolume, LTP, TotalBuyers, TotalSellers FROM VolumeStore WHERE InstrumentTocken = 11087106";

using (SqlCommand command = new SqlCommand(query, connection))
{
Console.WriteLine("Creating SqlDependency...");

Console.WriteLine("SqlDependency started.");

sqlDependency = new SqlDependency(command); // Initialize here
sqlDependency.OnChange += OnVolumeStoreChange;
Console.WriteLine("SqlDependency created.");

connection.Open();
Console.WriteLine("Connection opened.");
command.ExecuteReader();
Console.WriteLine("Query executed.");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred in MonitorVolumeStore: {ex.Message}");
}
}

private static void OnVolumeStoreChange(object sender, SqlNotificationEventArgs e)
{
Console.WriteLine("New row added to VolumeStore with InstrumentTocken = 11087106");

sqlDependency.OnChange -= OnVolumeStoreChange;
MonitorVolumeStore();

FetchLatestVolumeStoreRow();
}

private static void FetchLatestVolumeStoreRow()
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = @"
SELECT TOP 1 Date, InstrumentTocken, Volume, FullVolume, LTP, TotalBuyers, TotalSellers
FROM VolumeStore
WHERE InstrumentTocken = 11087106
ORDER BY Date DESC";

using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
DateTime date = reader.GetDateTime(0);
int instrumentTocken = reader.GetInt32(1);
int volume = reader.GetInt32(2);
int fullVolume = reader.GetInt32(3);
decimal ltp = reader.GetDecimal(4);
int totalBuyers = reader.GetInt32(5);
int totalSellers = reader.GetInt32(6);

Console.WriteLine($"Date: {date}, InstrumentTocken: {instrumentTocken}, Volume: {volume}, FullVolume: {fullVolume}, LTP: {ltp}, TotalBuyers: {totalBuyers}, TotalSellers: {totalSellers}");
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred in FetchLatestVolumeStoreRow: {ex.Message}");
}
}
}
}
Однако SqlDependency.Start(connectionString, "SQLDependencyQueue"); выдает исключение нулевой ссылки. Я также сделал:

Код: Выделить всё

ALTER SERVER ROLE sysadmin ADD MEMBER ad;
GRANT SUBSCRIBE QUERY NOTIFICATIONS TO ad;
ALTER DATABASE Sample SET ENABLE_BROKER;
Пожалуйста, помогите!

Подробнее здесь: https://stackoverflow.com/questions/792 ... ull-refere
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»