ASP.NET Testing sql query in c#











up vote
0
down vote

favorite












As part of my Application Architecture Module at Uni I am testing the time taken to retrieve data from an MS SQL server local and remotely. I have a user interface using visual studio 2017 ASP.NET with c#. I instantiate and start a stopwatch control when a user clicks a button and stop the stopwatch when afer the line of code that bind the result to the gridview. I have two buttons, one runs the code to a local databaase created in visual studio and the other button runs identical code (except connectionstring) to remote SQL Server (GoDaddy). The results i am getting are very fast 30ms and 100ms remotely.



My question really is, are the times i am getting accurate for what i think its doing or it just giving me the time to create the page after postback.



Is there i way i can simulate simultaneous connections to each database to test performance under real world use.



    protected void Btnlocal_Click(object sender, EventArgs e)
{
if (txthouseno.Text != "" && txtpostcode.Text != "")
{
Label4.Visible = true;
Stopwatch localStopWatch = new Stopwatch();
localStopWatch.Start();

string connectionString;
SqlConnection cnn;
connectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:xxxxxxxxxxxxxxxx.mdf;Integrated Security=True;Connect Timeout=30";
cnn = new SqlConnection(connectionString);
cnn.Open();
lbllocal.Text = "Local Database Connected";
SqlCommand command;
SqlDataReader dataReader;

String sql = "select * from Customer where postcode = @custpcode AND houseno = @custhouseno";
command = new SqlCommand(sql, cnn);

var custpostParam = new SqlParameter("custpcode", SqlDbType.VarChar);
custpostParam.Value = txtpostcode.Text;

var custhouseParam = new SqlParameter("custhouseno", SqlDbType.VarChar);
custhouseParam.Value = txthouseno.Text;

command.Parameters.Add(custpostParam);
command.Parameters.Add(custhouseParam);
dataReader = command.ExecuteReader();
GridView1.DataSource = dataReader;
GridView1.DataBind();
localStopWatch.Stop();
command.Dispose();
cnn.Close();
connectionString = "";

lbllocal.Text = "Local PC Time Taken = " + localStopWatch.ElapsedMilliseconds.ToString() + " ms";

}
}

protected void Btnremote_Click(object sender, EventArgs e)
{
if (txthouseno.Text != "" && txtpostcode.Text != "")
{
Label5.Visible = true;
Stopwatch remoteStopWatch = new Stopwatch();
remoteStopWatch.Start();
ViewState["pageLoadTime"] = DateTime.Now;
string connectionString;
SqlConnection cnn;
connectionString = @"Data Source=xxx.xxx.xx.xxx;Integrated Security=False;User ID=xxxxxx; Password = xxxxxx; Connect Timeout=15;Encrypt=False;Packet Size=4096";
cnn = new SqlConnection(connectionString);
cnn.Open();
SqlCommand command;
SqlDataReader dataReader;

String sql = "select * from dbo.Customer where postcode = @custpcode AND houseno = @custhouseno";
command = new SqlCommand(sql, cnn);

var custpostParam = new SqlParameter("custpcode", SqlDbType.VarChar);
custpostParam.Value = txtpostcode.Text;

var custhouseParam = new SqlParameter("custhouseno", SqlDbType.VarChar);
custhouseParam.Value = txthouseno.Text;

command.Parameters.Add(custpostParam);
command.Parameters.Add(custhouseParam);
dataReader = command.ExecuteReader();
GridView2.DataSource = dataReader;
GridView2.DataBind();
remoteStopWatch.Stop();
command.Dispose();
cnn.Close();
connectionString = "";

lblremote.Text = "Remote Server Time Taken = " + remoteStopWatch.ElapsedMilliseconds.ToString() + " ms";

}
} enter code here









