I need to check the SQL Column Name , which is created programmatically ...
There should be 2 validation rules:
- The name should not be a C # keyword
- Name must not be a SQL keyword (SQL Server 2008 R2)
The solution for the 1st rule is good:
The CSharpCodeProvider class has an IsValidIdentifier method that simplifies the implementation of validation.
(ex:
string myColumnName = "blabla"; var isValid = _cSharpCodeProvider.IsValidIdentifier(myColumnName);
)
Solution for the second rule: litle verbose:
The only way I found doing a search on Google is to take keywords from MSDN - SQL Server 2008 R2 reserved keywords (Transact-SQL)
To create a string [] property that will return all these keywords ...
(eg:
public static class SqlReservedKeywords { public static string[] SqlServerReservedKeywords { get { return SqlServerKeywords; } } private static readonly string[] SqlServerKeywords = new[] { "ADD","EXISTS","PRECISION",
// External code
var isValid = SqlReservedKeywords.SqlServerReservedKeywords.Contains(myColumnName);
)
Can you advise me on the implementation of the second validation rule. Is this a good practice? Maybe this is another implementation method that I did not find in googling ...
source share