.NET 6中的多个连接字符串,而无需使用实体框架

发布于 2025-02-06 16:32:29 字数 2329 浏览 1 评论 0原文

我是.NET核心的新手,如果我的问题是如此基本的话,请原谅我。 我总是为我的所有项目使用.NET框架,但是现在我必须移至.NET Core 6。 在.NET框架中,我在Web.config中设置了我的连接字符串:

<add key="ConnStr" value="Data Source=DESKTOP-854AIA7;Initial Catalog=TestOTA;User ID=sa;Password=1"/>
    <add key="ConnStrReport" value="Data Source=192.168.5.226;Initial Catalog=cachrsp01;User ID=sa;Password=123@123"/>
    <add key="ConnStrLog" value="Data Source=192.168.5.226;Initial Catalog=loghrsp01;User ID=sa;Password=123@123"/>

然后,我有一个类似这样管理的类:

public class AccessToDb{
     public static string ConnectionString
    {
        get
        {
            string tmp = System.Configuration.ConfigurationManager.AppSettings["ConnStr"];
            if (string.IsNullOrEmpty(tmp))
            {
                do something
                
            }
            return tmp;
        }
    }
    public static string ConnectionStringLog
    {
        get
        {
            string tmp = System.Configuration.ConfigurationManager.AppSettings["ConnStrLog"];
            if (string.IsNullOrEmpty(tmp))
            {
                do something
            }
            return tmp;
        }
    }
}

在需要调用的其他类中,在其他类中,取决于我需要哪一个,我称其为:

 objSqlCommand.Connection = new SqlConnection(AccessToDb.ConnectionString);

 objSqlCommand.Connection = new SqlConnection(AccessToDb.ConnectionStringLog);

但是我不知道在哪里设置连接字符串以及在哪里拨打它们以及如何在方法中使用它。我搜索,但是我发现的所有结果都使用了实体框架。


更新

我检查了一下,最后找到了解决方案。我将其添加到appsettings.json:

{
    "ConnectionStrings": {
        "mymainDB": "server=192.168.5.226;database=cachrsp01;user id=sa;password='123@123'",
        "myLogDB": "server=192.168.5.226;database=loghrsp01;user id=sa;password='123@123'"
    }
}

然后我创建了类class accessTODB:

public class AccessToDb
{
    public static string ConnectionString { get; set; }
    public static string ConnectionStringLog { get; set; }
}

最后在program.cs中我配置它们:

AccessToDb.ConnectionString = builder.Configuration.GetConnectionString("mymainDB");
AccessToDb.ConnectionStringLog = builder.Configuration.GetConnectionString("myLogDB");

现在一切对我来说都很好。

I am new to .NET Core, so forgive me if my question is so basic.
I always use .NET Framework for my all project, but now I have to move to .NET Core 6.
In .NET Framework, I set my connection string in Web.config like this:

<add key="ConnStr" value="Data Source=DESKTOP-854AIA7;Initial Catalog=TestOTA;User ID=sa;Password=1"/>
    <add key="ConnStrReport" value="Data Source=192.168.5.226;Initial Catalog=cachrsp01;User ID=sa;Password=123@123"/>
    <add key="ConnStrLog" value="Data Source=192.168.5.226;Initial Catalog=loghrsp01;User ID=sa;Password=123@123"/>

then I have a class to manage them like this :

public class AccessToDb{
     public static string ConnectionString
    {
        get
        {
            string tmp = System.Configuration.ConfigurationManager.AppSettings["ConnStr"];
            if (string.IsNullOrEmpty(tmp))
            {
                do something
                
            }
            return tmp;
        }
    }
    public static string ConnectionStringLog
    {
        get
        {
            string tmp = System.Configuration.ConfigurationManager.AppSettings["ConnStrLog"];
            if (string.IsNullOrEmpty(tmp))
            {
                do something
            }
            return tmp;
        }
    }
}

In other classes when I need to call them, depends on which one I need I call them like:

 objSqlCommand.Connection = new SqlConnection(AccessToDb.ConnectionString);

or

 objSqlCommand.Connection = new SqlConnection(AccessToDb.ConnectionStringLog);

But I don't know where to set my Connection string and where to call them and how to use it in my methods. I search but all results I found, used entity framework.


UPDATE

I checked and finally find a way around it. I add this in appsettings.json :

{
    "ConnectionStrings": {
        "mymainDB": "server=192.168.5.226;database=cachrsp01;user id=sa;password='123@123'",
        "myLogDB": "server=192.168.5.226;database=loghrsp01;user id=sa;password='123@123'"
    }
}

then I created Class AccessToDb like this:

public class AccessToDb
{
    public static string ConnectionString { get; set; }
    public static string ConnectionStringLog { get; set; }
}

Finally in Program.cs I configure them:

AccessToDb.ConnectionString = builder.Configuration.GetConnectionString("mymainDB");
AccessToDb.ConnectionStringLog = builder.Configuration.GetConnectionString("myLogDB");

everything now works fine for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文