How to customize the SQL Server connection ApplicationName property in Nhibernate for profiling - nhibernate

I am trying to correlate SQL query activity in a SQL Server Studio profile trace with different client processes in a large application that I am new to. The connection ApplicationName column in the trace window is set to ".Net SQLClient Data Provider" for all profiled query activity.
I would like to override the ApplicationName property on each new NHibernate command or connection with more meaningful internal application names associated with the main processes of the large application.
I have set a custom ApplicationName before but is it possible with NHibernate?

the first method I found was to use the connection-string (see links), but that is per connection and not per command and you won't know when .Net/NHibernate/Connection-Pooling/... decides to make a new connection.
Change Application Name in Sql Server Profiler
https://www.connectionstrings.com/use-application-name-sql-server/#:~:text=Simply%20include%20%22Application%20Name%3DMyAppName,%2C%20%22SELECT%20APP_NAME()%3B%22.
I am trying to find an T-SQL-Command to change/set this value from within a connection, but wasn't lucky till now.
Greetings
Juy Juka

Related

Data integration between IBM AS400 to SQL Server database

I'm a web developer that has been tasked with creating some sort of mechanism for moving data from an IBM AS400 to a SQL server. Unfortunately, linked servers are out of the question in this case as the SQL Server is just Standard Edition (db2 providers not available in this version) and the AS400 server is on a separate server. I've researched adding some sort of trigger on the AS400 table that calls a web service that would insert data into the SQL server, but that doesn't seem like the best method. Does anyone have any suggestions on the process to get the data from the AS400 to the SQL Server when it is committed to the AS400?
This solution assumes you are familiar with SQL Server Integration Services (SSIS):
Connection to AS400
Create a new ADO.Net connection Manager
Set the Provider to .Net Provider --> ODBC Data Provider
Create a DSN (Control Panel -->Administrative Tools-->Data Sources ODBC -->System DSN)
In the connection manager for Data source specification select the DSN created. Provide the login information.
Test the connection.
Data flow source:
Use the DataReader source
In Advanced Editor select the Ado.Net connection manager just created.
In Component Properties tab --> Custom properties, in SQLCommand specify the required query string (select * from DatabaseName.TableName)
Check the column mappings for accuracy
Go to Input and Output properties -->Data reader output -->External columns (Select the columns which were of type varchar in the table, they will now be of the datatype UnicodeString (DT_WSTR). This is because by default DataReader reads strings as unicode strings. This implies that in the destination table in SQL these columns must be of type unicode i.e NVARCHAR instead of VARCHAR)
Answer sourced from www.sqlservercentral.com/Forums
I synchronize my web applications with an IBM i. But I have my own database design and wrote a sync program on the Windows side.
Having the same database design I wonder why I would need a copy on SQL server. I would access the IBM directly. Install the drivers as #Kamran Farzami suggested and use them. That way there would be no lag between writes on the mainframe and your queries.
If a lag is acceptable for you and you can't access the IBM i directly, I see three main options:
Pull the data from your Windows system with the OLE DB driver. Using the .NET driver you can use the relative record number (RRN) to remember where you stopped synchronizing.
Read the journal files and make them available by creating a webservice on the IBM i.
Read the journal files in a scheduled job and push the changes from the journal to a webservice which updates the SQL server.
Option 1 only works if the files you sync are not reorganized. The RGZPFM command changes the record numbers. If that's okay, you can get the RRN in your SELECT statement: select *, RRN(MYTABLE) as RRNMYTABLE from MYTABLE
The web service server is included in OS400 since V5R4. So you should be able to use option 2.
I've done something similar where the SQL server was in a remote (Honduras) location where the internet connection was unreliable. It was a short VB program, using the OLE DB driver, running on the server that connected to the AS400 when it was available (or "slept" when the connection was down). When available the program would update/synchronize a uniquely keyed mirror file. Another program uploaded individual transaction records to a separate table (file).
We'd also periodically update SQL Server master tables (i.e. item master) from the AS400. That also utilized a VB program (could be any language using the driver) initiated on the server. It isn't exactly elegant, but more practical than an AS/400 trigger to a web service, I believe.

How to find which application changes a table

