| | |
Custom FxCop Rule.
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 11
Reputation:
Solved Threads: 0
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.
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;
}
}
} Thanks & Regards
Stored procedure name as well SQL Statement may be in SqlCommand class, can you fetch some properties of SqlCommand class using Microsoft.Cci??
Last edited by Ramy Mahrous; Dec 30th, 2008 at 8:13 am.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Look what makes difference is that you get SQLCommand.CommandType = ? "Text" or "StoredProcedure"
If you get CommandType value you'll solve your problem I didn't work before with Microsoft.Cci library!
If you get CommandType value you'll solve your problem I didn't work before with Microsoft.Cci library!
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Aug 2008
Posts: 11
Reputation:
Solved Threads: 0
Hi Ramy,
I have tried the approach you have mentioned but am unable to get the desired result. Also, the check needs to be done not only with commandType.Text or StoredProcedure but with other SQL statements if any in the program. Please see the following code:
There should be a warning generated in to remove the direct SQL query in the SqlCommand.
Can you please suggest how can i capture this?
Thank you.
Regards,
Sahana
I have tried the approach you have mentioned but am unable to get the desired result. Also, the check needs to be done not only with commandType.Text or StoredProcedure but with other SQL statements if any in the program. Please see the following code:
C# Syntax (Toggle Plain Text)
using System; using System.Data; using System.Data.SqlClient; namespace example { class Test { public static void Main() { SqlConnection MyConnection = new SqlConnection(@"Data Source=(local); Initial Catalog = CaseManager; Integrated Security=true"); MyConnection.Open(); SqlCommand MyCmd = new SqlCommand(@"INSERT INTO Test(ID, Contact, Email) VALUES(2, 'Greg', 'MacBeth')";, MyConnection); MyConnection.Close(); } } }
There should be a warning generated in to remove the direct SQL query in the SqlCommand.
Can you please suggest how can i capture this?
Thank you.
Regards,
Sahana
Thanks & Regards
You can check if the Command Object's Text value has (Select, Insert, Update or Delete) ? using string operations ? I don't know how to use Cci !! Did you try googlize it?
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
![]() |
Other Threads in the C# Forum
- Previous Thread: Webservice on port 9999 vs2008
- Next Thread: Change fileNames using C# code
| Thread Tools | Search this Thread |
.net access algorithm alignment app array barchart bitmap box broadcast c# c#gridviewcolumn check checkbox client combobox communication control conversion csharp custom database datagrid datagridview dataset datatable datetime degrees development draganddrop drawing elevated encryption enum event excel file focus form format forms function gdi+ hospitalmanagementsystem httpwebrequest image index input install java label list listbox localization login mandelbrot math messagebox mouseclick mysql operator path photoshop picturebox pixelinversion plotting pointer post programming radians read regex remote remoting richtextbox server sleep socket sql statistics stream string stringformatting sun table text textbox thread time timer update usercontrol validation visualstudio webbrowser whileloop windows winforms wpf xml







!!