简体   繁体   中英

Can't read database name web project

Trying to read the database name but it's not working in the site. Using an asp.net web project with a solution file. I put this in the web.config. This will change depending on development database or production database PRODDB via the web configuration transform files and scripts.

    <appSettings>
        <add key="databaseName" value="DEVELDB"/>
        <add key="operatingEnvironment" value="dev"/>
    </appSettings>

Within the Global.asax.cs file:

  void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
           
            var operatingEnvironment = WebConfigurationManager.AppSettings["operatingEnvironment"].ToString();

            if (operatingEnvironment == "dev")
            {
                Application["DBE"] = WebConfigurationManager.AppSettings["databaseName"].ToString();
            }
        }

Within the login.cs class file:

 internal static DatabaseConnection GetDatabaseConnection()
            {

                return

                    new DatabaseConnection(
                        new ConnectionProperties
                        {
                            User = Shared.User,
                            Application = Shared.Application,
                            DatabaseName = (string)HttpContext.Current.Application["DBE"]
                        });
            }

and within the Web.Development.config file there is:

    <appSettings>
        <add key="databaseName" value="DEVELDB" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
        <add key="operatingEnvironment" value="dev" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    </appSettings>

However, whenever I debug after login, it goes to the code below which is good (the same as coded above) but says Database name was not specified? I have it specified as DEVELDB and using "DBE" to reference it. However, it's not like it debugs to another area and to there, just straight to there and kills it?

...
  {
                            User = Shared.User,
                            Application = Shared.Application,
                            DatabaseName = (string)HttpContext.Current.Application["DBE"]
                        });

Connections to databases go inside the "connectionstrings" tags in you "webconfig" file. If you need SqlServer it is shown below

 <configuration>
  <connectionStrings>
    <add name="TwoWayConn" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\TwoWayData.accdb"
      providerName="System.Data.OleDb" />
    <add name="TWOWAYDATASQL" connectionString="Data Source=TBIRD\SQLEXPRESS1;Initial Catalog=TWOWAYDATASQL.MDF;Integrated Security=True"
  providerName="System.Data.SqlClient" />
 </connectionStrings>
....

    </configuration>

Am I misunderstanding what you are trying to do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM