none
DataContext class - how to define connection string RRS feed

  • Question

  • I've got a class that I'm using as a DataContext for my table classes. I don't want to pass the connection string into the DataContext constructor each time I create an instance of it. So I'm using this:

     

    using System.Data.Linq;
    
    public class DB : DataContext
    {
        public DB(string connection = "Data Source=DEBUG\\SQL2008;Initial Catalog=xx_dev;Integrated Security=True") : base(connection) { }
    
        public Table<Report> Reports;
        public Table<RoleReport> RoleReports;
    
    }
    

    Works great, but elsewhere in my app I'm using the same connection string read from web.config. My question is, how can I modify the code above to read from the web.config?

    Thursday, October 20, 2011 8:26 AM

Answers

  • Hi,

     web.config
    <connectionStrings>
       <add name="ConnectionString" connectionString="Server=xxx; Port=3306; Database=xxx; uid=xxx; pwd=xxx;" />
    </connectionStrings>
    c#
    System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString

    you can put the connection string to the constructor.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 24, 2011 12:08 PM
    Moderator
  • Hi

    Thanks for your reply... which takes me back to the original post - I don't want to pass the connection string into the DataContext constructor each time I create an instance of it But I guess the answer is - I have to? It's a minor point but every time I create a new instance of DB, I will always use the same connection string so it just seems a little redundant.

    Regards

     


    Tuesday, October 25, 2011 9:07 AM

All replies

  • Hi,

     web.config
    <connectionStrings>
       <add name="ConnectionString" connectionString="Server=xxx; Port=3306; Database=xxx; uid=xxx; pwd=xxx;" />
    </connectionStrings>
    c#
    System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString

    you can put the connection string to the constructor.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 24, 2011 12:08 PM
    Moderator
  • Hi

    Thanks for your reply. My original post was badly expressed because I have tried something similar already.

    The issue is that I can't use the following code because a default value in a constructor has to be a literal at compile-time.

    public class DB : DataContext
    {
        public DB(string connection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString) : base(connection) { }
    
    

    Tuesday, October 25, 2011 8:00 AM
  • Hi,

    no, no!

    the class should like this:

    public class DB : DataContext
    {
        public DB(string connection ) : base(connection) { }
    //DbSet<>;
    }
    
    


    When you new a instance of DB, you pass in the connection string:

    string con=System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    using(var db= new Db(con))

    {

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, October 25, 2011 8:30 AM
    Moderator
  • Hi

    Thanks for your reply... which takes me back to the original post - I don't want to pass the connection string into the DataContext constructor each time I create an instance of it But I guess the answer is - I have to? It's a minor point but every time I create a new instance of DB, I will always use the same connection string so it just seems a little redundant.

    Regards

     


    Tuesday, October 25, 2011 9:07 AM
  • Hi,

    I got your point, you can try like this:

     

    public class UnicornsContext : DbContext
    {
        public UnicornsContext()
            : base("name=UnicornsCEDatabase")
        {
        }
    }
    

     

    You may choose to put a connection string in your app.config or web.config file. For example:

    configuration>
      <connectionStrings>
        <add name="UnicornsCEDatabase"
             providerName="System.Data.SqlServerCe.4.0"
             connectionString="Data Source=Unicorns.sdf"/>
      </connectionStrings>
    </configuration>
    

    You can refer this link: http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, October 25, 2011 9:11 AM
    Moderator
  • Hi,

    Sorry for my careless, I gave the information about Entity Framework. You should retrieve the connection frist from Web.config(Like my first post), thanks for understanding.

    have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, November 1, 2011 8:44 AM
    Moderator