Connecting to Multiple Connection Strings at one click - connection-string

I have to connect to some 200 DB2 Databases and pull meta data and store those data in another Master SQL Server for reporting purposes. Now, when a user clicks "run a script". The Application should get connected to one DB2 Database pull data, store it in Master SQL Server. After that, again connecting to next DB2 Database and the process goes on. It will be very helpful if someone can suggest the best possible method to store and dynamically change these connection strings one after another when the script is running.
We also have to add or remove as and when new databases are turned over to us or remove if decommissioned.

If you use xml, it is relatively very easy to manage. Just store each connection string as a node like below
<connectionStrings>
<connectionString />
<connectionString />
<connectionString />
<connectionString />
<connectionString />
</connectionStrings>
Now from your application (any language that can consume XML) loop through every connectionstring node and write logic to connect and store meta data within the loop.
Your task is done.

Related

Change chosen SQL Server database in distributed database with Invantive Data Hub

We run a daily job to load Exact Online into a SQL Server database for reporting purposes with Sumatra.
We now want to redirect the SQL insert statement to another database on the same SQL Server.
Connections are:
<connection name="EOLIN_MUT" ...>
<database order="20" alias="eol" provider="ExactOnlineAll" connectionString="apiUrl=https://start.exactonline.nl;api-client-id=SOMETHING;apiredirecturl=https://eolclientredirect.invantive.com" />
<database order="10" alias="sql" provider="SqlServer" connectionString="Data Source=something;UID=datahub;PWD=moresecrets" AllowConnectionStringRewrite="false" />
I've already change the name of the connection from EOLIN to EOLIN_MUT but without result.
How can I redirect to another database?
You have three alternatives that you can use:
Change default database of user
Change default database on connection
Switch database.
To change default database of the user, have you SQL Admin change it in SQL Server. There is no database listed yet in your connection string.
To change default database of connection, see connectionstrings.com on SQL Server. Add database=NAME; to your connection string in settings.xml.
To switch database, change the Invantive SQL script you use by adding:
use XYZ#sql
where XYZ is the intended default database on SQL Server and sql is the alias on the SQL Server data container.
With use, you can also select multiple data containers, such as:
use XYZ#sql, 123123#eol, 456456#eol
which select XYZ on SQL Server and companies/administrations/divisions 123123 and 456456 on Exact Online with alias eol. More documentation on use statements.
Please note that the default SQL Server provider of Invantive does NOT allow you to select multiple database to be used for a query. So when using Exact Globe or Navision, you will need to explicitly include all companies in your query.

How to refer to multiple SQL Server databases in app.config connection string

I am running SQL Server unit tests and have two test databases on which I want to test, "A" and "B". In my VSTS Release definition, I deploy my DACPAC files for both of these test DBs to the same server. In my app.config file that is copied to the build output, I have the following connection string, which references database "A" as the "Initial Catalog":
<SqlUnitTesting>
<DataGeneration ClearDatabase="true" />
<ExecutionContext Provider="System.Data.SqlClient" ConnectionString="Data Source=XYZ;Initial Catalog=A;Integrated Security=True;Pooling=False"
CommandTimeout="30000" />
<PrivilegedContext Provider="System.Data.SqlClient" ConnectionString="Data Source=XYZ;Initial Catalog=A;Integrated Security=True;Pooling=False"
CommandTimeout="30000" />
</SqlUnitTesting>
If I also want to connect to database "B", how can I do this?
Could I create two Initial Catalogs in the same connection string?
Could I create two configSections, where one has the connection string refer to "A" and the other refers to "B"?
Could I remove Initial Catalog from my connection string so that it does not only limit me to "A"?
Could I create two app.config files and have them in the same location?
Thank you.
Answering my own question. I learned that each project has its own app.config, and when the projects build, they are outputted as .dll.config files. In each one of those, I specify the same server, but I use the different databases as the Initial Catalog values. By doing this, each project looks at its respective .dll.config and the db connections are successfully established.

