I have a simple program written in c#. It's main purpose is for data archiving. Below is a snapshot of how the program looks like.
As shown, whenever a button is clicked, message will be displayed on the big text box on the left side of the program. Right now, I am implementing another button called Row Count. The purpose of this function is when I click that button, it will execute one of my SQL script and will display the number of counts in a table to the text box.
Inside that SQL script contains row count queries for multiple tables, not just one row count query. Here is a snippet of it :
SELECT COUNT(*) AS [PromotionEventRows] FROM PromotionEvent (NOLOCK)
select top 1 EventCode as [PE - PE Code], LastChangeDate FROM PromotionEvent (NOLOCK) ORDER BY LastChangeDate DESC
SELECT COUNT(*) AS [PromotionBenefitRows] FROM PromotionBenefit (NOLOCK)
select top 1 PromotionBenefitCode as [PB - PB Code], LastChangeDate FROM PromotionBenefit (NOLOCK) ORDER BY LastChangeDate DESC
SELECT COUNT(*) AS [CardTypeBenefitRows] FROM CardTypeBenefit (NOLOCK)
select top 1 CardTypeBenefitID, LastChangeDate FROM CardTypeBenefit (NOLOCK) ORDER BY LastChangeDate DESC
I know there is a method ExecuteScalar to accomplish this but the issue here is I don't want to write SQl query inside my c# code. Rather I want my program to read that script and display the count in the text display in the program. Think of it like getting the results in SQL server but instead the results are displayed inside my program text box. Any suggestion would be helpful.
Here is also part of my C# code:
try
{
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
FileInfo file = new FileInfo("Directory of that SQL script");
string script = file.OpenText().ReadToEnd();
Server server = new Server(new ServerConnection(con));
server.ConnectionContext.ExecuteNonQuery(script);
Display("Whatever message display on textbox");
con.Close();
}
}
catch (Exception ex)
{
textBox1.AppendText(string.Format("{0}", Environment.NewLine));
textBox1.AppendText(string.Format("{0} MainPage_Load() exception - {1}{2}", _strThisAppName, ex.Message, Environment.NewLine));
Display(ex.Message + "message");
textBox1.AppendText(string.Format("{0}", Environment.NewLine));
Debug.WriteLine(string.Format("{0} MainPage_Load() exception - {1}", _strThisAppName, ex.Message));
}