SQLCacheDependency not working in Load balanced environment with sql server 2008 - c#

We have implemented sqlcachedependency in our application. We have multiple caches depending on one database table and set the sql cache policy with a sql monitor as shown below.
policy = new CacheItemPolicy();
//connection string name
var connectionString = connectionstring;
//SQlDependency Cache
SqlDependency.Start(connectionString);
SqlChangeMonitor monitor = null;
using (var connection = new SqlConnection(connectionString))
{
using (var command = new SqlCommand("SELECT col1,col2 FROM dbo.table1", connection))
{
var dependency = new SqlDependency(command);
connection.Open();
command.ExecuteReader();
monitor = new SqlChangeMonitor(dependency);
}
}
policy.ChangeMonitors.Add(CreateMonitor());
This method is called while adding multiple cache. Everything is working fine when we check on the development machine. But once we upload the application in load balanced QA machine(QA1,QA2,QA3) it is not working.
All the grant permissions and sql broker enabling is done on the database.
When we check the sql profiler, we see that subscriptions are being registered in the database. But the notification from the sql broker and not working back in the application when an update is done on the table. We see the following error in profiler
**Cannot find the remote service 'SqlQueryNotificationService-
ab49a23a-9beb-4a6f-a8b0-299bcfddbeda' because it does not
exist.
and
This message has been dropped because the TO service could not be
found. Service name: "SqlQueryNotificationService-58237ce1-
aa4d-4999-9fd8-d0b78c1d932b". Message origin: "Local".**
Any help regarding this will be highly appreciable.

Finally, was able to find the answer. Basically it is the permission issue. Below link helped me grant permissions which fixed the issue
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/bd195da8-93b2-43c6-8f59-674f5fb9d618/cannot-find-the-queue-sqlquerynotificationserviceguid?forum=sqlservicebroker

Related

Trouble connecting to Sql Azure Db from C# using Active Directory password

