I am used to working with the SqlHelper
class and so would like to modify the existing code to make use of it.
existing code
public string GetData()
{
string message = string.Empty;
string conStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection connection = new SqlConnection(conStr))
{
string query = "usp_getdata";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
message = reader[0].ToString();
}
}
}
return message;
}
The most i could do is this. How can I make this better. The basic idea is to minimize the lines of code, make use of SqlHelper, and also use dataset instead of datareader.
public string GetData()
{
string message = string.Empty;
string conStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
SqlConnection connection = new SqlConnection(conStr);
SqlCommand cmd = (SqlCommand)SqlHelper.CreateCommand(connection, "usp_getdata");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Notification = null;
SqlDependency dependency = new SqlDependency(cmd);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
message = ds.Tables[0].Rows[0]["Message"].ToString();
return message;
}