Hi All,
We have a requirement to write a custom FxCop rule to generate a warning message when a SQL Query is encountered in the program and suggest to use a Stored Procedure instead.
I have written the following code which is generating a warning when an SQL object is present -- Warning is generated as soon as an object of SqlConnection is created. Hence even when there is only stored procedure and no query warning still pops.
Can anyone please suggest me how do I modify so that warning is generated only if there is a direct SQL Query ie., for SELECT, INSERT,UPDATE & DELETE statements.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Cci;
using Microsoft.FxCop.Sdk;
using Microsoft.FxCop.Sdk.Introspection;
namespace CompanyRules
{
public class UseStoredProcedureForSQLQuery : BaseIntrospectionRule
{
public UseStoredProcedureForSQLQuery():
base("UseStoredProcedureForSQLQuery","CompanyRules.RuleData", typeofUseStoredProcedureForSQLQuery).Assembly)
{
}
public override ProblemCollection Check(TypeNode type)
{
return Problems;
}
public override ProblemCollection Check(Member member)
{
Method mainMethod = member as Method;
Instruction instruction;
if (mainMethod == null)
{
return null;
}
if (mainMethod.Instructions == null)
{
return null;
}
for (int count = 0; count <= mainMethod.Instructions.Length - 1; count++)
{
instruction = mainMethod.Instructions[count];
if (instruction.OpCode == OpCode.Newobj)
{
if (((Microsoft.Cci.Method)(instruction.Value)).
FullName.Contains("System.Data.SqlClient.SqlConnection.#ctor"))
{
Problems.Add(new Problem(GetResolution("SqlConnection","Cafe.net connection")));
}
if (((Microsoft.Cci.Method)(instruction.Value)).
FullName.Contains("System.Data.SqlClient.SqlCommand.#ctor"))
{
Problems.Add(new Problem(GetResolution("SqlCommand", "Cafe.net command")));
}
}
}
return Problems;
}
}
}