Core 2.1 SignalR and SQLDependency - signalr

Is there any Core 2.1 sample available for using SignalR with SQLDependency.
Did enable broker, etc. but never get any dependency onChange event firing. Just the event subscribe is triggered.
When the MS-SQL database table Cities changes on the back-end, I want to see the change reflected right-away on the client web page without having to refresh/reload the page.
//start the dependency when app start in ConfigureServices
SqlDependency.Start(Configuration.GetConnectionString("DefaultConnection"));
using Microsoft.AspNetCore.SignalR;
using SignalR_Test4.Data;
using SignalR_Test4.Hubs;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace SignalR_Test4.Models
{
public class CityRepository
{
private readonly ApplicationDbContext _context;
private readonly IHubContext<CityHub> _hubcontext;
public CityRepository(ApplicationDbContext context, IHubContext<CityHub> hubcontext)
{
_context = context;
_hubcontext = hubcontext;
}
public IEnumerable<City> GetCities()
{
List<City> listOf = new List<City>();
//listOf = _context.Cities;
using (var conn = new SqlConnection(GlobalVar.connectionString))
{
conn.Open();
using (var cmd = new SqlCommand(#"SELECT * FROM Cities", conn))
{
cmd.Notification = null;
SqlDependency dependency = new SqlDependency(cmd);
dependency.OnChange += Dependency_OnChange;
if (conn.State == System.Data.ConnectionState.Closed)
conn.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
listOf.Add(new City { Id = (string)reader["Id"], Name_en = (string)reader["name_en"], CountryId = (string)reader["CountryId"], Code = (string)reader["Code"] });
}
}
}
return listOf;
}
private void Dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
_hubcontext.Clients.All.SendAsync("GetCities");
}
}
}
}

