If you have multiple queries in your script file, then you should enhance your script with @rowsAffected
variable as shown in T-SQL below. Then, in your C# code you will need to call ExecuteScalar to get the detailed rows affected by your script.
**Script file with @rowsAffected variable logic**
--add following variable at start of your script
DECLARE @rowsAffected VARCHAR(2000);
INSERT INTO [dbo].[Products] ([ProductName]) VALUES ('sun1'),('sun2'),('sun3');
--after each query that you want to track, include the following line
SET @rowsAffected = 'Products : ' + CAST(@@rowcount AS varchar(20));
UPDATE [dbo].[newTable] SET [ColB] = 'b' ,[ColC] = 'd',[ColD] = 'e' ,[ColE] = 'f' WHERE ColA='a';
--after each query that you want to track, include the following line
SET @rowsAffected = @rowsAffected + ', newTable : ' + CAST(@@rowcount AS varchar(20));
-- add the query below at end of your script
SELECT @rowsAffected;
You will have to read the text from your script file, as you are doing in your code, and then create a command object using the text read from file before executing the code in snippet below.
C# code to execute above script
string rowsAffected =(string) command.ExecuteScalar();
//you can now use rowsAffected variable in any way you like
//it will contain something like Table1 : 4, Table2 : 6
Detailed C# code using your original code
using (SqlConnection con = new SqlConnection(constr))
{
FileInfo file = new FileInfo(DIRECTORY OF THE SCRIPT);
string script = file.OpenText().ReadToEnd();
SqlCommand command = new SqlCommand(script, con);
command.CommandType = CommandType.Text;
try
{
con.Open();
string rowsAffected =(string) command.ExecuteScalar();
Display( rowsAffected);
con.Close();
}
catch (Exception ex)
{
con.Close();
Display(ex.Message);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…