Example 1
Returns list with names of all available instances of SQL Server within the local network
private List<string> GetServerNames()
{
return SqlDataSourceEnumerator.Instance.GetDataSources().Rows.
Cast<DataRow>().
Select
(
row => row["ServerName"].ToString() +
(row["InstanceName"] != DBNull.Value ? "\\" + row["InstanceName"].ToString() : "") +
(row["Version"] != DBNull.Value ? " (" + row["Version"].ToString().Substring(0, 3) + ")" : "")
).
OrderBy(s => s).
ToList();
}
Example 2
Generates not used name for new file
private string NewName(string newNamePrefix, List<string> existingFileNames)
{
return newNamePrefix +
(existingFileNames.
Select
(
n =>
{
if (n.StartsWith(newNamePrefix))
{
int i;
if (int.TryParse(n.Replace(newNamePrefix, ""), out i))
return i;
}
return 0;
}
).
OrderBy(i => i).
Last() + 1
);
}