The issue was within the line:
var cmd = new SqlCommand(#"SELECT Id, Name_en, CountryId, Code from [dbo].Cities", conn)
It is required to use the field name (Not the *) and also the 2 part table name convention => [dbo].Cities

Related

Sqllite SQLiteDataReader returns enpty reader while SQLiteDataAdapter returns the right result

I have trouble with Sqllite SQLiteDataReader. Using the same connection string and the same sql statement SQLiteDataReader returns empty reader while SQLiteDataAdapter returns suspected record.
I this case I try to get the record with the highest value in the Id field.
The database contains several records with unique values in the Id field, but the reader return is empty when using SQLiteDataReader. When I use the same connection string and sql statement with SQLiteDataAdapter suspected results appears. I supply a part of the static class I use for communication with the database. The method SenasteBokning using SQLiteDataReader isn’t working. The method SenasteBokning2 using SQLiteDataAdapter works perfect.
What’s wrong with the method SenasteBokning?
I use:
Windows 10
Visual Studio 2017
.net framework 4.5.2 (Was default at creation of Windows Forms application)
Nuget package System.Data.SQLite.Core 1.0.108
static class Databas
{
private static string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
private static string dbPath = #"\BokningarConvention.db";
private static string connectionString = "Data Source= " + appPath + dbPath;
private static SQLiteConnection con;
private static SQLiteCommand cmd;
private static SQLiteDataReader reader;
private static SQLiteDataAdapter adapter;
private static SQLiteCommandBuilder commandBuilder;
private static DataTable table;
private static string senaste = "SELECT Nummer, NrSammaDag, Datum FROM Bekraftelser WHERE Id = (SELECT MAX (Id) FROM Bekraftelser)";
// This don't work
public static Bokning SenasteBokning()
{
Bokning bokning = new Bokning();
using (SQLiteConnection con2 = new SQLiteConnection(connectionString))
{
con2.Open();
SQLiteCommand cmd2 = new SQLiteCommand(senaste, con2);
SQLiteDataReader reader2 = cmd2.ExecuteReader();
// Here the reader is empty
while (reader2.Read())
{
// Error at first read
// should handle results the same way as in SenasteBokning2
// removed during testing
}
}
return bokning;
}
//This works perfekt
public static Bokning SenasteBokning2()
{
Bokning bokning = new Bokning();
using (SQLiteConnection db = new SQLiteConnection(connectionString))
{
adapter = new SQLiteDataAdapter(senaste, connectionString);
commandBuilder = new SQLiteCommandBuilder(adapter);
table = new DataTable();
db.Open();
adapter.Fill(table);
foreach (DataRow row in table.Rows)
{
int nummer;
int samma;
DateTime datum;
nummer = (int)((long)row["Nummer"]);
datum = Verktyg.FromDateInteger((int)((long)row["Datum"]));
if (!row.IsNull("NrSammaDag"))
{
samma = (int)((long)row["NrSammaDag"]);
bokning = new Bokning(nummer, samma, datum);
}
else
{
bokning = new Bokning(nummer, datum);
}
}
}
return bokning;
}
}

SQLDependency Onchange event always firing without data change

I'm using it on a console project.
.NET Framework: 4.5
In my test code, SQLDependency onChange always firing although there is no data changes in Database.
class Program
{
private static string _connStr;
static void Main(string[] args)
{
_connStr = "data source=xxx.xxx.xx.xx;User Id=xxx;Password=xxx; Initial Catalog=xxx";
SqlDependency.Start(_connStr);
UpdateGrid();
Console.Read();
}
private static void UpdateGrid()
{
using (SqlConnection connection = new SqlConnection(_connStr))
{
using (SqlCommand command = new SqlCommand("select msgdtl,msgid From NotifyMsg", connection))
{
command.CommandType = CommandType.Text;
connection.Open();
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
SqlDataReader sdr = command.ExecuteReader();
Console.WriteLine();
while (sdr.Read())
{
Console.WriteLine("msgdtl:{0}\t (msgid:{1})", sdr["msgdtl"].ToString(), sdr["msgid"].ToString());
}
sdr.Close();
}
}
}
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
UpdateGrid();
}
when I start running, onChange event fires and never stop. But there is no change in my databse.
Try to check the SqlNotificationEventArgs object in the dependency_OnChnage method. It looks like you have an error there. I had the same SqlDependency behavior one time and the problem was solved by changing select msgdtl,msgid From NotifyMsg to select msgdtl,msgid From dbo.NotifyMsg (The dbo statement has been added).
But I should warn you: be careful using SqlDependency class - it has the problems with memory leaks. Hovewer, you can use an open source realization of the SqlDependency class - SqlDependencyEx. It uses a database trigger and native Service Broker notification to receive events about the table changes. This is an usage example:
int changesReceived = 0;
using (SqlDependencyEx sqlDependency = new SqlDependencyEx(
TEST_CONNECTION_STRING, TEST_DATABASE_NAME, TEST_TABLE_NAME))
{
sqlDependency.TableChanged += (o, e) => changesReceived++;
sqlDependency.Start();
// Make table changes.
MakeTableInsertDeleteChanges(changesCount);
// Wait a little bit to receive all changes.
Thread.Sleep(1000);
}
Assert.AreEqual(changesCount, changesReceived);
With SqlDependecyEx you are able to monitor INSERT, DELETE, UPDATE separately and receive actual changed data (xml) in the event args object. Hope this help.
there is a custom implementation of SqlDependency that report you the changed table records:
var _con= "data source=.; initial catalog=MyDB; integrated security=True";
static void Main()
{
using (var dep = new SqlTableDependency<Customer>(_con, "Customer"))
{
dep.OnChanged += Changed;
dep.Start();
Console.WriteLine("Press a key to exit");
Console.ReadKey();
dep.Stop();
}
}
static void Changed(object sender, RecordChangedEventArgs<Customer> e)
{
if (e.ChangeType != ChangeType.None)
{
for (var index = 0; index < e.ChangedEntities.Count; index++)
{
var changedEntity = e.ChangedEntities[index];
Console.WriteLine("DML operation: " + e.ChangeType);
Console.WriteLine("ID: " + changedEntity.Id);
Console.WriteLine("Name: " + changedEntity.Name);
Console.WriteLine("Surame: " + changedEntity.Surname);
}
}
}
Here is the link: [https://tabledependency.codeplex.com]

How to cache global information for all users?

I have my first bigger asp.net website and there are userlists of all user online - of course this list is the same for every user, but as a normal online list I update this with PageMethod / WebMethod every 10 seconds.
So if 100 users online that means 10x6x100 = 6000 database querys each minute.
How can I avoid that?
Can I save this information for all user in something like a session / querystring / cookie but global for all users to avoid querys?
The Simplest way is to create an Application Variable or DataTable, which will hold your Required Information.
After each 10 minutes, when you update the records, Just update the Application Datatable you created above. This DataTable is common for all the users and that will decrease your load drastically.
Let me know if you need code.
You may us static variable for this. If you are having more than 1 app-pool to serverpages
then use asp.net caching since static variable are not thread safe.
Here is my code that i use for something similar it has 2 class.
class1
using System;
public class onlineuser
{
public string sessionid = "";
public string username = "";
public string currentpage = "";
public DateTime time = DateTime.Now;
public onlineuser()
{
//
// TODO: Add constructor logic here
//
}
}
class2
using System;
using System.Collections;
using System.Data;
public class user
{
public static ArrayList online;
public static void adduser(string sessionid,string username,string currentpage)
{
removeunused();
remove(sessionid);
onlineuser ou = new onlineuser();
ou.sessionid = sessionid;
ou.username = username;
ou.currentpage = currentpage;
ou.time = DateTime.Now;
if (online==null)
{
online = new ArrayList();
}
online.Add(ou);
online.TrimToSize();
}
public static void remove(string sessionid)
{
if (online==null)
{
return;
}
onlineuser ou = new onlineuser();
for (int i = 0; i < online.Count; i++)
{
ou = (onlineuser)online[i];
if (ou.sessionid == sessionid)
{
online.RemoveAt(i);
online.TrimToSize();
return;
}
}
}
public static void removeunused()
{
if (online == null)
{
return;
}
onlineuser ou = new onlineuser();
for (int i = 0; i < online.Count; i++)
{
ou = (onlineuser)online[i];
if (ou.time < DateTime.Now.AddMinutes(-2))
{
online.RemoveAt(i);
online.TrimToSize();
return;
}
}
}
public static DataTable totable()
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("SessionId", typeof(string));
DataColumn dc1 = new DataColumn("UserName", typeof(string));
DataColumn dc2 = new DataColumn("currentpage", typeof(string));
DataColumn dc3 = new DataColumn("Time", typeof(DateTime));
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
if (online!=null)
{
onlineuser ou = new onlineuser();
for (int i = 0; i < online.Count; i++)
{
ou = (onlineuser)online[i];
dt.Rows.Add(new object[] {ou.sessionid,ou.username,ou.currentpage,ou.time});
}
}
return dt;
}
}
following code is placed in mymaster page which update userlist
try
{
string uname= "N/A";
if (Session["uname"]!=null)
{
uname = Session["uname"].ToString();
}
string page = Path.GetFileName(Request.PhysicalPath).Trim().ToLower();
if (Request.QueryString!=null)
{
page += "?"+Request.QueryString.ToString();
}
user.adduser(Session.SessionID, uname, page);
}
catch (Exception)
{
}

How to close executereader in asp.net?

I am using more then one repeater on same page. But when I use Execute reader for 2nd repeater then it gives exception that there is already execute reader running.. so close it. I put ExecuteReader(CommandBehavior.CloseConnection) but it give error that command behaviour doesn't exists... Any idea about this issue?
You need to explicitly close the DataReader if you specify that CommandBehavior, it will not do it for you.
http://msdn.microsoft.com/en-us/library/y6wy5a0f.aspx
Personally I would bind UI controls to strongly typed objects. So for example I would define a Product model:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
then a method to read products from the database:
public static IEnumerable<Product> GetProducts()
{
using (var conn = new SqlConnection("Some connection string"))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "SELECT prod_id, prod_name FROM products";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
yield return new Product
{
Id = reader.GetInt32(reader.GetOrdinal("prod_id")),
Name = reader.GetString(reader.GetOrdinal("prod_name")),
};
}
}
}
}
and in the web tier I would call this method to fetch my products and bind them to some UI controls:
protected void Page_Load(object sender, EventArgs e)
{
var products = Db.GetProducts().ToArray();
repeater1.DataSource = products;
repeater2.DataSource = products;
gridView.DataSource = products;
...
}
And when you get sick of writing those SQL queries you might take a look at an ORM, such as Entity Framework for example.