share|improve this question






















  • A fairly quick and accurate way to get this data would be to add Application Insights to your app. You can either create a free instance on the Azure Portal, that your app will send data to, or just view the data locally in Visual Studio. The SQL calls will then appear as "Dependencies" complete with accurate timings.
    – sellotape
    Nov 10 at 16:33










  • Re your actually question, it seems reasonable that the SQL calls are complete when (or very close to when) you stop the stopwatch, as you're closing the connection and disposing the command then. Any deferred binding would not have access to these and would fail, so if he binding is indeed working, your timings are roughly correct.
    – sellotape
    Nov 10 at 16:39












  • Just remember that these are not SQL execution times, as the execution time would be less. What you're measuring is the time to go to SQL Server (through LAN/WAN), run the query and get the result back (LAN/WAN). If you need accurate SQL timing, you'll need SQL Server Profiler.
    – Anderson Matos
    Nov 10 at 16:58












  • Thanks for the comments.
    – Allan McCaffery
    Nov 11 at 14:45















up vote
0
down vote

favorite












As part of my Application Architecture Module at Uni I am testing the time taken to retrieve data from an MS SQL server local and remotely. I have a user interface using visual studio 2017 ASP.NET with c#. I instantiate and start a stopwatch control when a user clicks a button and stop the stopwatch when afer the line of code that bind the result to the gridview. I have two buttons, one runs the code to a local databaase created in visual studio and the other button runs identical code (except connectionstring) to remote SQL Server (GoDaddy). The results i am getting are very fast 30ms and 100ms remotely.



My question really is, are the times i am getting accurate for what i think its doing or it just giving me the time to create the page after postback.



Is there i way i can simulate simultaneous connections to each database to test performance under real world use.



    protected void Btnlocal_Click(object sender, EventArgs e)
{
if (txthouseno.Text != "" && txtpostcode.Text != "")
{
Label4.Visible = true;
Stopwatch localStopWatch = new Stopwatch();
localStopWatch.Start();

string connectionString;
SqlConnection cnn;
connectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:xxxxxxxxxxxxxxxx.mdf;Integrated Security=True;Connect Timeout=30";
cnn = new SqlConnection(connectionString);
cnn.Open();
lbllocal.Text = "Local Database Connected";
SqlCommand command;
SqlDataReader dataReader;

String sql = "select * from Customer where postcode = @custpcode AND houseno = @custhouseno";
command = new SqlCommand(sql, cnn);

var custpostParam = new SqlParameter("custpcode", SqlDbType.VarChar);
custpostParam.Value = txtpostcode.Text;

var custhouseParam = new SqlParameter("custhouseno", SqlDbType.VarChar);
custhouseParam.Value = txthouseno.Text;

command.Parameters.Add(custpostParam);
command.Parameters.Add(custhouseParam);
dataReader = command.ExecuteReader();
GridView1.DataSource = dataReader;
GridView1.DataBind();
localStopWatch.Stop();
command.Dispose();
cnn.Close();
connectionString = "";

lbllocal.Text = "Local PC Time Taken = " + localStopWatch.ElapsedMilliseconds.ToString() + " ms";

}
}

protected void Btnremote_Click(object sender, EventArgs e)
{
if (txthouseno.Text != "" && txtpostcode.Text != "")
{
Label5.Visible = true;
Stopwatch remoteStopWatch = new Stopwatch();
remoteStopWatch.Start();
ViewState["pageLoadTime"] = DateTime.Now;
string connectionString;
SqlConnection cnn;
connectionString = @"Data Source=xxx.xxx.xx.xxx;Integrated Security=False;User ID=xxxxxx; Password = xxxxxx; Connect Timeout=15;Encrypt=False;Packet Size=4096";
cnn = new SqlConnection(connectionString);
cnn.Open();
SqlCommand command;
SqlDataReader dataReader;

String sql = "select * from dbo.Customer where postcode = @custpcode AND houseno = @custhouseno";
command = new SqlCommand(sql, cnn);

var custpostParam = new SqlParameter("custpcode", SqlDbType.VarChar);
custpostParam.Value = txtpostcode.Text;

var custhouseParam = new SqlParameter("custhouseno", SqlDbType.VarChar);
custhouseParam.Value = txthouseno.Text;

command.Parameters.Add(custpostParam);
command.Parameters.Add(custhouseParam);
dataReader = command.ExecuteReader();
GridView2.DataSource = dataReader;
GridView2.DataBind();
remoteStopWatch.Stop();
command.Dispose();
cnn.Close();
connectionString = "";

lblremote.Text = "Remote Server Time Taken = " + remoteStopWatch.ElapsedMilliseconds.ToString() + " ms";

}
} enter code here









