.NET 6中的多个连接字符串,而无需使用实体框架
我是.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论