Change ProgramName for SQL Server connection pool - sql-server

I trying to identify the corporate users using the connections from the connection pool (C3P0) in a JAVA EE application. I executing the statement to set connection info before I give the connection to hibernate and setting it to empty string when the connection is returned to the pool.
For PostgreSQL:
SET application_name TO 'MyCorporateSystem - Billing - John Doe';
For Oracle:
call DBMS_SESSION.SET_IDENTIFIER('MyCorporateSystem - Billing - John Doe');
Microsoft SQL Server also has a ProgramName column on 'sp_who2', but it seems that you cannot set it after the connection has been made, only in the connection string, is that correct? The only way is to use CONTEXT_INFO ?

You are correct. This cannot be changed in t-sql. It is part of the connection string.

Related

Exclusive client application to Always Encrypted database

SQL Server 2016, encrypted with always encrypted, using certificate. In order for the client application to receive the decrypted data, it is enough to have a certificate installed on the PC, and connection string with ColumnEncryption = Enabled;.
But that means that on that PC can any application access database - just know the connection string. How do I make sure that on PC with the certificate installed - has only one / certain application access to database and his decrypted data?
there is a way, Application Name in Connection String .restricting-access-to-database-by-application-name-andor-host-name-in-sql-server
CREATE TRIGGER RestrictAccessPerApplication
ON ALL SERVER
FOR LOGON
AS
BEGIN
IF
(PROGRAM_NAME() = 'Microsoft® Access' AND HOST_NAME() = 'WORKSTATION_01')
BEGIN
ROLLBACK;
END
END
But i don't think this is good for this type of sensitive applications.

What should the connection string be when using Azure Sql Failover groups?

Ok so I have set up a Azure Sql Failover group at mydatabase.databsae.windows.net which contains 2 servers:
mydatabase1.databsae.windows.net
mydatabase2.databsae.windows.net
I go to connectionstrings.com to get my Sql Azure Connection String which looks like so:
Server=tcp:mydatabase.database.windows.net;Database=mySqlDatabase;
User ID=MyUser#[serverName];Password=myPassword;Trusted_Connection=False;
Encrypt=True;
Now the problem lies in User ID=MyUser#[serverName] I have tried:
User ID=MyUser#mydatabase but that didn't work and repeatedly returns the error:
Database 'mySqlDatabase' on server 'mydatabase' is not currently
available. Please retry the connection later
I also tried User ID=MyUser#mydatabase1 and that did work.
But the problem with the above in the connection string is that when I do failover to mydatabase2 I will need to go and update all my connection strings.
So what is the correct connection string when using Sql Failover groups?
This is a current limitation of failover groups:
Note: If at this point you go to SSMS and try to connect to your
Primary/Secondary database using above listeners, you will receive
error and will not be able to login. Ideally it should have allowed,
but it currently fails, as it tries to connect to the Master database
which is currently not part of the group. This is currently being
worked upon and should be resolved soon. Till then, workaround is to
provide the database name while connecting to server. Use the option
button to provide database name.
what this means is that currently only the following connection strings will work:
Server=tcp:mydatabase.database.windows.net;Database=mySqlDatabase;
User ID=MyUser#mydatabase1;Password=myPassword;Trusted_Connection=False;
Encrypt=True;
OR
Server=tcp:mydatabase.database.windows.net;Database=mySqlDatabase;
User ID=MyUser#mydatabase2;Password=myPassword;Trusted_Connection=False;
Encrypt=True;
Unfortunately this renders the automatic failover completely useless as you need to then reconfigure all your connection strings
Ref: https://social.technet.microsoft.com/wiki/contents/articles/37968.working-with-azure-sql-auto-failover-group.aspx
Try :
Server=tcp:mydatabase.database.windows.net;Database=mySqlDatabase;
User ID=MyUser#mydatabase;Password=myPassword;Trusted_Connection=False;
Encrypt=True;MultiSubnetFailover=True;

ColdFusion not maintaining connection to Azure Data Warehouse

Our ColdFusion 2016 Enterprise server (Windows Server 2012 R2) is not maintaining connections to an Azure Data Warehouse. The first Azure query on a page takes a second or more to run. Subsequent Azure queries on the same page take a fraction of a second, e.g.:
test1 (Datasource=azureDev, **Time=3485ms**, Records=1) in D:\DW\dwtest\CF2016\bob\azureAdhoc.cfm # 12:10:12.012
select count(*) cnt from dimpatient where name like 'smith%' and birthdate >'2014-02-01'
test2 (Datasource=AzureDev, **Time=125ms**, Records=3) in D:\DW\dwtest\CF2016\bob\azureAdhoc.cfm # 12:10:12.012
select * from dbo.dimPatientMergeStatus
test3 (Datasource=azureDev, **Time=281ms**, Records=1) in D:\DW\dwtest\CF2016\bob\azureAdhoc.cfm # 12:10:13.013
select count(*) cnt from dimpatient where name like 'jones%' and birthdate >'2004-02-01'
It seems apparent that CF is taking extra time to actually make the connection while running the first query on the page. We've tried with various queries and re-arranging their order and always end up with the same result.
We are connecting to Azure using the latest MS jdbc driver (mssql-jdbc-6.2.2.jre8.jar) and 'Maintain Connections' is checked. We first attempted to connect using the built in Microsoft SQL Server driver but kept getting this error:
Connection verification failed for data source: AzureDev2
java.sql.SQLException: [Macromedia][SQLServer JDBC Driver]Error
fetching requested database meta-data info.
We do not see this issue when we run the queries in SSMS.
Any idea what might be wrong?
How does ColdFusion Server manage database connections when there are client variables?
With ColdFusion Server version 4.5.1 SP1 and higher, when you store
your client variables in a database, your code connects to the
database only when a variable is set. This prevents unnecessary
database connections, for instance, in a case where you are using
client management, but no client variables are present in a particular
request.
https://helpx.adobe.com/coldfusion/kb/database-connections-handled-coldfusion.html