share|improve this question






















  • A fairly quick and accurate way to get this data would be to add Application Insights to your app. You can either create a free instance on the Azure Portal, that your app will send data to, or just view the data locally in Visual Studio. The SQL calls will then appear as "Dependencies" complete with accurate timings.
    – sellotape
    Nov 10 at 16:33










  • Re your actually question, it seems reasonable that the SQL calls are complete when (or very close to when) you stop the stopwatch, as you're closing the connection and disposing the command then. Any deferred binding would not have access to these and would fail, so if he binding is indeed working, your timings are roughly correct.
    – sellotape
    Nov 10 at 16:39












  • Just remember that these are not SQL execution times, as the execution time would be less. What you're measuring is the time to go to SQL Server (through LAN/WAN), run the query and get the result back (LAN/WAN). If you need accurate SQL timing, you'll need SQL Server Profiler.
    – Anderson Matos
    Nov 10 at 16:58












  • Thanks for the comments.
    – Allan McCaffery
    Nov 11 at 14:45













up vote
0
down vote

favorite









up vote
0
down vote

favorite











As part of my Application Architecture Module at Uni I am testing the time taken to retrieve data from an MS SQL server local and remotely. I have a user interface using visual studio 2017 ASP.NET with c#. I instantiate and start a stopwatch control when a user clicks a button and stop the stopwatch when afer the line of code that bind the result to the gridview. I have two buttons, one runs the code to a local databaase created in visual studio and the other button runs identical code (except connectionstring) to remote SQL Server (GoDaddy). The results i am getting are very fast 30ms and 100ms remotely.



My question really is, are the times i am getting accurate for what i think its doing or it just giving me the time to create the page after postback.



Is there i way i can simulate simultaneous connections to each database to test performance under real world use.



    protected void Btnlocal_Click(object sender, EventArgs e)
{
if (txthouseno.Text != "" && txtpostcode.Text != "")
{
Label4.Visible = true;
Stopwatch localStopWatch = new Stopwatch();
localStopWatch.Start();

string connectionString;
SqlConnection cnn;
connectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:xxxxxxxxxxxxxxxx.mdf;Integrated Security=True;Connect Timeout=30";
cnn = new SqlConnection(connectionString);
cnn.Open();
lbllocal.Text = "Local Database Connected";
SqlCommand command;
SqlDataReader dataReader;

String sql = "select * from Customer where postcode = @custpcode AND houseno = @custhouseno";
command = new SqlCommand(sql, cnn);

var custpostParam = new SqlParameter("custpcode", SqlDbType.VarChar);
custpostParam.Value = txtpostcode.Text;

var custhouseParam = new SqlParameter("custhouseno", SqlDbType.VarChar);
custhouseParam.Value = txthouseno.Text;

command.Parameters.Add(custpostParam);
command.Parameters.Add(custhouseParam);
dataReader = command.ExecuteReader();
GridView1.DataSource = dataReader;
GridView1.DataBind();
localStopWatch.Stop();
command.Dispose();
cnn.Close();
connectionString = "";

lbllocal.Text = "Local PC Time Taken = " + localStopWatch.ElapsedMilliseconds.ToString() + " ms";

}
}

protected void Btnremote_Click(object sender, EventArgs e)
{
if (txthouseno.Text != "" && txtpostcode.Text != "")
{
Label5.Visible = true;
Stopwatch remoteStopWatch = new Stopwatch();
remoteStopWatch.Start();
ViewState["pageLoadTime"] = DateTime.Now;
string connectionString;
SqlConnection cnn;
connectionString = @"Data Source=xxx.xxx.xx.xxx;Integrated Security=False;User ID=xxxxxx; Password = xxxxxx; Connect Timeout=15;Encrypt=False;Packet Size=4096";
cnn = new SqlConnection(connectionString);
cnn.Open();
SqlCommand command;
SqlDataReader dataReader;

String sql = "select * from dbo.Customer where postcode = @custpcode AND houseno = @custhouseno";
command = new SqlCommand(sql, cnn);

var custpostParam = new SqlParameter("custpcode", SqlDbType.VarChar);
custpostParam.Value = txtpostcode.Text;

var custhouseParam = new SqlParameter("custhouseno", SqlDbType.VarChar);
custhouseParam.Value = txthouseno.Text;

command.Parameters.Add(custpostParam);
command.Parameters.Add(custhouseParam);
dataReader = command.ExecuteReader();
GridView2.DataSource = dataReader;
GridView2.DataBind();
remoteStopWatch.Stop();
command.Dispose();
cnn.Close();
connectionString = "";

lblremote.Text = "Remote Server Time Taken = " + remoteStopWatch.ElapsedMilliseconds.ToString() + " ms";

}
} enter code here









