SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a problem with accessing the SQL Server database from an application page in SharePoint 2010. With using the SqlConnection and SqlCommand an Adapter I keep getting an error

Cannot find table [0]

But the strange thing is that the exact same code (same connection string and everything) works when it's in a console application and run on that server.

What could be the problem here?

Thanks in advance

share|improve this question
Is this a custom database you're trying to access? Is the server remote or local to the code running? – James Love Oct 3 '11 at 10:06
You don't state what credentials you're using to connect – SHug Oct 4 '11 at 12:13

Could be authentication, ensure your SP service account has correct permissions, try connecting to the SQL instance as the service account.

share|improve this answer

This is all caused by the "Code Access Security" changes made by SharePoint that disable the SQLConnection.

You have to use SPSecurity.RunWithElevatedPrivileges

share|improve this answer

This is probably related to User Account Control (UAC). Try running IE as an administrator by right clicking and choosing "Run as Administrator." I know a bunch of people will give me hell for this, but it's the worst thing Microsoft has ever implemented... wait.... within the context of server applications. Write back and let us know if that's what was biting you in the rump.

share|improve this answer

Your Answer


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.