How to connect to simple database table?

I don't know much about databases - Sorry if the question seems silly.
I have sql server 2012 on my machine and i create simple database table.
I want to connect to this database table thru C# code.
So, I need to know my ConnectionString.
I don't understand the parameters of the ConnectionString.
I try to google it - but still didn't find any good explanation.
Anyone can please explain the connectionString fields ?
How to define the connectionString that i will be able to connect the local database ?
thanks
Your connection string should be as simple as like below
Data Source=.;Initial Catalog=DB_NAME;Integrated Security=True"
Where
Data Source=. means local database
Initial Catalog=DB_NAME means the database it will connect to
Integrated Security=True means it will use windows authentication (no user name and password needed; it will use logged in credential)
Take a look Here
(OR)
Search in Google with key term sqlconncectionstring which will fetch you many help.
EDIT:
You are getting exception cause Initial Catalog=DB_Name\Table_001. It should be Initial Catalog=DB_Name (only database name). Provide the table name in sql query to execute. Check some online tutorial to get more idea on the same.
You use . in data source only when you are connecting to local machine database and to the default SQL Server instance. Else if you are using different server and named SQL Server instance then your connection string should look like
using(SqlConnection sqlConnection = new SqlConnection())
{
sqlConnection.ConnectionString =
#"Data Source=Actual_server_name\actual_sqlserver_instance_name;
Initial Catalog=actual_database_name_Name;
Integrated Security=True;";
sqlConnection.Open();
}
In case you are using local machine but named SQL Server instance then use
Data Source=.\actual_sqlserver_instance_name;
Initial Catalog=Actual_Database_NAME;Integrated Security=True"
using System.Data.SqlClient;
Then create a SqlConnection and specifying the connection string.
SqlConnection myConnection = new SqlConnection("user id=username;" +
"password=password;server=serverurl;" +
"Trusted_Connection=yes;" +
"database=database; " +
"connection timeout=30");
Note: line break in connection string is for formatting purposes only
SqlConnection.ConnectionString
The connection string is simply a compilation of options and values to specify how and what to connect to. Upon investigating the Visual Studio .NET help files I discovered that several fields had multiple names that worked the same, like Password and Pwd work interchangeably.
User ID
The User ID is used when you are using SQL Authentication. In my experience this is ignored when using a Trusted_Connection, or Windows Authentication. If the username is associated with a password Password or Pwd will be used.
"user id=userid;"
Password or Pwd
The password field is to be used with the User ID, it just wouldn't make sense to log in without a username, just a password. Both Password and Pwd are completely interchangeable.
"Password=validpassword;"-or-
"Pwd=validpassword;"
Data Source or Server or Address or Addr or Network Address
Upon looking in the MSDN documentation I found that there are several ways to specify the network address. The documentation mentions no differences between them and they appear to be interchangeable. The address is an valid network address, for brevity I am only using the localhost address in the examples.
"Data Source=localhost;"
-or-
"Server=localhost;"
-or-
"Address=localhost;"-or-"Addr=localhost;"
-or-"Network Address=localhost;"
Integrated Sercurity or Trusted_Connection
Integrated Security and Trusted_Connection are used to specify wheter the connnection is secure, such as Windows Authentication or SSPI. The recognized values are true, false, and sspi. According to the MSDN documentation sspi is equivalent to true. Note: I do not know how SSPI works, or affects the connection.
Connect Timeout or Connection Timeout
These specify the time, in seconds, to wait for the server to respond before generating an error. The default value is 15 (seconds).
"Connect Timeout=10;"-or-
"Connection Timeout=10;"
Initial Catalog or Database
Initial Catalog and Database are simply two ways of selecting the database associated with the connection.
"Inital Catalog=main;"
-or-
"Database=main;"

Connection Failed SQL Server

I have the following code snippet:
SqlConnection sr = new SqlConnection(this.strConnection);
sr.Open();
Where connection string is:
Data Source = localhost;Initial Catalog=UIB_IS;Persist Security info=True;User ID=USR;Password=PSW;
(I have removed the user name and password from above for purpose of this question - I have double checked the password has not expired and is still the same.)
Every time I try to open the connection I get a failed exception.
I have also tried using the Microsoft.SqlServer.Management.Smo.Server() object with a string connection as well as a ServerConnection object. With the server connection object using a string and SQLConnection object.
None seem to be working.
Within the connection string I have tried the syntax ServerName\SQLSERVERNAME, 127.0.0.1 as well as localhost as you can see above.
Previously this code has been working over the last 6 months using the 'ServerName\SQSERVERNAME' syntax in the connection string on a Microsoft.SqlServer.Management.Smo.Server() object.
This last week.....no longer works.
No System updates have been applied. The DB and SQL User exist with the appropriate permissions set as were before. SQL Server still has allow remote connections set to allow.
The server firewall is also off (Development machine)
I cannot understand why it would suddenly stop working?
Anyone have any ideas?
Thanks in advance.
Be sure that your Sql Server allow remote connections. You can check its status under Sql Server Surface Area Configuration --> Surface Area Configuration for Services and Connection.

Resources