share|improve this question













As part of my Application Architecture Module at Uni I am testing the time taken to retrieve data from an MS SQL server local and remotely. I have a user interface using visual studio 2017 ASP.NET with c#. I instantiate and start a stopwatch control when a user clicks a button and stop the stopwatch when afer the line of code that bind the result to the gridview. I have two buttons, one runs the code to a local databaase created in visual studio and the other button runs identical code (except connectionstring) to remote SQL Server (GoDaddy). The results i am getting are very fast 30ms and 100ms remotely.



My question really is, are the times i am getting accurate for what i think its doing or it just giving me the time to create the page after postback.



Is there i way i can simulate simultaneous connections to each database to test performance under real world use.



    protected void Btnlocal_Click(object sender, EventArgs e)
{
if (txthouseno.Text != "" && txtpostcode.Text != "")
{
Label4.Visible = true;
Stopwatch localStopWatch = new Stopwatch();
localStopWatch.Start();

string connectionString;
SqlConnection cnn;
connectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:xxxxxxxxxxxxxxxx.mdf;Integrated Security=True;Connect Timeout=30";
cnn = new SqlConnection(connectionString);
cnn.Open();
lbllocal.Text = "Local Database Connected";
SqlCommand command;
SqlDataReader dataReader;

String sql = "select * from Customer where postcode = @custpcode AND houseno = @custhouseno";
command = new SqlCommand(sql, cnn);

var custpostParam = new SqlParameter("custpcode", SqlDbType.VarChar);
custpostParam.Value = txtpostcode.Text;

var custhouseParam = new SqlParameter("custhouseno", SqlDbType.VarChar);
custhouseParam.Value = txthouseno.Text;

command.Parameters.Add(custpostParam);
command.Parameters.Add(custhouseParam);
dataReader = command.ExecuteReader();
GridView1.DataSource = dataReader;
GridView1.DataBind();
localStopWatch.Stop();
command.Dispose();
cnn.Close();
connectionString = "";

lbllocal.Text = "Local PC Time Taken = " + localStopWatch.ElapsedMilliseconds.ToString() + " ms";

}
}

protected void Btnremote_Click(object sender, EventArgs e)
{
if (txthouseno.Text != "" && txtpostcode.Text != "")
{
Label5.Visible = true;
Stopwatch remoteStopWatch = new Stopwatch();
remoteStopWatch.Start();
ViewState["pageLoadTime"] = DateTime.Now;
string connectionString;
SqlConnection cnn;
connectionString = @"Data Source=xxx.xxx.xx.xxx;Integrated Security=False;User ID=xxxxxx; Password = xxxxxx; Connect Timeout=15;Encrypt=False;Packet Size=4096";
cnn = new SqlConnection(connectionString);
cnn.Open();
SqlCommand command;
SqlDataReader dataReader;

String sql = "select * from dbo.Customer where postcode = @custpcode AND houseno = @custhouseno";
command = new SqlCommand(sql, cnn);

var custpostParam = new SqlParameter("custpcode", SqlDbType.VarChar);
custpostParam.Value = txtpostcode.Text;

var custhouseParam = new SqlParameter("custhouseno", SqlDbType.VarChar);
custhouseParam.Value = txthouseno.Text;

command.Parameters.Add(custpostParam);
command.Parameters.Add(custhouseParam);
dataReader = command.ExecuteReader();
GridView2.DataSource = dataReader;
GridView2.DataBind();
remoteStopWatch.Stop();
command.Dispose();
cnn.Close();
connectionString = "";

lblremote.Text = "Remote Server Time Taken = " + remoteStopWatch.ElapsedMilliseconds.ToString() + " ms";

}
} enter code here






