這是appsettings.json檔案中的連接字串。
應用設定.json
"ConnectionStrings": {
"DefaultConnection": "Server=DESKTOP-11G3852\\SQLEXPRESS;Database=MyDB;Trusted_Connection=True;MultipleActiveResultSets=true",
"OfficeConnection": "Server=DESKTOP-DTUS54A;Database=MyDB;Trusted_Connection=True;MultipleActiveResultSets=true",
"LappyConnection": "Server=DESKTOP-J8PN84H;Database=MyDB;Trusted_Connection=True;MultipleActiveResultSets=true"
}
啟動.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
.....
}
這里的問題是,每次更換機器時,我都必須在Sratup.cs檔案中手動設定連接。在這里你可以看到我設定了 "DefaultConnection"。
我想要解決方案,比如我在 appsetings.json 檔案中有多個連接字串(現在我有 3 個連接字串),它將檢查是否使用第一個連接字串(DefaultConnection)建立了連接。
如果沒有,那么它將開始與第二個連接字串(OfficeConnection)建立連接。
如果連接建立成功,那么我的網站應該繼續使用第二個連接字串(OfficeConnection)。
額外: 如果可能的話,我的遷移還需要應用成功連接的連接字串。
uj5u.com熱心網友回復:
這不是正確的做法。組態檔的存在正是出于這個原因,即表示它們在哪個環境上運行:在此處查看此答案:ASP.NET Core set hosting environment in build process
因此,正確的解決方案是根據您運行它的環境更改組態檔。
如果你真的想檢查連接,你可以嘗試這樣的事情:以編程方式測驗 SQL Server 連接的最佳方法是什么?
/// <summary>
/// Test that the server is connected
/// </summary>
/// <param name="connectionString">The connection string</param>
/// <returns>true if the connection is opened</returns>
private static bool IsServerConnected(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
return true;
}
catch (SqlException)
{
return false;
}
}
}
對于您的每個連接字串并相應地選擇。
不過說實話,您不應該在配置期間執行這些操作,因為這會花費一些時間并使您的應用程式看起來沒有回應。
uj5u.com熱心網友回復:
我通過他的解決方案得到了這個概念,然后我滿足了我的要求。
這是您只想將代碼粘貼到檔案中的解決方案
//開始
...
//結尾
阻止以下解決方案:
using System;
using System.Collections.Generic;
using System.Linq;
using MySystem.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Data.SqlClient;
using System.Configuration;
namespace MySystem
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
//START
ConnectionStrings = Configuration.GetSection("ConnectionStrings").GetChildren().ToDictionary(x => x.Key, x => x.Value);
ConnString = "";
foreach (var item in ConnectionStrings)
{
if (IsServerConnected(item.Value))
{
ConnString = item.Value;
break;
}
}
//END
}
public IConfiguration Configuration { get; }
//START
public string ConnString { get; }
public static Dictionary<string, string> ConnectionStrings { get; private set; }
//END
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//services.AddDbContext<ApplicationDbContext>(options =>
// options.UseSqlServer(
// Configuration.GetConnectionString("DefaultConnection")));
//START
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
ConnString));
//END
.......
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
......
}
//START
private static bool IsServerConnected(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
return true;
}
catch (SqlException)
{
return false;
}
finally
{
connection.Close();
}
}
}
//END
}
}
uj5u.com熱心網友回復:
您可以創建多個 appsetting.json 檔案,并在午餐設定中指定您將使用其中的哪些。在根 appsettings 中放置不會更改的部分。例如 appsettings.office.json 只有在這種情況下,您才更改連接。但是,如果您想自動更改它,我認為正確的路徑將是使用在您的每個環境中設定的環境變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/427916.html
標籤:C# sql服务器 asp.net-mvc-3 .net-core
上一篇:帶有d3.js的日期軸標簽
