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'm sorry if this has been asked, but I cannot find an answer. Can you query a SharePoint Document Library or site in a C# program using SQL?

If so, how do you set up the connection to SharePoint with C#?

share|improve this question
up vote 8 down vote accepted

In case you are planning to query a SharePoint database, please don't do that! It's not supported and wrong in many ways.

You should use SharePoint APIs to retrieve data you need. You can retrieve items from a SharePoint list via SPList object (not optimal for large list), or if you plan to query a list you should use CAML.

share|improve this answer
All the answers were helpful, but the links in your's really helped, thanks all. – sange Jan 6 '11 at 13:21

You can use FullTextSQLQuery to issue SQL-like queries against the search index.

For simply returning a list of items, the standard is to use CAML queries, or LINQ to SharePoint is quickly becoming a popular method of doing this now.

Never attempt to directly query the underlying SQL content database, as this is not a supported practice.

share|improve this answer

Just to add my $2 on why you shouldn't connect directly to the SQL DB (and should use the SharePoint API as others have said), I blogged about this here: Supported Database Operations in SharePoint 2010.

share|improve this answer
much more deserved +1 – Falak Mahmood Jan 5 '11 at 23:34

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.