So, I have a web part that needs to connect to a different database other than sharepoint. This is a custom web part that I'm writing in VS, not something built with SPD. How can I connect to the external database?

2 Answers 2


I believe Steve hit it on the head. Write your web part as you normally would using OLEDB or ODBC to make the connection to your DB and interact accordingly. Then store your password and connection string in the web.config of the SharePoint WFE(s).

You should use WebConfigurationManager to access the web.config and pull the appropriate keys out of it to use in your script. This way if your enterprise requires annual password changes (or similar) you don't need to recompile and redeploy your code every time the password changes.

Additionally, be sure to encrypt the applicable nodes of your web.config after you store your passwords there. You should never store plain text credentials in a web.config (or any configuration file for that matter). See http://msdn.microsoft.com/en-us/library/dtkwfdky.aspx for more information.

  • Am I able to use linq to sql?
    – pinniger
    Commented Jul 15, 2010 at 17:59
  • If you're making a custom web part from scratch in .NET, you should be able to do anything that works in .NET. I haven't used that method personally; we've used OLEDB to connect to Oracle and SQL.
    – webdes03
    Commented Jul 15, 2010 at 18:36
  • To use linq you need to make sure you modify the web.config to allow the .net 3.5 extensions as well as make sure your WFE's have .net 3.5 installed. Commented Jul 15, 2010 at 21:27

Just write it how you would in a normal ASP.NET webpart or application. You can store the connection string in the sharepoint web.config.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.