wally_davis
asked on
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
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();
}
Sounds like you could encapsulate this entire block into a method in another class, with a single parameter that passes param.Value.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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";
}
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";
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(IPSt atus.Succe ss);
}
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(IPSt
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
William, I've never worked with the Dictionary class and was a nice intro to work with it. Thanks for walking me through it.