Unable to access SQL Server database on a different machine

I'm developing an asp.net mvc application at my office. I wanted to work at home as well, so, I pushed my project on github, when I came my home back, I pulled it from github to my projects folder, now I tried to access its SQL Server database in server explorer using windows authentication, on pressing ok, it showed me the following error :
Cannot open database "HrAndPayrollSystem.Models.HrAndPayroll" requested by the login. The login failed.
Login failed for user 'user'.
and here is my connection string :
<connectionStrings><add name="HrAndPayroll" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=HrAndPayrollSystem.Models.HrAndPayroll;Integrated Security=False" providerName="System.Data.SqlClient" />
What could be the reason, how should I resolve it? Thanks in Advance :)
So, I somehow managed to get through this.
In other words, this error says that you need to access your database at least for a single time, means you need to perform some transactions with the database, inserting something or retrieving something, at this point you accessed the database for the first time on the different machine. Since, I had to access database at the point when my application starts, so, crashing start crashes my whole application, so I created another controller that does not contain any authorize attribute, just to leave it open in order to access the database, created a single field, accessed the database, now go and check your database in server explorer, you are about to access it for the first time in the database, and now also, it will never crash anywhere.
So, the point is that you need to access your database at least for a first time..

External SQL Server access in MVC 3 + Entity Framework for query-only

I'm devoloping a system using MVC 3 with EF 4. In this system, I need to lookup some information that already exists in an external database.
Saving me project's information on my database is OK. I defined my Models on it and let DbContext create the tables on my server.
The problem is when I try to query some data from the external database. I've added the connection string in the properly place on Web.config:
<connectionStrings>
<add name="ExtDBConnectionString" connectionString="Data Source=path.to.server;Initial Catalog=DBName;Persist Security Info=True;User ID=user;Password=pass;Pooling=True;Min Pool Size=5;Max Pool Size=60;Connect Timeout=2;" providerName="System.Data.SqlClient" />
</connectionStrings>
Is it possible to just create some queries to this server on my project without the overload of recreating all the Models from this external database? I know I can recover this connection string on my code by using the command System.Configuration.ConfigurationManager.ConnectionStrings["ExtDBConnectionString"].ConnectionString, but how can I create a connection and instantiate my queries?
Thanks in advance!
Edit: #rouen called my atention I didn't explicitly said the databases are differents. They do not share the same schema! The external database is the output of a report; so, I need to traverse it on my project to import the relevant lines to the local database.
I would just use standard ADO.NET. Use the SqlConnection and SqlCommand classes.
ADO.NET Examples on MSDN:
http://msdn.microsoft.com/en-us/library/dw70f090.aspx
Is the remote database schemantically identical as your local ? If yes, you can pass connectionstring to constructor of ObjectContext/DbContext, and it will use that database.
http://msdn.microsoft.com/en-us/library/gg679467%28v=vs.103%29.aspx

What is the point of "Initial Catalog" in a SQL Server connection string?

Every SQL Server connection string I ever see looks something like this:
Data Source=MyLocalSqlServerInstance;Initial Catalog=My Nifty Database;
Integrated Security=SSPI;
Do I need the Initial Catalog setting? (Apparently not, since the app I'm working on appears to work without it.)
Well, then, what's it for?
If the user name that is in the connection string has access to more then one database you have to specify the database you want the connection string to connect to. If your user has only one database available then you are correct that it doesn't matter. But it is good practice to put this in your connection string.
This is the initial database of the data source when you connect.
Edited for clarity:
If you have multiple databases in your SQL Server instance and you don't want to use the default database, you need some way to specify which one you are going to use.
Setting an Initial Catalog allows you to set the database that queries run on that connection will use by default. If you do not set this for a connection to a server in which multiple databases are present, in many cases you will be required to have a USE statement in every query in order to explicitly declare which database you are trying to run the query on. The Initial Catalog setting is a good way of explicitly declaring a default database.

Resources