Connect to SQLServer and read connectionStrings in .NET 6.0 ?

0
0

Hi Team,

I need to connect to SQLServer and access the connection with C#.
In framework 4.8 I do it through the Web.Config file and access:
<connectionStrings>DefaultConnection … </connectionStrings>
And In c# I use
string cnString = ConfigurationManager.ConnectionStrings[“DefaultConnection”].ConnectionString;

How can I do it same in .Net6.0 ?

 

  • You must to post comments
0
0

Hi Paul,

in my Wisej .Net 6.0 Project I put the connection strings in Default.json like this:

{
...
},
"settings": {
"dbConnectionLiveString": "Server=xxx;Database=xxx;user id=xxx;password=xxx;",
"dbConnectionTestString": "Server=xxx;Database=yyy;user id=xxx;password=xxx;"
}
}

Without further ado you can access the strings via Wisej.Web,Application class:

var dbConnectionString = Application.Configuration.Settings.dbConnectionLiveString;

Cheers, Gerhard

  • Em Dev
    I’ve attempted to do this in my .net4.8 web page application and I get an invalid configuration entries: dbConnectionString application error on running my project. Is there a different method for .net 4.8 projects?
  • Frank (ITG)
    You can use the web.config or the approach described above. If it does not work, maybe post a sample of the code here? Best, Frank
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.