How do I set a default connection string in a console application?

connection string in app.config c# console application
database connection in c# console application
app.config connection string with username and password
encrypt and decrypt connection string in app.config file c#
connection string in c#
get connection string
c# connection string from appsettings
connection string in windows service c#

In my MVC projects with a web interface, I'm used to setting the Connection String in the Web.Config file.

However, now I'm making a bog standard console application - also with a database hook, but how do I set the connection string globally for the application?

At the moment, I am setting

var dbIndex = new DBContext();
dbIndex.Database.Connection.ConnectionString = 
    "Data Source=USER-PC;Initial Catalog=TextProject.DBContext;" + 
    "Integrated Security=True;MultipleActiveResultSets=True";

but I have to set this connectionstring property every time, in all function calls. Is there a way to set a global connection string when I don't have a web.config?


So I think what your saying is that Entity Framework (I assume that is what you are using) looks for defaultConnection connection string.

You can try putting it in the app.config file as suggested by others, but I'm not sure if this will be automagically picked up by EF.

What you could do, if it doesn't work, is to create new class which inherits DbContext -

public class MyDbContext : DbContext
{
    public MyDbContext() : base()
    {
        var cs = ConfigurationManager.ConnectionStrings["defaultConnection"]
                                     .ConnectionString;

        this.Database.Connection.ConnectionString = cs;                
    }
}

How To Create Single Connection String in Console Application, Then in the program, access (use) the connection string using: Properties.Setting.​Default.(name) where (name) is the name you provided in the  The solution is very simple and easy. Just right click and set the project as start-up project. entity framework by default picks the start-up project and uses the connection string inside of App.config or web.config . Please refer to this:


Connection Strings and Configuration Files, to. This parameter is often named "Server" or "Data Source". In my MVC projects with a web interface, I'm used to setting the Connection String in the Web.Config file. However, now I'm making a bog standard console application - also with a database hook, but how do I set the connection string globally for the application? At the moment, I am setting


As others have said put your connection string in the App.config file.

Regarding Entity Framework. If you are using Entity Framework (EF) version 6.2.0 perhaps earlier EF will automatically look for a connection with the same name as the class that inherits from DbContext.

If your class is

public class MyDatabaseContext : DbContext 
{
    // other code
}

EF will look for a connection string with a name like this <add name="MyDatabaseContext" ...

You can also set the name in the constructor like so

public class MyDatabaseContext : DbContext 
{
  public MyDatabaseContext() : base ("name=defaultConnection")
  {
    // other code
  }

  // other code
}

In this case your connection string name can be <add name="defaultConnection" ...

Connection Strings Explained, is in your hands you can use it anywhere you want. Click on the ellipsis and create the connection string. Then in the program, access (use) the connection string using: Properties.Setting.Default.(name) where (name) is the name you provided in the name column.


You can put the same element in a App.config file for the console application and access it the same way you would a Web.config. Related: What is App.config in C#.NET? How to use it?

Get SQL Server Database Connection String Easily from Visual , What is initial catalog in connection string in web config? To access the connection string the first step would be to make sure that a reference is added to System.Configuration. Then make sure you include it in the class using System.Configuration and the code below is used to access the connection string.To access the connection string


The solution is very simple and easy. Just right click and set the project as start-up project. entity framework by default picks the start-up project and uses the connection string inside of App.config or web.config

Please refer to this:

SQL Server Connection String for SQL Server Authentication in Web , – The name of the Database. User Id – The User Id of the SQL Server. WinForms, WPF, and ASP.NET 4 applications have a tried and tested connection string pattern. The connection string should be added to your application's App.config file (Web.config if you are using ASP.NET). If your connection string contains sensitive information, such as username and password, you can protect the contents of the configuration file using the Secret Manager tool.


How do I set a default connection string in a console application?, config for console or .exe programs. Add a reference to System.Configuration on any project that you want to use the ConnectionStrings of the app.config. Add a AppConfig to your entry point project. Add the connection string in the <connectionStrings></connectionStrings> section of the app.config. When retrieving connection strings by provider name from the app.config file in a Windows application, the connection strings in machine.config get loaded first, and then the entries from app.config. Adding clear immediately after the connectionStrings element removes all inherited references from the data structure in memory, so that only the connection strings defined in the local app.config file are considered.


Console C# get database connection string from app.config, Embedding connection strings in your application's code can lead to security vulnerabilities and create a separate file that contains only the connectionStrings section. ConnectionString properties in the console window. I've been trying to make a connection with my SQL Server database (Mynewdatabase) using the default connection string that was generated (supposedly) when I added the data source for this project.


How to Use ConnectionString from App config file in Visual Studio C#, How to get the connection string from the app.config file in a C# console It keeps these bits of data in one place, making it easy to manage when things change. <configuration> <connectionstring> <add key="MySQL. The most common types are String, Integer, and Boolean, but you can also store values as Color, Object, or as a connection string. Application settings also hold a value. The value is set with the Value property and must match the data type of the setting. In addition, application settings can be bound to a property of a form or control at design time.