Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
Am I able to use linq to sql? –  pinniger Jul 15 '10 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 Jul 15 '10 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. –  Steve Lineberry Jul 15 '10 at 21:27
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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