Quickest way to setup this asp.net page against MS Access DB . .

I have an access database with 3 tables.
People
Gifts
PeopleGifts
Using VS 2008, what is the quickest way to get a page up and running which allows me to run queries against these tables and do basic inserts.
I want to have comboboxs bound to fields in the table so a user can click on a person and click on a gift and they click "Add".
The quickest way? Iron Speed
try using an oleDBDataAdapter and a formview
public interface IOleDbDataGateway
{
void ExecuteNonQuery(string sql, params object[] args);
object ExecuteScalar(string sql, params object[] args);
DataTable FillDataTable(string sql, params object[] args);
}
public class OleDbDataGateway : IOleDbDataGateway
{
private readonly string connectionString;
public OleDbDataGateway(string connectionString)
{
this.connectionString = connectionString;
}
public void ExecuteNonQuery(string sql, params object[] args)
{
if (args != null)
{
sql = string.Format(sql, args);
}
var connection = new OleDbConnection(connectionString);
var command = new OleDbCommand(sql, connection);
connection.Open();
try
{
command.ExecuteNonQuery();
}
finally
{
connection.Close();
}
}
public object ExecuteScalar(string sql, params object[] args)
{
if (args != null)
{
sql = string.Format(sql, args);
}
var connection = new OleDbConnection(connectionString);
var command = new OleDbCommand(sql, connection);
connection.Open();
try
{
return command.ExecuteScalar();
}
finally
{
connection.Close();
}
}
public DataTable FillDataTable(string sql, params object[] args)
{
if (args != null)
{
sql = string.Format(sql, args);
}
var connection = new OleDbConnection(connectionString);
var adapter = new OleDbDataAdapter(sql, connection);
var table = new DataTable();
connection.Open();
try
{
adapter.Fill(table);
}
finally
{
connection.Close();
}
return table;
}
}

Resources