I am trying to use nHibernate and Castle and make a connection to one database based on the connection string stored in another database. Is there any way to change the connection string for SessionManager dynamically?
the connectionstring is baked into the sessionfactory. i doubt that you can change it. you could create a lightweight factory to get the connection string or just use ADO.NET directly and then build up the real factory.
Related
Recently I am working on an .Net project. We used EF to handle SQL, when we make an installer of the program, we realize that app.config is visible which mean that the connection string is not safe.
I am looking for a way to add connection string (or maybe secret code and username) to the EF so that the connection string is not visible.
Something like change old code from this
Using db As ConnectDb.adoSentoEntities= New ConnectDb.adoSentoEntities
'TODO
End Using
to this
Using db As ConnectDb.adoSentoEntities= New ConnectDb.adoSentoEntities(ConnectionString)
'TODO
End Using
But since we used connect code to SQL all over the place, changing every single line of code is not possible. There is a way I only need to add connection string once?
You’d be better off encrypting the connection string section in the app.config. You wouldn’t need to make any changes.
Storing any sort of configuration in an assembly can be read using a hex editor.
It’s been answered on here before.
Encrypting Connection String in web.config
You’d be better off using a trusted connection if you’re using SQL Server. The user running the app would need to have permissions and no username and password is required.
Save connection string is settings of project properties.
Go in project properties.
Select settings.
Add new setting as connection string and save connection string.
Then you can use it for whole project.
I have an ASP.NET MVC 5 application. And I use Entity Framework 6 Code First in order to access a database in another machine.
I use the connection string from the DbContext to make another direct sql query to the database, using a micro ORM. In order to get the connection string I use:
myContext.Database.Connection.ConnectionString
The problem is that inside an Action, the connection string changes after a call to the method "Find" on a context DbSet. Previously, the database password is there, but after calling this method, the password just goes away.
Has anybody experienced such change to the connection string?
Thank's in advance.
This is per design. See https://connect.microsoft.com/VisualStudio/feedback/details/514829/datacontext-getcommand-alters-the-contexts-connection-string
You have to save the connection string before it has opened.
Currently I am using SQL Server CE for persisting my data for which I am providing with a .sdf and connection string mentioned in app.config pointing to this .sdf file.
Now I want to provide user with the flexibility to have the data stored in their own SQL Server database if present at there disposal.
Now I am facing the problem of how to change the connection string at runtime if user chooses to uses its own database ?
Or if restrict them to use my predefined .mdf file how to attach that in their SQL Server ?
My recommendation would be to have 2 connection strings in the configuration file (app or web). There is a special section for them intuitively called ConnectionStrings. You can then switch between them based on other settings.
Changing connection strings dynamically is actually pretty easy to do as long as you have a place to store the new settings (ie. web or app config files). If you provide a way for them to enter the server information you can use the ConfigurationManager class to update your app/web.config.
Ado.net typically has parameters on almost any db connection object that allows you to specify the connection string as an arguement. Additionally, there are helper classes that can be used to construct the connection string on the fly like the SqlConnectionStringBuilder or EntityConnectionStringBuilder. I personally love the Entity Framework as it allows you to create the database from the model itself if it does not already exist, provided you already have the connection string.
In C#, how do I manually build a proper connection string? I have the server name, database name, user name, and password. This is a SQL Server database and .NET 4.0.
I looked at the SQLConnectionStringBuilder and that appears to be what I want but I don't know how to specify the server.
On the SqlConnectionStringBuilder use the DataSource property for the server host name/ip address.
Have a look at the examples at MSDN:
MSDN - SqlConnectionStringBuilder Class
The line of code in question:
builder["Server"] = yourServerName;
Your looking for the DataSource Property
Although you could just set the ConnectionString to something like
Data Source =myServerAddress; Initial Catalog =myDataBase; User Id =myUsername; Password =myPassword;
For alternative connection string for SQL Server 2008 see
http://www.connectionstrings.com/sql-server-2008#p1
You're right : SqlConnectionStringBuilder is the way to go. The DataSource property is the server name. You can find more info about this class on the msdn library. One easy way to figure out what properties you have to set may be to initialize a SqlConnectionStringBuilder using an existing connection string and seeing which properties are used.
For instance, it's likely that you'll use IntialCatalog (the name of the database you want to connect to).
I suggest taking a look at connectionstrings.com.
You can use the SQLConnectionStringBuilder constructor overload that takes a connectionstring as described in the above site.
I have an application that needs to connect to a SQL database, and execute a SQL Agent Job.
The connection string I am trying to access is stored in the registry, which is easily enough pulled out.
This appliction is to be run on multiple computers, and I cannot guarantee the format of this connection string being consistent across these computers. Two that I have pulled out for example are:
Data Source=Server1;Initial Catalog=DB1;Integrated Security=SSPI;
Data Source=Server2;Initial Catalog=DB1;Provider=SQLNCLI.1;Integrated Security=SSPI;Auto Translate=False;
I can use an object of type System.Data.SqlClient.SqlConnection to connect to the database with the first connection string, howevever, I get the following error when I pass the second to it:
keyword not supported: 'provider'
Similarly, I can use the an object of type System.Data.OleDb.OleDbConnection to connect to the database with the second connection string, howevever, I get the following error when I pass the first to it:
An OLEDB Provider was not specified in the ConnectionString'
I can solve this by scanning the string for 'Provider' and doing the connect conditionally, however I can't help but feel that there is a better way of doing this, and handle the connection strings in a more generic fashion.
Does anyone have any suggestions?
The normal way of handling this is storing the ADO.NET provider unique name (separately from the connection string) and using a DB Provider Factory.
Use a SqlConnectionStringBuilder, initialize it with the connection string you find in registry and then read its ConnectionString property, which would normalize the connection string to proper SQL Client syntax.