How to check which IIS application is calling the database - asp.net

I have 2 application (websites) on one IIS and which is calling same remote database. Is there any thing in SQL server which will give me in stored procedure that who is calling :) ?

APP_NAME() returns the application name for the current session if set by the application. An another way of identifying the app is to log the app name through the CRUD operation , so that an additional field will be added in the tables to hold the app name.

You need to modify your connection strings and add Application Name
Data Source=myServer;
Initial Catalog=myDB;
User Id=myUsername;
Password=myPassword;
Application Name=myApp;
Now when you query sys.processes,program name will be application name
References:
http://johnnycoder.com/blog/2006/10/24/take-advantage-of-application-name/
https://www.connectionstrings.com/sql-server/
Check this thread if you are using VB.NET:
How to set "Application Name" in ADODB connection string
if not above will work

Related

Get application name that executes specific stored procedure

I want to know the application name that executes a specific stored procedure. We have many applications and all have application name property inside the connection string.
Here I don't exactly know which application calling that stored procedure. I think we can get this by calling APP_NAME() but I don't know the exact query to get the correct application name.
SELECT APP_NAME()
The APP_NAME() is a built-in function to return the 'Application Name' token from the connection string used to set up the current session.
Here is a good article on setting up your connection string appropriately:
http://www.sqlerudition.com/using-a-meaningful-application-name-in-the-oledb-connection-string/
If you don't have control over the connection string, then you will need to use a combination of other session variables such as SESSION_USER() or ##SPID and possibly refer to the dmv called sys.dm_exec_sessions for a full record of what the server 'knows' about the connection.
https://learn.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-exec-sessions-transact-sql?view=sql-server-2017
If you want detailed information, i would suggest to use Extended Events by selecting rpc_completed event. For detailed steps..

Specifying default schema WebSpehere 8.5 Server

I am working on a migration project where we are migrating one application from Weblogic to Websphere 8.5 server.
In Weblogic server, we can specify default schema while creating datasource but I don't see same option in WebSpehere 8.5 server.
Is there any custom property through which we can set it , I tried currentSchema=MySchema but it did not work.
This answer requires significantly more work, but I'm including it because it's the designed solution to customize pretty much anything about a connection, including the schema. WebSphere Application Sever allows you to provide/extend a DataStoreHelper.
Knowledge Center document on providing a custom DataStoreHelper
In this case, you can extend com.ibm.websphere.rsadapter.Oracle11gDataStoreHelper.
JavaDoc for Oracle11gDataStoreHelper
The following methods will be of interest:
doConnectionSetup, which performs one-time initialization on a connection when it is first created
doConnectionCleanup, which resets connection state before returning it to the connection pool.
When you override doConnectionSetup, you are supplied with the newly created connection, upon which you can do,
super.doConnectionSetup(connection);
Statement stmt = connection.createStatement();
try {
stmt.execute(sqlToUpdateSchema);
} finally {
stmt.close();
}
doConnectionCleanup lets you account for the possibility that application code that is using the connection might switch the schema to something else. doConnectionCleanup gives you the opportunity to reset it. Again, you are supplied with a connection, upon which you can do,
super.doConnectionCleanup(connection);
Statement stmt = connection.createStatement();
try {
stmt.execute(sqlToUpdateSchema);
} finally {
stmt.close();
}
Note that in both cases, invoking the corresponding super class method is important to ensure you don't wipe out the database-specific initialization/cleanup code that WebSphere Application Server has built in based on the database.
As far as I know Weblogic only allows setting a default schema by setting the 'Init SQLto a SQL string which sets the current schema in the database, such asSQL ALTER SESSION SET CURRENT_SCHEMA=MySchema`. So, this answer is assuming the only way to set the current schema of a data source is via SQL.
In WebSphere, the closest thing to WebLogic's Init SQL is the preTestSQLString property on WebSphere.
The idea of the preTestSQLString property is that WebSphere will execute a very simple SQL statement to verify that you can connect to your database properly when the server is starting. Typically values for this property are really basic things like select 1 from dual', but since you can put in whatever SQL you want, you could setpreTestSQLStringtoSQL ALTER SESSION SET CURRENT_SCHEMA=MySchema`.
Steps from the WebSphere documentation (link):
In the administrative console, click Resources > JDBC providers.
Select a provider and click Data Sources under Additional properties.
Select a data source and click WebSphere Application Server data source properties under Additional properties.
Select the PreTest Connections check box.
Type a value for the PreTest Connection Retry Interval, which is measured in seconds. This property determines the frequency with which a new connection request is made after a pretest operation fails.
Type a valid SQL statement for the PreTest SQL String. Use a reliable SQL command, with minimal performance impact; this statement is processed each time a connection is obtained from the free pool.
For example, "select 1 from dual" in oracle or "SQL select 1" in SQL Server.
Universal Connection Pool (UCP) is a Java connection pool and the whitepaper "UCP with Webshere" shows how to set up UCP as a datasource.
for JDBC datasource, the steps are similar but, you can choose the default JDBC driver option.
Check out the paper for reference.

What is the Data Source syntax SQL Client Connection String to Azure?

I am attempting to configure the connection string in web.confg for an ASP.NET app to connect to Azure DB. I am new to this but am familiar with sql configuration strings to SQL Server. How do I configure the login and data source in the connection string I am building. I haven't ever dealt with the cloud like this. If my db name is DB1 how do I set the Azure path for the data source portion of the connection string. NOTE: I know where to configure it in web.config and know to create a connection object.
Thanks.
In the Azure portal, you can navigate to your database's dashboard and click 'Show connection strings' on the right side. See the first screenshot on the below tutorial:
http://azure.microsoft.com/en-us/documentation/articles/sql-database-dotnet-how-to-use/
To answer your original question, "Data Source" means the same as "Server", it is just your server name, i.e. blah.database.windows.net. All of the connection string keywords (including their synonyms) are documented here: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring%28v=vs.110%29.aspx

get session variable in runtime

There are more web applications (asp.net 4) in own virtual directory of IIS server (under win2003).
And appertain to all own schema and account in oracle 11g database.
After login all users have some session variables (like username,shema name ...etc). I want to get these information for user management in runtime. How can I ask it from the server?
Maybe you are looking for this?
To retrieve:
string username = Session["Username"];
To put things on Session:
Session["Username"]="SomeValue";

What is the Default Database for Entities Code-First without defined Connection String?

I have written a trivial Entities code-first WinForms application with one simple class and one database context class as all the tuturials describe it.
But I did not add a connection string in the app.config file.
Nevertheless, when I start the application, it can insert objects into the database and even show all objects already inserted.
I figure there must be some default database in SQL Server for that case but I cannot find out which instance and database name is used.
Database name is the same as the name on your DbContext class. By default it creates a database on the local machine Sql Server express installation. You should be able to see it in Sql Server Managment when you connect to your local SQLEXPRESS.
If you spesify a connectionstring with the same name as youd DbContext it will use that instead.

Resources