Enterprise library logging + dynamic connection string - c#

I am using the enterprise library logging and exception handling.
Currently I am adding the connection string in the configuration file, and this means that it is static.
My problem is that the user may connect to a different database each time depending on his user name. Is there any way to change the connection string at run time?

I created a CustomTraceListener

You can set the Connection string at run time by using ConfiguratiomManager class from System.Configuration namaspace as below;
ConfigurationManager.ConnectionStrings[0].ConnectionString = "Your new Connection string";

Related

Passing connection string from linqpad to custom library causes login failure

I am writing a linqpad script for migrating data into a db from an excel file. Once the data has been imported it needs to be synchronized between multiple dbs. This has been done in a dll as the functionality is used elsewhere.
My issue is that when passing a connection string to my dll from linqpad I get the message Login failed for user 'xxxxxxx'. As far as I can tell it is because the password for the SQL authentication is not passed in the connection string. IS there any way to make linqpad include the password in the connection string:
SyncController syncController = new SyncController(this.Connection.ConnectionString);
syncController.SyncAll();
If it is at all possible I want to avoid forcing the person who will be running this to update a password variable when running this. Thanks.
You're right: the password will disappear from the connection string after the connection is opened. This is a feature of the .NET Framework, and is not specific to LINQPad.
There are two workarounds. First, you can capture the connection string as the first line in your LINQPad script, before querying any data:
string cxString = this.Connection.ConnectionString;
The other workaround is to add the following text to your connection string:
Persist Security Info=true
To do this in LINQPad, right-click the connection and choose Properties. Click Advanced, and enter the additional connection string text into the box provided.

SSIS password stripped from connection string in database configuration

I have a SSIS package that is using SSIS Configuration table in a SQL server to retrieve the connection string property for a OLE DB connection manager.
Thing is that I also need that same connection string to invoke an assembly that uses Entity Framework. I've tried to access the connection manager connection string property but SSIS always strips the password so the connection string goes incomplete to the EF Code First Context and gives login errors.
Any ideas to overcome this?! thanks
I've already gone another path. It seems that the connection string property in the configuration manager always "hides" the password for security reasons so I'm not reusing that property and instead I'm getting all I need from the SSIS configuration file.

Delivering SQLServer based app on client System

how can i manage connection string of an ADO.net Object model dynamically?
i have connected my application to database using Entity Framework and connection string is automatically added to app.config. how can i get list of available servers and change connections string based on that in the run-time?
Just because VS adds the connection string to app.config by default it doesn't mean that is a good idea and if you do keep the connection string there you cannot change it at run time.
One solution is to keep encrypted connection string in your settings and that will allow you to dynamically change it at run time by simply calling:
Settings.Default.Save();
and
Settings.Default.Reload();

Generating a Connection String in C#

