locked
how can i pass connection string to service library ffrom the service ? RRS feed

  • Question

  • hi...

    I have created one wcf service library project and i have write Sql Connection string in app.config file ....

    the i have referenced that service library Dll to the wcf service ....

    it's works fine on my local computer but does not work when i host service on other server....and gives me the database error becouse ther connection string is wrong...

    is there any way ...so that i pass sql connection string in WCF service 's web.config file and pass it to the fucation in the service's DLL file ...

    so the i don't have to refernce DLL each thime i need to change sql connection string ... 

    Sunday, August 5, 2012 6:56 PM

Answers

  • You can have connectionstring in your web or app config file and read in your wcf service

    <connectionStrings>
      <add 
        name="NorthwindConnectionString" 
        connectionString="Data Source=serverName;Initial 
        Catalog=Northwind;Persist Security Info=True;User 
        ID=userName;Password=password"
        providerName="System.Data.SqlClient"
      />
    </connectionStrings>

    Have a look at this MSDN post

    Tanvir Huda Application Architect/Consultant http://thetechnocrate.wordpress.com/

    • Proposed as answer by Bogdan Verbenets Monday, August 6, 2012 1:24 PM
    • Marked as answer by LeoTang Sunday, August 12, 2012 11:12 AM
    Monday, August 6, 2012 6:48 AM
  • Well you can add a new method in your service library that accepts connectionstring and provider name.

    Instead of using sql connection try using the more generic connection library in System.Data.Common namespace.

    See: http://msdn.microsoft.com/en-us/library/9tahwysy

    using System.Data.Common;
    
    public void Init(string providerName, string connectionString)
    
    {
    
    DbProviderFactory database = DbProviderFactories.GetFactory(providerName);
    
    using( DbConnection dbConn = database.CreateConnection())
    {
        dbConn.ConnectionString = connectionString;
    
    }
    
    
    } 


    Randy Aldrich Paulo

    MCTS(BizTalk 2010/2006,WCF NET4.0), MCPD | My Blog


    BizTalk Message Archiving - SQL and File
    Automating/Silent Installation of BizTalk Deployment Framework using Powershell >
    Sending IDOCs using SSIS


    • Edited by Randy Paulo Monday, August 6, 2012 8:05 AM
    • Marked as answer by LeoTang Sunday, August 12, 2012 11:12 AM
    Monday, August 6, 2012 8:01 AM

All replies

  • You can have connectionstring in your web or app config file and read in your wcf service

    <connectionStrings>
      <add 
        name="NorthwindConnectionString" 
        connectionString="Data Source=serverName;Initial 
        Catalog=Northwind;Persist Security Info=True;User 
        ID=userName;Password=password"
        providerName="System.Data.SqlClient"
      />
    </connectionStrings>

    Have a look at this MSDN post

    Tanvir Huda Application Architect/Consultant http://thetechnocrate.wordpress.com/

    • Proposed as answer by Bogdan Verbenets Monday, August 6, 2012 1:24 PM
    • Marked as answer by LeoTang Sunday, August 12, 2012 11:12 AM
    Monday, August 6, 2012 6:48 AM
  • Well you can add a new method in your service library that accepts connectionstring and provider name.

    Instead of using sql connection try using the more generic connection library in System.Data.Common namespace.

    See: http://msdn.microsoft.com/en-us/library/9tahwysy

    using System.Data.Common;
    
    public void Init(string providerName, string connectionString)
    
    {
    
    DbProviderFactory database = DbProviderFactories.GetFactory(providerName);
    
    using( DbConnection dbConn = database.CreateConnection())
    {
        dbConn.ConnectionString = connectionString;
    
    }
    
    
    } 


    Randy Aldrich Paulo

    MCTS(BizTalk 2010/2006,WCF NET4.0), MCPD | My Blog


    BizTalk Message Archiving - SQL and File
    Automating/Silent Installation of BizTalk Deployment Framework using Powershell >
    Sending IDOCs using SSIS


    • Edited by Randy Paulo Monday, August 6, 2012 8:05 AM
    • Marked as answer by LeoTang Sunday, August 12, 2012 11:12 AM
    Monday, August 6, 2012 8:01 AM