Reading Hibernate Properties from Web.config - c#

The C# project I'm working on uses nHibernate and the connection string is in the web.config as a property of a Hibernate element. I need to read the connection string in the installer to get a connection manually without using Hibernate. I know I can use configManager.connectionStrings, but as the connection string is already defined in the Hibernate portion of web.config I don't want to copy it again into the connectionStrings element. So how can I access this?

You could put the connection string in the <connectionStrings /> section of the web.config and then have NHibernate get it from there. In the NHibernate settings, remove the <connection.connection_string> property and replace it with <connection.connection_string_name> supplying the name from the <connectionStrings> section. See here for details.

<hibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="hibernate.connection.connection_string" value="${local}"/>
</hibernate>
<connectionStrings>
<add name="local" connectionString="server=(local);database=db;Uid=username;Pwd=password;"/>
</connectionStrings>
This makes it available in your ConfigurationManager, but only referenced once.

Related

Common database name Entity Framework

I use Entity Framework 6 code-first in a Windows application to connect to a SQL Server database. How could I edit my code to enable me or any app user to change the database name without getting any errors?
You can set the connection string separately in the configuration file and then use ConfigurationManager class to read strings.
But to change only the database name without other errors, you must ensure that other settings are the same. Otherwise, you must modify the corresponding code or settings according to the actual situation.
For example: add in the configuration file (config)
<connectionStrings>
<add name="MyDbContext" connectionString="Data Source=myServer;Initial Catalog=myDatabase;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Then call it like this:
string connectionString = ConfigurationManager.ConnectionStrings["MyDbContext"].ConnectionString;
For detailed examples, please refer to documentation.

Connection string updated on server when publishing

i'm trying to get my connection string values updated on production server during publish. I'm trying the following with no results:
<connectionStrings>
add name="DataConnect" connectionString="Server=Pepe;Database=Oyeti;Integrated Security=SSPI;" xdt:Transform="Replace" />
and also added the xdt:Locator, but i don't think it's the correct way:
<connectionStrings>
add name="DataConnect" connectionString="Server=Pepe;Database=Oyeti;Integrated Security=SSPI;" xdt:Transform="Replace" xdt:Locator="Match(name)" />
What i'm not seeing there?
thanks!
You can add the xdt:Transform="SetAttributes" xdt:Locator="Match(name) attributes to the connectionString tag in your transformation file (Web.Release.config) to transform the values in your primary config file (Web.config). As an alternative, you can also specify connection strings in your publish profile.
Although the default transform file contains an example that shows how
to update a connection string, in most cases you do not need to set up
connection string transformations, because you can specify connection
strings in the publish profile. You'll do that in the deploy to IIS
and deploy to production tutorials.
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/web-config-transformations

C# Web API connectionstrings web.config not being read

This is my first question here, so please be gentle (and provide constructive criticism as to how I can improve my question) :)
I'm trying to connect to an Oracle Database in my Web API project.
This project has an app.config and a web.config.
I don't know why I have both, but it came with that when setting up the (empty asp.net with Web API) project.
This is my code to connect to the database and get the connection string, which is also where I get the NullReferenceException:
// create a connection to the database
using (var con = new OracleConnection(ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString))
And I have included the appropriate using-statements:
using Oracle.ManagedDataAccess.Client;
using System.Configuration;
using System.Data;
using Oracle.ManagedDataAccess.Types;
My web.config file is as follows:
<configuration>
<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<!-- Customize these connection alias settings to connect to Oracle DB -->
<dataSource alias="ALIAS" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=IPOFSERVER)(PORT=PORTOFSERVER))(CONNECT_DATA=(SERVICE_NAME=SERVICENAME)));User Id=USERNAME;Password=PASSWORD;" />
</dataSources>
</version>
</oracle.manageddataaccess.client>
<connectionStrings>
<clear/>
<add name="Oracle" connectionString="Data Source=ALIAS;" providerName="Oracle.ManagedDataAccess.Client"/>
</connectionStrings>
</configuration>
Things I've tried so far:
Using System.Web.Configuration.WebConfigurationManager instead
Checking references etc.
Adding AppSettings key and value and try to read that from the web.config file. Also returns null.
I'm not getting any errors other than the runtime NullReferenceException.
I hope this is enough information to suggest a solution. If not, please let me know and I'll provide extra info.
Try the following. It is within System.Configuration:
var con = ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString;
Also, you may want to try without the clear/> in your config.
I've only used an app.config file when I have a console application project in my solution for testing. I store my connection strings in the web.config like so:
<connectionStrings>
<add name="NAME" connectionString="Server=Server;Initial Catalog=TABLE_NAME;User ID=ID;Password=PASSWORD;Application Name='WEBSITE'" />
</connectionStrings>
Then I reference it in my code like so:
protected const string ConnectionString = #"NAME";
Turns out it was a very simple fix, as is usually the case.
As mentioned I have an app.config AND a web.config.
In the web.config are the Oracle Client settings, so because of that and prior experience I figured the connection string should be there too.
When defining it in the app.config however, it works!
Confusing with 2 config files though, as one would expect a connection string to be defined in a web.config, not app.config.
Bottom line: ConnectionStrings should be in the app.config.
That fixed it for me.

Dynamic sql connection string

I am using my SQL connection in my mvc4 application as follows:
public static string ConnectionString=#"Data Source=LocalDB)\v11.0;AttachDbFilename=C:\Users\..\Documents\Visual Studio 2012\Projects\..\..\App_Data\RoDB.mdf;Integrated Security=True";
I want to rewrite it as dynamically.
When I change the system, I don't want to change the connection string.
If you use ".\SQLExpress" as server name it will connect to the local instance. In that case you don't need to change your connection string on different machines.
You can put connection strings in your web.config file, this means it is out of application code and doesn't require a re-build to change.
<configuration>
<!-- Other config settings -->
<connectionStrings>
<add name="localDBConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\..\Documents\Visual Studio 2012\Projects\..\..\App_Data\RoDB.mdf;Integrated Security=True" />
</connectionStrings>
</configuration>
Then to use this in your application you can put the following in compiled code:
string myConnectionString = ConfigurationManager.ConnectionStrings["localDBConnection"].ConnectionString;
Whats wrong with using a web config? its pretty much standard practice I'd assume?
Also read up on using the relative path. EG. Relative to application location
add a app configuration file in you application and add setting inside it
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source=LocalDB)\v11.0;AttachDbFilename=C:\Users\..\Documents\Visual Studio 2012\Projects\..\..\App_Data\RoDB.mdf;Integrated Security=True"/>
</appSettings>
</configuration>
in your code you can write
string ConnectionString= System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();

Reference sqlite Database in App.config

Edit Question:
Sorry about this basic question. This is a new technology to me.
I am using a c#, winforms application with sqlite.
Currently I have included sqliteDb in app_data folder, Also App.Config has the connection string:
<connectionStrings>
<clear/>
<add name ="RConnString"
providerName="System.Data.Sqlite"
connectionString="Data Source= C:\project1\R1\App_Data\RVEST_V1.DB"/>
</connectionStrings>
This RconnString is being accessed in .cs file.
string connString =
ConfigurationManager.ConnectionStrings["RConnString"].ConnectionString;
When I send it to client, How would I send the database. I mean how should I change the connection string.( Instead of referring to my local C:..)
Thank you
Sun
Try using relative path like this
connectionString="Data Source=App_Data\RVEST_V1.DB"

Categories

Resources