• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

Need some help to modulize some C# Database code

I want to take the following code below and some how put it in another file to keep the Main() code / Method calls for passing in information to database from repeating itself over and over.
Example, in the code below, the value "Reachable" which is assigned to the param.Value property will probably be the only piece of information that changes.
I want to add other "IF" conditional statements, for example, if (pReply.Status == IPStatus.Timeout)
{
Do all the same stuff as previous if statement but just change the param.Value = "Timed Out";
}
I'm trying to figure out something now but I want some insight from an Expert.
Regards,
Wallace
if (pReply.Status == IPStatus.Success)
            {
                // setting up connection object
                SqlConnection conn = new SqlConnection(SettingsManager.ConnectionString);
                conn.Open();
 
                // setting up command objects
                SqlCommand cmd = new SqlCommand("usp_PingStatus", conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
 
                // creating output parameter (as objects)
                // & adding to parameter collection
                SqlParameter param = new SqlParameter();
                SqlParameter param2 = new SqlParameter();
                param = cmd.Parameters.Add("@Status", System.Data.SqlDbType.VarChar, 50, "PingStatus");
                param2 = cmd.Parameters.Add("@Endpoint", System.Data.SqlDbType.VarChar, 75, "Name");
                param3 = cmd.Parameters.Add();
                param.Direction = System.Data.ParameterDirection.Input;
                param2.Direction = System.Data.ParameterDirection.Input;
                param.Value = "Reachable";
                param2.Value = hostname;
                                                
                // execute the query
                cmd.ExecuteNonQuery();
 
                Console.WriteLine(param.Value);
                Console.ReadLine();
                conn.Close();
            }

Open in new window

0
wally_davis
Asked:
wally_davis
  • 3
  • 3
3 Solutions
 
ScottieSLGCommented:
Sounds like you could encapsulate this entire block into a method in another class, with a single parameter that passes param.Value.

0
 
williamcampbellCommented:
Something like this
public class Class1
{
	Dictionary<int,string> commands;
 
    public Class1()
    {
        commands[IPStatus.Success] = "Reachable";
        commands[IPStatus.Timeout] = "Timeout";
    }
 
    void ExecuteQuery ( string operation )
    {
        // setting up connection object
        SqlConnection conn = new SqlConnection(SettingsManager.ConnectionString);
        conn.Open();
 
        // setting up command objects
        SqlCommand cmd = new SqlCommand("usp_PingStatus", conn);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
 
        // creating output parameter (as objects)
        // & adding to parameter collection
        SqlParameter param = new SqlParameter();
        SqlParameter param2 = new SqlParameter();
        param = cmd.Parameters.Add("@Status", System.Data.SqlDbType.VarChar, 50, "PingStatus");
        param2 = cmd.Parameters.Add("@Endpoint", System.Data.SqlDbType.VarChar, 75, "Name");
        param3 = cmd.Parameters.Add();
        param.Direction = System.Data.ParameterDirection.Input;
        param2.Direction = System.Data.ParameterDirection.Input;
        param.Value = operation;
        param2.Value = hostname;
 
        // execute the query
        cmd.ExecuteNonQuery();
 
        Console.WriteLine(param.Value);
        Console.ReadLine();
        conn.Close();
    }
 
    void HandleStatus ( StatusClass pReply )
    {
        ExecuteQuery ( commands[pReply.Status] );
    }
}

Open in new window

0
 
wally_davisAuthor Commented:
William, it appears that the Dictionary<int,string> commands requires two values.
I only need one value to pass in. Is there a way to pass in a value of null or empty
to "int" in <int, string>?

EXAMPLE:
    public Class1()
    {
        commands[null, IPStatus.Success] = "Reachable";
        commands[null, IPStatus.Timeout] = "Timeout";
    }
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
williamcampbellCommented:
Try this instead  (I get confused with C++ sometimes :)
            Dictionary<int, string> commands = new Dictionary<int,string>();
 
            commands.Add( IPStatus.Success,"Reachable");

Open in new window

0
 
wally_davisAuthor Commented:
William,

What does the calling method look like?
I have the routine you've given me above in a Class named "EPStatus.cs".
The method name in the class I'm using is the one you have, which is "void HandleStatus ( StatusClass pReply )"
So, if I have this selection statement:
if (pReply.Status == IPStatus.Success)
}
        // What do I need to pass to the called method in the
        // EPStatus.cs Class?
        EPStatus.HandleStatus(IPStatus.Success);
}
0
 
williamcampbellCommented:

 I wasn't sure what type  pReply was  
 You should change the function to

 HandleStatus ( <whatever a preply is> )

then call the function like this

 EPStatus.HandleStatus ( IPStatus )  
0
 
wally_davisAuthor Commented:
William, I've never worked with the Dictionary class and was a nice intro to work with it. Thanks for walking me through it.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now