Is it possible to find back which application made changes to a database (e.g. update a record)?
I'm using SQL Server 2012.
You might have to ask your developers to start including Application Name in their connection string in web.config files of their application.
Data Source=myServer;Initial Catalog=myDB;User Id=myUsername; Password=myPassword; Application Name=MyApplicationName;
Then you'll be able to use APP_NAME function to track the application connecting to the session or can also be used in the store procs.
Also, you can use Application Name to filter queries in your Sql Profiler, otherwise in absence of Application Name, all application gets generic app name, .NET sqlClient Data Provider

Change data source for SQL Server Reports automatically

We have an ASP .NET MVC web application that links to some SQL Server Reporting Services reports. Our web application uses one database for the morning (let's call this the "AM database") and a different database for the afternoon/night ("PM database").
We have a SQL Server Agent job set up that switches the connection string for the web application from the AM database to the PM database (or vice-versa) on a schedule. It works by copying/replacing the MVC application's Web.config file with the appropriate AM/PM version. This works as intended.
However, the reports will remain pointed to whichever database they were using previously, unless the data source is manually changed through SSRS. These reports need to be updated to point to the same database as the MVC web app.
How can I automate this process (preferably through SQL Server Agent)? We need the reports to change their database connection on a regular interval.
This works for embedded connections I am not sure about shared data source references. Just add two parameters to your report for database name and database server name. You could also use codes or mask the names if you prefer not so send this info over the wire.
In your embedded data set add the following as your connectionstring
expression:="Data Source="+Parameters!DatabaseServerName.Value+";Initial Catalog="&Parameters!DatabaseName.Value
Next, add a defined user for your credentials then all you need to do is pass in the two params for each report from your application.
NOTE: You need to add your defaults when designing to a server that is accessible.

Making OCA with sync service utilize dynamic table name

I'm working an OCA application based on a SQL server db and a SQL CE client db. Each user has there own table in the on the SQL server db. Each user's table has the same schema but can have different data. I'd like to make the synctable name to be dynamically set in the client application based on the their SQL server DB table name (which is set as userID + "__usersdatatable").
The sync service runs without problems when I don't attempt to change anything. My latest idea was to change SyncTable.TableName = "jsmith_usersdatatable"; in the LocalDataCache1SyncAgent.cs in the oninitialized() method. However, I get the following error:
"Unable to initialize the client database, because the schema for table 'zlentz_GOBOS'
could not be retrieved by the GetSchema() method of DbServerSyncProvider. Make sure
that you can establish a connection to the client database and that either the
SelectIncrementalInsertsCommand property or the SelectIncrementalUpdatesCommand
property of the SyncAdapter is specified correctly."
In short the idea is that each OCA will need to sync to a different table (bidirectional).
I'm obviously no SQL or sync Services expert so advice would helpful. :)
Thanks in advanced!
After learning more about the Sync Framework it was apparent that I needed to work outside the wizard.
For future viewers the following link was very helpful:
http://www.codeproject.com/Articles/18027/Take-Data-Offline-Using-Microsoft-Synchronization

Getting current connection properties in SQL Server

In MS SQL Server, the Database Properties dialog has the "View Connection Properties" link over on the left. Clicking that brings the "Connection Properties" dialog with properties of the current connection, such as Authentication Method, Network Protocol, Computer Name, etc...
Is there a way to get that information programmatically by running a sql query? What would that query look like?
SQL 2005 and after you interrogate sys.dm_exec_connections. To retrieve your current connection properties you'd run:
select * from sys.dm_exec_connections
where session_id = ##SPID
The field values depend on the protocol used to connect (shared memory, named pipes or tcp) but all contain information about authentication method used, protocol and client net address.
Yes you can, but it depends on which property you are after as the ones displayed in the connection properties UI come from several places.
It uses several queries (such as xp_msver and select suser_sname()) to get hold of some properties, but it also uses the xp_instance_regread stored procedure to get hold of some values from the registry of the server.
Pretty much everything that is done is management studio when interacting with the SQL engine can be done using SQL. Starting a profiler session and doing the actions in the UI will uncover what (sometimes obscure/undocumented/unsupported) SQL is being run.
From client tool perspective you could use CONNECTIONPROPERTY:
For a request that comes in to the server, this function returns information about the connection properties of the unique connection which supports that request.
SELECT ConnectionProperty('net_transport') AS [Net transport],
ConnectionProperty('protocol_type') AS [Protocol type];
DBFiddle Demo
I think the answer is 'no'.
Computer information is stored on the computer.
Connection information is most likely stored in a configuraton file or in MS SQL Server.
But have a look at the MSSQL system tables and see what properties they have.