Using the ADO.NET Entity Data Model, how do I pass (or set) the password of the connection string?

I am working with an ADO.NET entity data model for this side project that I am working on. Normally, I would include the entire connection string (user and password) inside web.config, but this morning I felt excited, so I decided to exclude the password from the connection string. Unfortunately for me, I can’t understand how to transfer or set a password before I manipulate the database, and all the searches that I have done so far have not yielded any fruitful results. All I have at present is a few lines that add a new entry:

_entities.AddToUploadSet(uploadFile); _entities.SaveChanges(); 

I see that inside _entities.Connection there is a ConnectionString property, but I did not find anything useful as a password property.

Can anyone help me out? Thanks!

Edit

For explanation, this screenshot shows a step where I can choose whether to include the password in the web.config file or not:

alt text

So, as you can see, I need to install it in the application code, as the prompt suggests.

+4
source share
3 answers
+1
source

Well, I dug up something and tied this solution together:

 private static string CreateNewConnectionString(string connectionName, string password) { var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~").ConnectionStrings.ConnectionStrings[connectionName]; var split = config.ConnectionString.Split(Convert.ToChar(";")); var sb = new System.Text.StringBuilder(); for (var i = 0; i <= (split.Length - 1); i++) { if (split[i].ToLower().Contains("user id")) { split[i] += ";Password=" + password; } if (i < (split.Length - 1)) { sb.AppendFormat("{0};", split[i]); } else { sb.Append(split[i]); } } return sb.ToString(); } 

Of course, this is not the most beautiful method, but it does its job. I pass it the connection string name and password, and it returns the updated password connection string. Here's how I implemented it:

 _entities = new UploadEntity(CreateNewConnectionString("UploadEntity", "[removed]")); _entities.AddToUploadSet(uploadFile); _entities.SaveChanges(); 
+1
source

Inside your web.config, an entry similar to this will appear:

  <connectionStrings> <add name="HelpEntities" connectionString="metadata=res://*/Models.HelpModel.HelpModel.csdl|res://*/Models.HelpModel.HelpModel.ssdl|res://*/Models.HelpModel.HelpModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=(local);Initial Catalog=XXX;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> 

The above example uses built-in security, but you can use a username and password instead.

0
source

All Articles