0

I want to know about connection string without database name is working or not, i have built a web api in visual studio, but it is built without entity framework. In the web api all the code is same, but database are different, those are exist databases in sql server management studio and those database are from different computer and i want to try and call these api from cloud server(actually there are only 5 databases in 3 different computer). All 5 databases have different name.

What can i do to the connection string for these databases, and i don't want to change the database name in the sql server. Is there any possible way to code the connection string in my expected way?

<add name="MyConnection" connectionString="Server=.;Database=(different dbname);User Id=owner; Password=1234" providerName="System.Data.SqlClient"/>

dot can equal to all location in the connection string as i know.

1 Answers1

1

You can work with as many databases as you want at the same time.

  <connectionStrings>
      <add name="db1" connectionString="Data Source=.;Initial Catalog=systemdb1; User ID=sa;Password=sqlsa;MultipleActiveResultSets=true;"
          providerName="System.Data.SqlClient"/>
      <add name="db2" connectionString="Data Source=.;Initial Catalog=systemdb1; User ID=sa;Password=sqlsa;MultipleActiveResultSets=true;"
          providerName="System.Data.SqlClient"/>
      <add name="arsiv" connectionString="Data Source=.;Initial Catalog=systemdb1; User ID=sa;Password=sqlsa;MultipleActiveResultSets=true;"
          providerName="System.Data.SqlClient"/>
      <add name="db3" connectionString="Data Source=.;Initial Catalog=db1; User ID=sa;Password=sqlsa;MultipleActiveResultSets=true;"
          providerName="System.Data.SqlClient"/>
      
  </connectionStrings>

It may take your time to do this. You can easily find resources that you can use as examples.

-Check this link for example Connect multiple Databases to .NET core project via Entity Framework Core

Mcan_Cicek
  • 234
  • 2
  • 7
  • I think you have to read the title carefully, what i ask was one connection string not a couple. If i add more databases, it isn't need to do more connection string, this will be diffcult everytime when i add new database, thats why i want to code it in one connection string only, try to ask is it possible to code it, thank you – user19613128 Aug 15 '22 at 08:17
  • I think you can edit connection string before the context is created but i never try this. – Mcan_Cicek Aug 15 '22 at 08:37