c# sql postback






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 16:25









Allan McCaffery

11




11












  • A fairly quick and accurate way to get this data would be to add Application Insights to your app. You can either create a free instance on the Azure Portal, that your app will send data to, or just view the data locally in Visual Studio. The SQL calls will then appear as "Dependencies" complete with accurate timings.
    – sellotape
    Nov 10 at 16:33










  • Re your actually question, it seems reasonable that the SQL calls are complete when (or very close to when) you stop the stopwatch, as you're closing the connection and disposing the command then. Any deferred binding would not have access to these and would fail, so if he binding is indeed working, your timings are roughly correct.
    – sellotape
    Nov 10 at 16:39












  • Just remember that these are not SQL execution times, as the execution time would be less. What you're measuring is the time to go to SQL Server (through LAN/WAN), run the query and get the result back (LAN/WAN). If you need accurate SQL timing, you'll need SQL Server Profiler.
    – Anderson Matos
    Nov 10 at 16:58












  • Thanks for the comments.
    – Allan McCaffery
    Nov 11 at 14:45


















  • A fairly quick and accurate way to get this data would be to add Application Insights to your app. You can either create a free instance on the Azure Portal, that your app will send data to, or just view the data locally in Visual Studio. The SQL calls will then appear as "Dependencies" complete with accurate timings.
    – sellotape
    Nov 10 at 16:33










  • Re your actually question, it seems reasonable that the SQL calls are complete when (or very close to when) you stop the stopwatch, as you're closing the connection and disposing the command then. Any deferred binding would not have access to these and would fail, so if he binding is indeed working, your timings are roughly correct.
    – sellotape
    Nov 10 at 16:39












  • Just remember that these are not SQL execution times, as the execution time would be less. What you're measuring is the time to go to SQL Server (through LAN/WAN), run the query and get the result back (LAN/WAN). If you need accurate SQL timing, you'll need SQL Server Profiler.
    – Anderson Matos
    Nov 10 at 16:58












  • Thanks for the comments.
    – Allan McCaffery
    Nov 11 at 14:45
















A fairly quick and accurate way to get this data would be to add Application Insights to your app. You can either create a free instance on the Azure Portal, that your app will send data to, or just view the data locally in Visual Studio. The SQL calls will then appear as "Dependencies" complete with accurate timings.
– sellotape
Nov 10 at 16:33




A fairly quick and accurate way to get this data would be to add Application Insights to your app. You can either create a free instance on the Azure Portal, that your app will send data to, or just view the data locally in Visual Studio. The SQL calls will then appear as "Dependencies" complete with accurate timings.
– sellotape
Nov 10 at 16:33












Re your actually question, it seems reasonable that the SQL calls are complete when (or very close to when) you stop the stopwatch, as you're closing the connection and disposing the command then. Any deferred binding would not have access to these and would fail, so if he binding is indeed working, your timings are roughly correct.
– sellotape
Nov 10 at 16:39






Re your actually question, it seems reasonable that the SQL calls are complete when (or very close to when) you stop the stopwatch, as you're closing the connection and disposing the command then. Any deferred binding would not have access to these and would fail, so if he binding is indeed working, your timings are roughly correct.
– sellotape
Nov 10 at 16:39














Just remember that these are not SQL execution times, as the execution time would be less. What you're measuring is the time to go to SQL Server (through LAN/WAN), run the query and get the result back (LAN/WAN). If you need accurate SQL timing, you'll need SQL Server Profiler.
– Anderson Matos
Nov 10 at 16:58






Just remember that these are not SQL execution times, as the execution time would be less. What you're measuring is the time to go to SQL Server (through LAN/WAN), run the query and get the result back (LAN/WAN). If you need accurate SQL timing, you'll need SQL Server Profiler.
– Anderson Matos
Nov 10 at 16:58














Thanks for the comments.
– Allan McCaffery
Nov 11 at 14:45




Thanks for the comments.
– Allan McCaffery
Nov 11 at 14:45

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240955%2fasp-net-testing-sql-query-in-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240955%2fasp-net-testing-sql-query-in-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma