How to find which application changes a table - sql

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

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.

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.

How to deploy windows form application with entity framework

I am busy developing my own application, it uses a sql server database and it is connected through an entity framework. I use store procedures to insert, update, delete, select from my database.
The app works perfect on my machine even when I publish it. But my problem comes in when I try to install the app to my friend’s computer. It crashes and does not start because it cannot connect to the database.
Is there a way to publish my app with the database, without importing all the tables, store procedures and database into my friend’s pc? I just want to make it so the user just has to install the app and it works.
Your app relies on the db to work, so if you want to put it on your friends PC then you need to make the DB available somewhere, whether it be a local copy or a copy stored on a server somewhere running SQL.
How are you storing your connection string? Is it hard coded in the app or are you utilising the app.config file? To do what you're trying to do you'll need to put the connection string into the app.config file, so you can change it depending on the installation.
either that or
if you want to run your app without data, put a demo flag or something into the app.config file. Put some code into your app to check this value, if it's true then bypass the SQL code and maybe supply some demo data which is hard coded.
Does this make sense?
You could use SQL CE, but you may find it a little more difficult to 'design' your database in it if you are more used to working in SQL Server.
Have you considered SQL Server Express as an option?
On the connection string issue, you can now get the data connection wizard that Microsoft use in Visual Studio via Nuget; this makes adding a way to dynamically configure connection strings on your clients machine much easier.
Lastly, connection strings for the entity framework are different from standard SQL connection strings. Make sure that you clearly understand the differences before you start trying to configure them programmatically. Julie Lerman's excellent book on the Entity Framework explains the differences well.

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

Few simple questions on managing different types of databases in Visual Studio SQL manager

Recently started with ASP.NET and MVC and have a few questions on working with databases.
I have the following connection string:
<add name="EFDbContext" connectionString="data source=.\SQLEXPRESS;InitialCatalog=EFDbContext;MultipleActiveResultSets=True;IntegratedSecurity=SSPI;" providerName="System.Data.SqlClient" />
And a class (EFDbContextInitializer) that derives from another class that implements the IDatbaseInitializer interface which creates the database if database does not exist. However when it's created it works and all and the database
is obviously created somewhere. However I cannot see it in my SQL
manager in Visual Studio. The database is probably mounted with the built in ./SQLEXPESS so I
figure I should be able to access it somehow when application is not running ? Where is the thing?
Also I have another existing database an *.mdf type that I want to
transfer a bunch of tables to from the above database that
(EFDbContextInitializer) created. The tables I want to export are for the ASPNET
membership provider implementation . I actually used the *.mdf database at first with my
application, but then decided to exclude it from the project so that a
new database could be created with the proper tables for the
membership provider as I don't think it's possible or practical to
try and add those tables to an existing database using the the
(EFDbContextInitializer) way and now I don't want to recreate all the
data in my *.mdf database that I excluded from the project or manually put anything into seed() method. Just want to use my *.mdf database again and add the membership provider tables to it that were created by the (EFDbContextInitializer) in that illusive database.
What is the best tool or method to go about transferring those tables
to the *.mdf database? If I gotta write lots of T-SQL queries please point to some examples. I probably don't know how to use it but I don't think there's
anyway this can be done in the SQL manager in Visual Studio? I know there's compare database
option where you can get the target to match the source, or just get certain tables but once
again I don't know where to find that database that was created automatically. Also should I look
into installing some SQL database management tool? I am using Windows 7 Pro so I take it I can't just
install a full blown SQL server 2008 R2 suite and use the management studio there so what other tools are available to me for proper db management.
AD1. Sure you can connect to these DB's - just connect to .\SQLEXPRESS instance of you local MS SQL Express server using Windows Authentication.
Optionally you can check if the database was created for you on the file system by going to :
C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA
AD2. To add Application Services structures (tables,stored procedures, views) you can use aspnet_regsql.exe GUI tool if you are using asp.net 4.0 it's in
c:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regsql.exe
This will recreate the structure for you. Othwerwise if you would like to keep using your .mdf database for Application Services puproses you only need to add connection string entry in your Web.config. Something like the below should work for you
<add name="ApplicationServices"
connectionString="data source=|DataDirectory|aspnetdb.mdf;;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
You should be able to point SQL management studio to .\sqlexpress and see your database
To transfer data you can use the database Publishing wizard. Right click on your database in the server explorer view in visual studio and you should be able to publish to a provider which will generate your scripts etc for you.