How do I create connection string programmatically to MS SQL in Entity Framework 6?

后端 未结 5 1698
一整个雨季
一整个雨季 2021-02-14 02:55

How do I create connection string programmatically to MS SQL in Entity Framework 6?

I\'m using c# and WPF and I was wondering if someone could show me how or link me to

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-14 03:16

    I investigated this question today. in my opinion the easiest solution is not mentioned above.

    Why not use the SqlConnectionStringBuilder class? (using System.Data.SqlClient)

    Here a simple example how to use it.

        SqlConnectionStringBuilder sqlb = new  SqlConnectionStringBuilder(getConnectionString(DATABASENAME);
    
        using (SqlConnection connection = new SqlConnection(sqlb.ConnectionString))
        ...
        )
    
        // works for EF Core, should also work for EF6 (haven't tried this) 
        private static string getConnectionString(string databaseName)
        {
            return "Data Source=SQLSERVERNAME;Initial Catalog="+databaseName+";Integrated Security=True";
        }
    

提交回复
热议问题