How search a string in all the tables of a database

If you receive a database in witch you need to search for some info, and you know the info is there, but you don't know where, probabily adding a search stored procedure could help you.

I've found a good stored procedure in the article How to search all columns of all tables in a database for a keyword?: it's written for SQL Server 7 and 2000, but I've used in SQL Server 2008 without any problem.

In the file SearchAllTables.sql.txt you can also find a backup copy of this important script!

C# 4 collection interfaces

In C# we have the following interfaces for collections and lists:

  • IEnumerable<T>: supports the foreach statement via the GetEnumerator() method;
  • ICollection<T>: supports the Count property and the Add(), Remove(), Clear() and CopyTo() methods;
  • IList<T>: derives from ICollection<T> and adds the Insert() and RemoveAt() methods; also it offers the following methods:
    • public void ForEach(Action<T> action);
    • public int FindIndex(Predicate<T> match);
    • public T Find(Predicate<T> match);
    • public List<T> FindAll (Predicate<T> match);
    • public List<T> Sort(Comparison<T> comparer);
    • public List<T> Sort(IComparer<T> comparer);
    • public List<TOutput> ConvertAll(Converter<T, TOutput> converter);
  • ISet<T>: derives from ICollection<T> and adds union and intersection of sets;
  • IDictionary<TKey, TValue>: collection of values indexed by a key;
  • ILookup<TKey, TValue>: like IDictionary, but allows multiple values for the same key;
  • IComparer<T>: used to sort elements inside a collection via the Compare() method;
  • IEqualityComparer<T>: compare values;
  • IProducerConsumerCollection<T>: support thread-safe classes.

How enable the filestream feature in SQL Server 2008

SQL Server 2008 introduces the concept of filestream, but by default it is disabled.

To enable it you have to do the following things:

  1. enable the feature from SQL Server Configuration manager (SQL Server Services, SQL Server, Properties, FileStream tab);
  2. then activate on the database server, via SSMS (Database server, properties) or via T-SQL:
         EXEC sp_configure filestream_access_level, 2

More details in the article How to: Enable FILESTREAM.

SharePoint 2010: extending existing web-parts

In SharePoint 2010, if you need to extend the existing web-parts, you need to create, from Visual Studio 2010, an empty SharePoint project, then add a web-part.

After buid and deploy, you could get an error saying that it's impossible to launch the new web-part.

In this case, the problem is that you need to delete the .webpart file from your project, and add a new .dwp file, witch is essentially the same as the .webpart, but in another xml schema.

You can read more details in the article Extend the Search Box web part in SharePoint 2010.

SharePoint 2010 development: how reference a dll in your wsp

In Visual Studio 2010, you don't need to use WSPBuilder anymore, because there are native SharePoint 2010 project templates.

But how can you add a class library to your solution, and have it referenced and above all included in your wsp package?

Open the Package Designer, go to the Advanced tab, Add, an Assembly from Project Output and select your utility project.

More details in the article How to: Add and Remove Additional Assemblies.

SPQuery issues

If your SPQuery is returning all elements of a list, and not only the filtered ones, probably your Query contains the <Query> begin and end tags: remove them; more details in the post SPQuery returns all items.

Another issue with SPQuery: the SPQuery object is not reusable for multiple queries, so after you use it for a query, if you need to change the Query property, you need to create another SPQuery instance.

C#: delegates, anonymous methods, lambda expressions and events

Delegates = typed function pointere signatures:

delegate void MyMethodDelegate(int myParam)
MyMethodDelegate myMethod = new MyMethodDelegate(x.ToString()
MyMethodDelegate myMethod = x.ToString()

There are two generic shortcuts to delegate declaration:

  • Action: a method that takes up to 16 parameters and returns void
  • Func: a method that takes up to 16 parameters and return the last type

Anonymous methods allow instancing delegates faster:

Func anonDel = delegate(string param) {... return y;}

Lambda expressions simplify this even more:

Func lambdaExpr = param => {... return y;}
Func lambdaExpr = (double x, double y) => ...
Func lambdaExpr = x, y => xxx

Events allow subscribe/unsubscribe to/from delegates:

public event EventHandler myEvent;

SharePoint 2007: how reproduce the Edit With command

Writing your own web parts, it may be possibile to reproduce the Edit with Word/Excel/... command; in fact, this command check-outs the document, then allows it to edit/read it from the office application.

The trick is to use the editDocumentWithProgID2 javascript function; here is an example call:

editDocumentWithProgID2('http://mywebserver/mysiteurl/myDoc.doc', '', 'SharePoint.OpenDocuments', '0', 'http://mywebserver/mysiteurl', 0);

You can read the following very detailed and intresting post: How to open SharePoint documents in Edit mode with Internet Explorer 6 and Windows XP (without regfix).

A few notes:

  • calling this command forces the refresh of the page. If you want to avoid it, you can set the g_varSkipRefreshOnFocus javascript function to true, as described in this question page: editDocumentWithProgID2 issue;
  • if you handle non Office documents (for example: pdf), you can't call this method, and you have to revert to the link allowing the document to open.