I'm trying to write an application that will let users connect to an arbitrary database, assuming they have the proper ODBC drivers. However, I'm getting stuck on the connection strings. For any particular database type it's easy to look online and find which keyword/value pairs are required, but is that information accessible from the driver itself?
What I want to do is have the user select the driver they want from a list (generated by SQLDrivers), then generate a form that will prompt them for the keywords necessary to generate the string, so that my application does not need to know about specific ODBC drivers. Is there any way to do this? Or will I have to hard code support for each one separately? Maybe connection strings aren't the way to go, and there's another method I'm overlooking?
You can take a look at ConnectionStringBuilders and see if they'll be any help.
You can use the following code to enumerate the configurable properties, which would allow you to create your config form.
var builder = new SqlConnectionStringBuilder();
var properties = (from key in builder.Keys.OfType<string>()
select key);
properties contains:
Data Source
Failover Partner
AttachDbFilename
Initial Catalog
Integrated Security
Persist Security Info
User ID
Password
Enlist
Pooling
Min Pool Size
Max Pool Size
Asynchronous Processing
Connection Reset
MultipleActiveResultSets
Replication
Connect Timeout
Encrypt
TrustServerCertificate
Load Balance Timeout
Network Library
Packet Size
Type System Version
Application Name
Current Language
Workstation ID
User Instance
Context Connection
Transaction Binding
In 2010 Microsoft released there VS2010 Connection dialog for us little people to use. It is awesome, should do what you want and is freely distributable. The download for the dialog and its source code can be found here.
To get you started: once you have compiled the code you will need to include references to Microsoft.Data.ConnectionUI, Microsoft.Data.ConnectionUI.Dialog and Microsoft.Data.ConnectionUI.SqlCeDataProvider. The dialog is then called like
// Initialisation.
DataConnectionDialog dcd = new DataConnectionDialog();
DataConnectionConfiguration dcs = new DataConnectionConfiguration(null);
dcs.LoadConfiguration(dcd);
// Edit existing connection string.
if (!String.IsNullOrEmpty(strExistingConn))
dcd.ConnectionString = strExistingConn;
// Launch Microsoft's SqlConnection dialog.
string strSqlFinConn = String.Empty;
if (DataConnectionDialog.Show(dcd) == DialogResult.OK)
{
// Load tables as test.
using (SqlConnection connection = new SqlConnection(dcd.ConnectionString))
connection.Open();
}
dcs.SaveConfiguration(dcd);
return dcd.ConnectionString;
If this is not what you want then #HackedByChinese answer looks like the best approach.
I hope this helps.
I found a very simple way to solve this, actually, at http://msdn.microsoft.com/en-us/library/windows/desktop/ms715433(v=vs.85).aspx.
If you call the ODBC API function SQLDriverConnect with an incomplete connection string (like, for example, one that only contains the Driver keyword) and set the last argument to SQL_DRIVER_COMPLETE, it will pop up with a dialog generated by the driver that prompts for any required information that wasn't included, then return the finished connection string once it's closed.
Apart from the good answers already posted you might want to take a look at the ODBC API SQLBrowseConnect.

using app.config file for connection string, and abstract it from users

I have used app.config file for my winform application , the file is used to store the connection string which is used by all the Forms in my application to connect to a remote MySQL database.
But when I install the application on my customer's PCs, then I want that they could not see the app.config file. Is it possible? How? Please help with code.
Also, is there any other way, to create a connection string which is accessible by all the Forms. Like, can I make a class connection.cs and then inherit it in every Form.
But how to do this? I mean how to implement the code
My main objective is to create just one string for connection, so that , if i change it again and again, then i need not go and change it every Form, instead, i would just change it only in one File , and it would be used by all the Forms
Is using app.config file a good option or making a connection.cs file's a better one?
You don't need to use a connection string from every form, you need a data access layer and then you use it from everywhere, in theory only from another layer called business logic...
A form which needs to load data into a grid, a drop down or some other controls should consume data loaded by lower layers in your application stack.
Read something about 3 tier architecture.
The app.config is always visible on the user machine, so you should not treat any information stored in it as secret.
You really have two options:
Continue to store the connection string in the app.config but encrypt it. This will work fine if its an internal app and security is not to much of an issue. But since the encryption key has to be stored in the app a dedicated hacker could retrieve it.
use a three tier architecture as suggested already. With this the connection string is stored in the middle tier, while your application no longer connects directly to the database but rather through the middle tier. Authentication can then be done with a user name/password per user or by making use of windows authentication. The connection string is stored on a server and only people with acces to this server can look at it and see the DB connection string.
If you just want a simple solutions why not create a class named for example "Connection" in a file connection.cs and let it have a static attribute or property named for example "ConString" which holds the connection string:
public class Connection
{
public static ConString = "your connection string here";
}
Then you can access it everywhere:
OdbcConnection conn = new OdbcConnection(Connection.ConString);
BUT that would only be the quick and dirty way of doing it (although it works). It would be much nicer to create an own Database-Layer - but also much more work.
App.config can't be hidden on users machine, this is what you can do.
You can encrypt the connection string and store it in the app.config. have a look on this article, it shows you how to do that.
Try to define your connection string in program.cs before [statThread] by storing it in a public static string variable like constr etc. Then u can use that var anywhere referencing:
program.constr

Categories