I am setting up an azure database to which the CRM 360 Data Export Service (DES) needs to connect. I can do this manually using SSMS and logging on using my azure account.
And then setting up the DES service account as an external provider.
CREATE USER DynamicsDataExportService FROM EXTERNAL PROVIDER
All good, however I wish to automate the process, so I need to logon to the service with in C# and connect and run the code that way.
I am running core 2.2 with System.Data.SqlClient 4.7 using the following code:
var cs = "data source=tcp:dvzxfsdg-sql.database.windows.net,1433;initial catalog=dfsdfs-sqldb; Authentication=Active Directory Integrated";
using (var connection = new SqlConnection(cs))
{
connection.Open();
Annoyingly It gives the error
"System.ArgumentException: 'Keyword not supported: 'authentication'.'".
I am pretty sure the mistake I am making is fairly basic, but I cannot seem to the get to the bottom of it. So any pointers are gratefully received.
You might want to try using the SQLConnectionStringBuilder to properly format your connection string. This helps avoid typos and issues you might be running into with a plaintext connection string. After that, you can actually set the AuthenticationMethod directly on the builder.
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder ()
{
DataSource = "ServerName",
InitialCatalog = "DatabaseName",
etc...
}
builder.Authentication = SqlAuthenticationMethod.ActiveDirectoryInteractive;
SqlConnection sqlConnection = new SqlConnection(builder.ConnectionString);
If the SQL db is an Azure SQL db, then you can view the overview page of the resource and find the "Connection Strings" blade as well. This might help troubleshoot the issue you're having with the connection string you're providing.
Check out this answer about building connection strings for more info

How to use my .NET Core Chatbot to respond with answers based on queries to an Azure SQL database?

I am able to connect to and query the Azure SQL database through my Chatbot in the Bot Framework Emulator, However when I deploy it to azure it doesn't work at all.
I have created a chatbot and have used Microsofts Enterprise Bot template as my starting point.
I have managed to connect it to my own knowledge base and luis model and am also dispatching fine between the two. I have also managed to incorporate user authentication and MS flows.
I have deployed it to Azure at the point and all the features and Dialogues worked in the Web-Chat feature on the portal.
I have now also created an azure hosted sql database and am querying it through the chatbot. I connect to the database as shown below:
public DataRow[] GetAllValues()
{
DataTable Library = new DataTable();
DataRow[] foundRows;
using (var connection = new SqlConnection(
"Server=tcp:<MY_SERVER_NAME>,1433;" +
"Database=<MY_DATABASE>;User ID=<My_USERNAME>;" +
"Password=<MY_PASSWORD>;Encrypt=True;" +
"TrustServerCertificate=False;Connection Timeout=30;"
))
{
connection.Open();
using (var command = new SqlCommand())
{
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = $"SELECT * FROM <MY_TABLE>";
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(Library);
foundRows = Library.Select();
return foundRows;
}
}
}
All the features of this work when I test it on the Emulator and it responds exactly as I want.
However, when i deploy it to azure, the bot no longer works in any shape or form, the welcome card doesn't even pop up anymore.
This is the only place in my code that i reference the connection to the Database, do i need to reference it somewhere else also, or is there another issue?
I am guessing this is a new Web App Bot you deployed in Azure. There are a couple of common issues when deploying the first time.
First, make sure you delete the additional files in Azure. This should be an option in your publish profile.
Second, I would delete the application settings in the Azure Portal referring to bot file path, secret, and others that you have configured in you appsettings.json.
After confirming your microsoftappid, microsoftapppassword are correct, you can try debugging the code locally, but using an actual channel (Test in Web Chat, Teams, etc) by using NGrok. If that works, and republishing doesn't work, then use the channels blade in the portal to see if you can identify any issues under the 'health' column. Addtionally, you can add stdoutLogEnabled="true" in your configuration file and then use Kudu to review the logs.

Getting Transact SQL query result in C# Application

I'm trying to write a C# application that restores many databases from .bak files placed in a certain folder. To do so I need the databases logical name from the files.
So I want to execute the following query :
RESTORE FILELISTONLY FROM DISK = N'C:\Folder\File'
The problem is: I can't get the result of this to my application, when I execute it in SQL Server Management Studio, it shows the name I want.
I tried to use ExecuteReader function from SqlDataReader but it doesn't return any data.
Can someone help me figure out how to get the result of queries like restore database, backup database into a variable in a C# application ?
Thanks in advance
The command RESTORE FILELISTONLY does return a result set as per the documentation RESTORE Statement - FILELISTONLY. What I did find during testing my code, you need to make sure the user that is running the application has permissions to read the specified file and directory. Otherwise you will run into issues.
For a code example of this:
var command = #"RESTORE FILELISTONLY FROM DISK = N'F:\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Backup\Scratch.bak'";
using (var sqlConnection = new SqlConnection("Server=localhost;Database=Scratch;Trusted_Connection=True;"))
using (var sqlCommand = new SqlCommand(command, sqlConnection))
{
sqlConnection.Open();
var sqlDataReader = sqlCommand.ExecuteReader();
while (sqlDataReader.Read())
{
Console.WriteLine($"Logical Name: {sqlDataReader["LogicalName"]}");
Console.WriteLine($"Physical Name: {sqlDataReader["PhysicalName"]}");
Console.WriteLine($"Type: {sqlDataReader["Type"]}");
}
}
You may also find that when trying to work SQL Server management, using the SqlConnection and SqlCommand objects may be more frustrating then they are worth. I strong recommend using the SQL Server Management Objects (SMO). This is actually what management studio uses when you are work with SQL Server. I have an example on GitHub if you are interested in it. Checkout GitHub: SMO Demo.

how to deploy winform app with mdf file on server

I have a winform app with a database running on sql server 2012. I want the app to work on multiple computers connected to a server in the local network.
I deployed the app using clickonce and moved the mdf file to the server.
Then I installed the app in a few computers and it works on every one of them separately however when the app runs on one computer and I try to open it on another one I get the following exception:
Cannot open database (database name) requested by the login. The login failed.\r\nLogin failed for user 'USER-PC\user'
using (SqlConnection con = new SqlConnection(conString))
using (SqlCommand command = new SqlCommand("select 1", con))
{
con.Open(); //exception thrown here
object returnValue = command.ExecuteScalar();
if (returnValue != null)
returnString = returnValue.ToString();
con.Close();
}
The connection string is:
Data Source=(LocalDB)\v11.0; AttachDbFilename=path on server\database
name; Integrated Security=true;
I have tried to change attachdbfilename to Initial Catalog=database name, and add users to the db in ssms and add user id and password to the connection string. Nothing helped.
I'm afraid I have some basic concept misunderstood and doing it the wrong way.
How can I resolve this problem?
Please check/correct broken links between Login and DB-User with sp_change_users_login.

SQL Server Azure "Login failed for User"

I am using the following code to perform SQL commands to my azure DB. The I do two calls inside my ASP.NET MVC action method. One to delete from Table A, and the second call to delete from Table B.
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString))
{
using (System.Data.SqlClient.SqlCommand command = conn.CreateCommand())
{
conn.Open();
command.CommandText = statement;
command.ExecuteNonQuery();
}
}
For whatever reason when I make the second call to this code (I have it in a Helper service class, it bombs with the following Azure error.
Additional information: Login failed for user 'MyUser'.
Is there something I'm not doing correctly but not perhaps closing a connection or something that Azure is having issues with this?
You need allow your IP Address to access azure database. click in configure and add your IP.
I just needed add to the connection string
Persist Security Info=False;
when you publish your project into azure from visual studio, there is a 'settings' tab on the left.
go to the settings. it will show you the connection string that you're using in the web.config.
what worked for me is I unchecked the check box that says 'Use this connection string at runtime (update destination web.config)' and everything went well for me.

Categories

Resources