SQL script to search all tables for a particular column name
By peter.stilgoe
Run the below script against a database to return all tables where a particular column name exists
use my_db SELECT t.name AS table_name, SCHEMA_NAME(schema_id) AS schema_name, c.name AS column_name FROM sys.tables AS t INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID WHERE c.name LIKE '<columnname>' ORDER BY schema_name, table_name;
How to create an Excel Workbook from an SQL database using Open XML SDK 2.0
By peter.stilgoe
Below is the C# code to create a new Excel workbook based on a template file & populated by an SQL database.
You need to add references to:
DocumentFormat.OpenXml
System.Data.Linq
Windows.Base
using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;
namespace ElitetoExcel
{
class Program
{
static string[] headerColumns =
new string[] { "A", "B", "C", "D", "E"};
static void Main(string[] args)
{
//Make a copy of the template file
File.Copy(@"c:\templatedoc.xlsx", @"c:\newdoc.xlsx", true);
//Open up the copied workbook
using (SpreadsheetDocument myWorkbook =
SpreadsheetDocument.Open(@"c:\newdoc.xlsx", true))
{
//Access the main workbook part which contains all the references
WorkbookPart workbookPart = myWorkbook.WorkbookPart;
//Grab the first worksheet
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
//Sheet data will contain all the data
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
//Connect to Database LINQ
DataClasses1DataContext
DataClasses1DataContext = new
DataClasses1DataContext();
int index = 2;
//select * from SPOpenMatters table
var MatterQuery = from t in DataClasses1DataContext.SPOpenMatters
select t;
//For each record in database add row in spreadsheet
Console.WriteLine("Creating Excel report...........");
foreach (var item in MatterQuery)
{
string MatterNum = item.matterno;
string Description = item.description;
string OpenedDate = item.openeddate.ToString();
string MatterStatus = item.matterstatus;
string BillingTKPR = item.billingTKPR;
//Add a new row
Row contentRow =
CreateContentRow(index, MatterNum, Description, OpenedDate, MatterStatus, BillingTKPR);
index++;
//Append new row to sheet data
sheetData.AppendChild(contentRow);
}
worksheetPart.Worksheet.Save();
}
}
private static Row CreateContentRow(int index, string MatterNum, string Description, string OpenedDate, string MatterStatus, string BillingTKPR)
{
//Create new row
Row r = new Row();
r.RowIndex = (UInt32)index;
//First cell is a text cell, so create it and append it
Cell firstCell =
CreateTextCell(headerColumns[0], MatterNum, index);
r.AppendChild(firstCell);//
//create cells that contain data
for (int i = 1; i < headerColumns.Length; i++)
{
Cell c = new Cell();
c.CellReference = headerColumns[i] + index;
CellValue v = new CellValue();
if (i == 1)
v.Text = Description.ToString();
if (i == 2)
v.Text = Convert.ToDateTime(OpenedDate).ToString("D");
if (i == 3)
v.Text = MatterStatus.ToString();
if (i == 4)
v.Text = BillingTKPR.ToString();
c.AppendChild(v);
r.AppendChild(c);
}
return r;
}
private static Cell CreateTextCell(string header,
string MatterNum, int index)
{
//Create a new inline string cell
Cell c = new Cell();
c.DataType = CellValues.InlineString;
c.CellReference = header + index;
//Add text to text cell
InlineString inlineString = new InlineString();
Text t = new Text();
t.Text = MatterNum;
inlineString.AppendChild(t);
c.AppendChild(inlineString);
return c;
}
}
}
Sharepoint 2010: Database doesnt exist but sill appears in Central Admin –> Manage Databases Upgrade Status
By peter.stilgoe
If you have deleted your database in SQL but they sill appear in Central Admin –> Manage Databases Upgrade Status, you can use the following Powershell script to remove them:
$db = Get-SPDatabase | where{$_.Name -eq "YourDatabaseName"}
$db.Delete()
Populate a Sharepoint list from an SQL Database
By peter.stilgoe
If you are wanting to populate a Sharepoint list from an SQL database or populate a SQL database from a Sharepoint list you can do this failry easily using SSIS & the SharePoint List Source and Destination Adapter project on Codeplex.
Introduction
This sample illustrates the creation of a a source and destination adapter in C#, using the public APIs for Microsoft SQL Server Integration Services. The component connects to SharePoint through a proxy dll, which wraps the complexities of working with the SharePoint Lists Webservice.
The Specific Features of the included SSIS Components are:
SharePoint Source Component
- Auto-detects all of the public fields from a SharePoint List or view of the list.
- Supports removal of fields from the output flow, which optimizes the query against SharePoint for increased performance.
- Supports restricting SharePoint data by a custom CAML query, which can be provided through a variable expression for fine tuned filtering over large lists.
- Provides a tuning feature for the # of rows to return at a time. Often with SharePoint, large lists need to be brought over in smaller batches than wide (many field) lists, or else the Webservice will die. The batchsize lets you specify a solid size that works for your list.
SharePoint Destination Component
- Auto-detects all of the editable fields from a SharePoint List
- Only fields bound to columns from the input flow will be ‘updated’, for performance.
- Supports deleting rows by ID
Included Bonus – SharePoint Utility
To simplify the adapter code and keep the SharePoint routines generic, a proxy VB.Net component is included which access the SharePoint Webservice API. It includes a very flexible api for getting SharePoint List information, which is used by the Components above. In addition, it can publish and remove files from a document library.
Component Features
Unique features that may inspire you to create great custom SSIS components:
• Extensive Validation: Validation for this component actively goes against the SharePoint Site to verify the properties are valid.
• Values from Expressions: This component supports external variables, which can be expressions, and can be attached to the source component to customize the query. Similar to the CommandText for the other Sql Components
• Linq: The Component has been written using Linq with .net 3.5 and shows how elements such as the metadata and columns can be combined to create a readable usage in a Linq format.
• Custom properties: The component keeps its configuration in custom properties on itself, inputs, and input columns.
• SharePointUtility Component: Simplifies access to the SharePoint Lists / Views WS using Linq friendly objects
Sharepoint List Source & Destination project on Codeplex
Error (Exception from HRESULT: 0×80004004 (E_ABORT))
By peter.stilgoe
This error usually occurs in Sharepoint when the transaction log is full or no drive space available to it on the SQL server in your farm.
You will usually get the error when trying to create new content etc.
More From pstilgoe
K2 deployment error: The partner transaction manager has disabled its support for remote/network transactions
By peter.stilgoe
When deploying a new K2 workflow / process you get an error message containing:
The partner transaction manager has disabled its support for remote/network transactions
First check on the application server(s):
1. Go to “Administrative Tools > Services”
2. Turn on the “Distribute Transaction Coordinator” Service if it is not running
If you still get the error:
First verify the “Distribute Transaction Coordinator” Service is
running on both database server and application servers
1. Go to “Administrative Tools > Services”
2. Turn on the “Distribute Transaction Coordinator” Service if it is not running
If it is running and its not running on the same server as the SQL Server:
1. Go to “Administrative Tools > Component Services”
2. On the left navigation tree, go to “Component Services > Computers
> My Computer” (you may need to double click and wait as some nodes
need time to expand)
3. Right click on “My Computer”, select “Properties”
4. Select “MSDTC” tab
5. Click “Security Configuration”
6. Make sure you check “Network DTC Access”, “Allow Remote Client”,
“Allow Inbound/Outbound”, “Enable TIP” (Some option may not be
necessary, have a try to get your configuration)
7. The service will restart
8. YOU MAY NEED TO REBOOT YOUR SERVER IF IT STILL DOESN’T WORK
On the application server(s) use the same above procedure to open the
“Security Configuration” setting, make sure you check “Network DTC
Access”, “Allow Inbound/Outbound” option, restart service and computer
if necessary.
On you SQL server service manager, click “Service” dropdown, select
“Distribute Transaction Coordinator”, it should be also running on
your server computer
More From pstilgoe
Save to sql server failed no description found
By peter.stilgoe
When trying to create an SISS package in sql server managament studio using the export wizard you get the following error message at the last stage:
“Save to sql server failed no description found”
To fix this you need to reregister the following DDL’s
Regsvr32 msxml3.dll
Regsvr32 msxml6.dll
Now when you try & save you SISS package it should go through with no problems.



January 30th, 2012
