Page 1 of 1

Connection strings

Posted: Fri Jun 02, 2017 7:52 pm
by aspmaker_fan

Hi

I think the connection strings are stored in the compiled exe file. How do we change the connection strings after combiled and published? Or is there any way we could dynamically change the connection strings for multi tenant and multi user system? (v2017).

Thanks
Zam


Re: Connection strings

Posted: Fri Jun 02, 2017 9:41 pm
by Webmaster

Use Database_Connecting server event, see Server Events and Client Scripts in the help file.


Re: Connection strings

Posted: Sat Jun 03, 2017 12:45 am
by aspmaker_fan

Hi,

Many thanks. The script below works. How we can use different connection strings based on logging in user? I meant dynamically set the connection string? or is there any way of doing this such as using session ids?

// Database Connecting event
public virtual void Database_Connecting(ref string Connstr) {

        System.Configuration.Configuration rootWebConfig =
        System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
        System.Configuration.ConnectionStringSettings connString;
        if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
        {
            connString =
                rootWebConfig.ConnectionStrings.ConnectionStrings["Northwind"];
            if (connString != null)
                ew_Response.Write(connString.ConnectionString);
            else
                ew_Response.Write("No Northwind connection string");

            Connstr = connString.ConnectionString;

        }