How to trigger and find status of sql job through c#? [duplicate] - c#

I need to create an application for monitoring SQL Server 2000 Agent Job status and info when Job occur same as show on Windows application event log. Now I connect to the database already via a connection string, but I don't know how to get the status and info from Job.
I need to show status and info on Textbox.
What do you suggestion how to do.
Developer tools :
MS SQL Sever 2000 SP4
MS Visual Studio 2008 (C#)
I am a rookie programmer.

i can do this already...
i select form table "Sysjobserver" in database "msdb" for read status, date, time of job that i want.
use this code
public void GetJobsAndStatus()
{
string sqlJobQuery = "select j.job_id, j.name, j.enabled, jh.run_status," +
" js.last_outcome_message, jh.run_date, jh.step_name, jh.run_time" +
" from sysjobs j left join sysjobhistory jh on (j.job_id = jh.job_id)" +
" left join sysjobservers js on (j.job_id = js.job_id)" +
" where jh.run_date = (select Max(run_date) from sysjobhistory)" +
" and jh.run_time = (select Max(run_time) from sysjobhistory)";
// create SQL connection and set up SQL Command for query
using (SqlConnection _con = new SqlConnection("server=10.15.13.70;database=msdb;user id=sa;pwd="))
using (SqlCommand _cmd = new SqlCommand(sqlJobQuery, _con))
{
try
{
// open connection
_con.Open();
SqlConnection.ClearPool(_con);
// create SQL Data Reader and grab data
using (SqlDataReader rdr = _cmd.ExecuteReader())
{
// as long as we get information from the reader
while (rdr.Read())
{
Guid jobID = rdr.GetGuid(0); // read Job_id
string jobName = rdr.GetString(1); // read Job name
byte jobEnabled = rdr.GetByte(2); // read Job enabled flag
int jobStatus = rdr.GetInt32(3); // read last_run_outcome from sysjobserver
string jobMessage = rdr.GetString(4); // read Message from sysjobserver
int jobRunDate = rdr.GetInt32(5); // read run_date from sysjobhistory
string jobStepName = rdr.GetString(6); // read StepName from sysjobhistory
int jobRunTime = rdr.GetInt32(7); // read run_time from sysjobhistory
String[] lviData = new String[] // ตัวแปรอะเรย์ชื่อ lviData
{
jobID.ToString(),
jobName.ToString(),
jobStepName.ToString(),
jobMessage.ToString(),
jobStatus.ToString(),
jobRunDate.ToString(),
jobRunTime.ToString(),
//jobEnabled.ToString(),
};
newData = lviData;
DisplayList(); // for display data on datagridview
}
rdr.Close();
}
}
thank you for everybody help very much. :-D

SQL stored procedures of queries don't give you any system data unless you have db_owner rights on the msdb system database, at lease in SQL Server 2008. Therefore mentioned methods normally don't work for applications where you want to show or manage jobs. However SMO namespace provides you with managed code solution for many SQL Server management features, including the SQL Server Agent functions that only require SQLServerAgent* permissions that you normally could get sorted for your application user. A good intro of using SMO classes to work with jobs is given here:
http://www.codeproject.com/Tips/367470/Manage-SQL-Server-Agent-Jobs-using-Csharp
I work on a similar task now and whilst SQL queries give me access denied, with C# code and Microsoft.SqlServer.Management.Smo.Agent namespace I just listed all jobs with this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo.Agent;
namespace SmoTest
{
class Program
{
static readonly string SqlServer = #"SQL01\SQL01";
static void Main(string[] args)
{
ServerConnection conn = new ServerConnection(SqlServer);
Server server = new Server(conn);
JobCollection jobs = server.JobServer.Jobs;
foreach (Job job in jobs)
{
Console.WriteLine(job.Name);
}
}
}
}

This should be a good starting point to find out how to find your SQL Agent jobs using T-SQL:
View (and disable) SQL Agent Jobs with TSQL
The script will list out all your jobs on your database, and when they will be run next and so forth.
Using the job_name, you should also be able to find out details about your jobs using the SQL Server Agent Stored Procedures in the msdb database on your server.

On SQL Server 2005 and above, you can use the system stored procedure msdb.dbo.sp_help_job to get information, including status, about SQL Server Agent Jobs. You can read more about sp_help_job at http://msdn.microsoft.com/en-us/library/ms186722(v=SQL.90).aspx.
Here is the sample code to do this from C#.
private Dictionary<int, string> ExecutionStatusDictionary = new Dictionary<int, string>()
{
{0, "Not idle or suspended"},
{1, "Executing"},
{2, "Waiting for thread"},
{3, "Between retries"},
{4, "Idle"},
{5, "Suspended"},
{7, "Performing completion actions"}
};
public string GetStatus()
{
SqlConnection msdbConnection = new SqlConnection("Data Source=SERVERNAME;Initial Catalog=msdb;Integrated Security=SSPI");
System.Text.StringBuilder resultBuilder = new System.Text.StringBuilder();
try
{
msdbConnection.Open();
SqlCommand jobStatusCommand = msdbConnection.CreateCommand();
jobStatusCommand.CommandType = CommandType.StoredProcedure;
jobStatusCommand.CommandText = "sp_help_job";
SqlParameter jobName = jobStatusCommand.Parameters.Add("#job_name", SqlDbType.VarChar);
jobName.Direction = ParameterDirection.Input;
jobName.Value = "LoadRegions";
SqlParameter jobAspect = jobStatusCommand.Parameters.Add("#job_aspect", SqlDbType.VarChar);
jobAspect.Direction = ParameterDirection.Input;
jobAspect.Value = "JOB";
SqlDataReader jobStatusReader = jobStatusCommand.ExecuteReader();
while (jobStatusReader.Read())
{
resultBuilder.Append(string.Format("{0} {1}",
jobStatusReader["name"].ToString(),
ExecutionStatusDictionary[(int)jobStatusReader["current_execution_status"]]
));
}
jobStatusReader.Close();
}
finally
{
msdbConnection.Close();
}
return resultBuilder.ToString();
}

You can get a list of all server jobs using this SELECT:
SELECT [name] FROM msdb.dbo.sysjobs
If you'd like to get a list of currently running jobs and their information, I would recommend writing a stored procedure in SQL which your application calls. There's a good demonstration here you could use...
http://feodorgeorgiev.com/blog/2010/03/how-to-query-currently-running-sql-server-agent-jobs/
Good luck!

For my use case, I specifically needed to know when the job was finished running, and whether or not it succeeded. Here is my code to do that:
using System;
using System.Data;
using System.Data.SqlClient;
namespace LaunchJobAndWaitTillDone
{
class Program
{
const string connectionString = "Data Source=YOURSERVERNAMEHERE;Initial Catalog=msdb;Integrated Security=SSPI";
const string jobName = "YOURJOBNAMEHERE";
static readonly TimeSpan waitFor = TimeSpan.FromSeconds(1.0);
enum JobExecutionResult
{
Succeeded,
FailedToStart,
FailedAfterStart,
Unknown
}
static void Main(string[] args)
{
var instance = new Program();
JobExecutionResult jobResult = instance.RunJob(jobName);
switch (jobResult)
{
case JobExecutionResult.Succeeded:
Console.WriteLine($"SQL Server Agent job, '{jobName}', ran successfully to completion.");
break;
case JobExecutionResult.FailedToStart:
Console.WriteLine($"SQL Server Agent job, '{jobName}', failed to start.");
break;
case JobExecutionResult.FailedAfterStart:
Console.WriteLine($"SQL Server Agent job, '{jobName}', started successfully, but encountered an error.");
break;
default:
Console.WriteLine($"Unknown result from attempting to run SQL Server Agent job, '{jobName}'.");
break;
}
Console.ReadLine();
return;
}
JobExecutionResult RunJob(string jobName)
{
int jobResult;
using (var jobConnection = new SqlConnection(connectionString))
{
SqlCommand jobCommand;
SqlParameter jobReturnValue;
SqlParameter jobParameter;
jobCommand = new SqlCommand("sp_start_job", jobConnection);
jobCommand.CommandType = CommandType.StoredProcedure;
jobReturnValue = new SqlParameter("#RETURN_VALUE", SqlDbType.Int);
jobReturnValue.Direction = ParameterDirection.ReturnValue;
jobCommand.Parameters.Add(jobReturnValue);
jobParameter = new SqlParameter("#job_name", SqlDbType.VarChar);
jobParameter.Direction = ParameterDirection.Input;
jobCommand.Parameters.Add(jobParameter);
jobParameter.Value = jobName;
jobConnection.Open();
try
{
jobCommand.ExecuteNonQuery();
jobResult = (Int32)jobCommand.Parameters["#RETURN_VALUE"].Value;
}
catch (SqlException)
{
jobResult = -1;
}
}
switch (jobResult)
{
case 0:
break;
default:
return JobExecutionResult.FailedToStart;
}
while (true)
{
using (var jobConnection2 = new SqlConnection(connectionString))
{
SqlCommand jobCommand2 = new SqlCommand("sp_help_jobactivity", jobConnection2);
jobCommand2.CommandType = CommandType.StoredProcedure;
SqlParameter jobReturnValue2 = new SqlParameter("#RETURN_VALUE", SqlDbType.Int);
jobReturnValue2.Direction = ParameterDirection.ReturnValue;
jobCommand2.Parameters.Add(jobReturnValue2);
SqlParameter jobParameter2 = new SqlParameter("#job_name", SqlDbType.VarChar);
jobParameter2.Direction = ParameterDirection.Input;
jobCommand2.Parameters.Add(jobParameter2);
jobParameter2.Value = jobName;
jobConnection2.Open();
SqlDataReader rdr = jobCommand2.ExecuteReader();
while (rdr.Read())
{
object msg = rdr["message"];
object run_status = rdr["run_status"];
if (!DBNull.Value.Equals(msg))
{
var message = msg as string;
var runStatus = run_status as Int32?;
if (message != null && message.StartsWith("The job succeeded")
&& runStatus.HasValue && runStatus.Value == 1)
{
return JobExecutionResult.Succeeded;
}
else if (message != null && message.StartsWith("The job failed"))
{
return JobExecutionResult.FailedAfterStart;
}
else if (runStatus.HasValue && runStatus.Value == 1)
{
return JobExecutionResult.Unknown;
}
}
}
}
System.Threading.Thread.Sleep(waitFor);
}
}
}
}
Note that you may need database/server owner permissions or something like that for this code to work.

Related

'sqlConnection.InnerConnection.ServerVersionNormalized' Threw an Exception (Connection open, sqldatareader not working)

This seems to be a problem either with my computer or the azure sql servers. The problem I have was not a problem before but the code all of a sudden acting strange. Here is my code that is used to work. Connection opens successfully, my executenonquery works fine but reader acts strange.
public static List<ClientDto> GetClientInformation()
{
List<ClientDto> results = new List<ClientDto>();
try
{
using (var sqlConnection =
new SqlConnection(ConfigurationManager.ConnectionStrings["ClientData"].ConnectionString))
{
using (var command = new SqlCommand(null, sqlConnection))
{
sqlConnection.Open();
command.CommandType = CommandType.StoredProcedure;
//running the GetClientInformation stored procedure in DB
command.CommandText = Constants.GetClientInformation;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
results.Add(new ClientDto()
{
Id = Convert.ToInt32(reader["Id"].ToString()),
DealerName = reader["DealerName"].ToString(),
GroupID = reader["GroupID"].ToString(),
DealerId = reader["DealerId"].ToString(),
DealerFolderName = reader["DealerFolder"].ToString(),
DMSType = reader["DMSType"].ToString(),
MAIsActive = Convert.ToBoolean(Convert.ToInt32(reader["MAIsActive"])),
SalesIsActive = Convert.ToBoolean(Convert.ToInt32(reader["SalesIsActive"])),
SalesSource = reader["SalesSource"].ToString(),
InventoryIsActive = Convert.ToBoolean(Convert.ToInt32(reader["InventoryIsActive"])),
InventorySource = reader["InventorySource"].ToString(),
AppointmentsIsActive =
Convert.ToBoolean(Convert.ToInt32(reader["AppointmentsIsActive"])),
AppointmentsSource = reader["AppointmentsSource"].ToString(),
LeadsIsActive = Convert.ToBoolean(Convert.ToInt32(reader["LeadsIsActive"])),
LeadsSource = reader["LeadsSource"].ToString(),
ServiceIsActive = Convert.ToBoolean(Convert.ToInt32(reader["ServiceIsActive"])),
ServiceSource = reader["ServiceSource"].ToString(),
SalesIsDelete = Convert.ToBoolean(Convert.ToInt32(reader["SalesIsDelete"])),
SalesDeleteRange = reader["SalesDeleteRange"].ToString(),
InventoryIsDelete = Convert.ToBoolean(Convert.ToInt32(reader["InventoryIsDelete"])),
InventoryDeleteRange = reader["InventoryDeleteRange"].ToString(),
AppointmentsIsDelete =
Convert.ToBoolean(Convert.ToInt32(reader["AppointmentsIsDelete"])),
AppointmentsDeleteRange = reader["AppointmentsDeleteRange"].ToString(),
LeadsIsDelete = Convert.ToBoolean(Convert.ToInt32(reader["LeadsIsDelete"])),
LeadsDeleteRange = reader["LeadsDeleteRange"].ToString(),
ServiceIsDelete = Convert.ToBoolean(Convert.ToInt32(reader["ServiceIsDelete"])),
ServiceDeleteRange = reader["ServiceDeleteRange"].ToString(),
IsActive = Convert.ToBoolean(Convert.ToInt32(reader["IsActive"])),
UserDefinedName = reader["UserDefinedName"].ToString()
});
}
}
}
return results;
}
catch (Exception ex)
{
//If an exception happens it will insert the error to errorlog table and to the log file
Logger.WriteError(new ErrorDto()
{
Source = "GetClientInformation",
Message = ex.Message,
StackTrace = ex.StackTrace
});
Console.WriteLine(string.Format("Error - {0} - {1} ", "GetClientInformation", ex.Message));
return results;
}
}
There is data in the sql, the executable of my application works fine on our virtual machine (Windows Server).
Only strange thing I see in the code is that SQLConnection have this innerexception (it is not stopping the code to run though)
ServerVersionNormalized = 'sqlConnection.InnerConnection.ServerVersionNormalized' threw an exception of type 'System.NotSupportedException'
I am not sure if this was an issue before or not.
This picture shows that Enamration yielded no result
This picture shows that reader has rows
I am reinstalling my visual studio now to see if it will fix the issue.
This issue is happening in both Visual Studio 2015 and 2017 for me.
This is my connection string format:
Server=.database.windows.net,1433;Database=; User ID=;Password=;Trusted_Connection=False; Encrypt=True;
It looks like it is working fine now. To me it sounds like since I had breakpoint in the code, it might have caused the issue because of the longer time between connecting and running the query.

Create a local db in winform and get data from server in the background

I'm looking for a way to enhance the performance of my application which excessively uses the database that is hosted on a server, The application needs to remotely access the database thus, causing it to be slow, so I was thinking about creating a local database and populating it from the server the minute the application is run, and afterwards performing updates on a regular basis to the hosted mySQl database after every hour or when the user decides to logout, the main issue I have is I will be having 10-20 users, they don't update the same kind of data but how will I know which tables have been updated and according to that I would apply the changes over my hosted database? and is there any article or link that has further explanation regarding this issue?
My Application is a C# windows form application and the database is mysql database.
One of the queries that I have and takes too long to execute is this one:
/**
* getting the schedule based on the submitted id
* */
public static Schedule2 getTeachersSchedule(String therapistID, int weekday, int period, int school_year)
{
// connecting to mysql database
try
{
using (MySqlConnection myConn = new MySqlConnection(connectionString))
{
using (MySqlCommand command = new MySqlCommand("Select * FROM student_schedule2, weekday, school_year, period, task where school_year_id = school_year_id_fk AND therapist_id_fk =" + therapistID + " AND weekday_id = weekday_id_fk AND period_id=period_id_fk AND task_id=task_id_fk AND weekday_id=" + weekday + " AND period_id=" + period + " AND school_year_id =" + school_year, myConn))
{
MySqlDataReader reader;
myConn.Open();
reader = command.ExecuteReader();
Schedule2 schedule = null;
while (reader.Read())
{
schedule = new Schedule2();
schedule.ID = reader.GetInt32("student_schedule2_id");
try
{
if (reader["student_id_fk"] != DBNull.Value)
schedule.student = getStudent(reader.GetString("student_id_fk"));
else
schedule.student = null;
Weekday weekDay = new Weekday();
weekDay.ID = reader.GetInt32("weekday_id");
weekDay.weekdayName = reader.GetString("weekday_name");
schedule.weekday = weekDay;
schedule.semesterName = reader.GetString("semester_name");
Period periodObj = new Period();
SchoolYear schoolYEar = new SchoolYear();
periodObj.ID = reader.GetInt32("period_id");
periodObj.period_name = reader.GetString("period_name");
schedule.period = periodObj;
schoolYEar.ID = reader.GetInt32("school_year_id");
schoolYEar.year_name = reader.GetString("school_year_name");
schedule.schoolYear = schoolYEar;
Task course = new Task();
course.ID = reader.GetInt32("task_id");
course.taskName = reader.GetString("task_name");
schedule.task = course;
schedule.therapist = getTherapist(reader.GetString("therapist_id_fk"));
}
catch
{
}
}
myConn.Close();
return schedule;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
Your approach will be prone to stale data and most likely be slower, since all data will be sent over the network.
I suggest you look at your SQL queries and determine why they are slow. Adding indexes to the table will help enormously.

How to unlock SQL Server 2008 table in ADO.NET/C# 4.0 using SqlClient?

I created a method while back that:
Locked a table
Read value from it
Wrote updated value back
Unlocked the table
The code worked for Oracle. Now I can't get it work for SQL Server 2008. The method is below and executing my unlocking command results in a SqlException with text:
"NOLOC" is not a recognized table hints option. If it is intended as a
parameter to a table-valued function or to the CHANGETABLE function,
ensure that your database compatibility mode is set to 90.
Code:
public static int GetAndSetMaxIdTable(DbProviderFactory factory, DbConnection cnctn, DbTransaction txn, int tableId, string userName, int numberOfIds)
{
bool isLocked = false;
string sql = string.Empty;
string maxIdTableName;
if (tableId == 0)
maxIdTableName = "IdMax";
else
maxIdTableName = "IdMaxTable";
try
{
bool noPrevRow = false;
int realMaxId;
if (factory is OracleClientFactory)
sql = string.Format("lock table {0} in exclusive mode", maxIdTableName);
else if (factory is SqlClientFactory)
sql = string.Format("select * from {0} with (TABLOCKX)", maxIdTableName);
else
throw new Exception(string.Format("Unsupported DbProviderFactory -type: {0}", factory.GetType().ToString()));
using (DbCommand lockCmd = cnctn.CreateCommand())
{
lockCmd.CommandText = sql;
lockCmd.Transaction = txn;
lockCmd.ExecuteNonQuery();
isLocked = true;
}
using (DbCommand getCmd = cnctn.CreateCommand())
{
getCmd.CommandText = CreateSelectCommand(factory, tableId, userName, getCmd, txn);
object o = getCmd.ExecuteScalar();
if (o == null)
{
noPrevRow = true;
realMaxId = 0;
}
else
{
realMaxId = Convert.ToInt32(o);
}
}
using (DbCommand setCmd = cnctn.CreateCommand())
{
if (noPrevRow)
setCmd.CommandText = CreateInsertCommand(factory, tableId, userName, numberOfIds, realMaxId, setCmd, txn);
else
setCmd.CommandText = CreateUpdateCommand(factory, tableId, userName, numberOfIds, realMaxId, setCmd, txn);
setCmd.ExecuteNonQuery();
}
if (factory is OracleClientFactory)
sql = string.Format("lock table {0} in share mode", maxIdTableName);
else if (factory is SqlClientFactory)
sql = string.Format("select * from {0} with (NOLOC)", maxIdTableName);
using (DbCommand lockCmd = cnctn.CreateCommand())
{
lockCmd.CommandText = sql;
lockCmd.Transaction = txn;
lockCmd.ExecuteNonQuery();
isLocked = false;
}
return realMaxId;
}
catch (Exception e)
{
...
}
}
So what goes wrong here? Where does this error come from? Server or client? I copied the statement from C code and it's supposed to work there. Unfortunately I can't debug and check if it works for me.
Edit: Just trying to lock and unlock (without reading or updating) results in same exception.
Thanks & BR -Matti
The TABLOCKX hint locks the table as you intend, but you can't unlock it manually. How long the lock stays on depends on your transaction level. If you don't have an active transaction on your connection, the lock is held while the SELECT executes and is discarded thereafter.
If you want to realize the sequence "lock the table -> do something with the table -> release the lock" you would need to implement the ADO.NET equivalent of this T-SQL script:
BEGIN TRAN
SELECT TOP (1) 1 FROM myTable (TABLOCKX, KEEPLOCK)
-- do something with the table
COMMIT -- This will release the lock, if there is no outer transaction present
you can either execute the "BEGIN TRAN"/"COMMIT" through DbCommand objects or you can use the System.Data.SqlClient.SqlTransaction class to start a transaction and commit it.
Attention: This approach only works if your connection is not enlisted in a transaction already! SQL Server doesn't support nested transaction, so the COMMIT wouldn't do anything and the lock would be held. If you have a transaction already running, you cannot release the lock until the transaction finishes. In this case maybe a synchronisation through sp_getapplock/sp_releaseapplock might help.
Edit: If you want to educate yourself about transactions, locking and blocking, I recommend these two videos: http://technet.microsoft.com/en-us/sqlserver/gg545007.aspx and http://technet.microsoft.com/en-us/sqlserver/gg508892.aspx
Here is answer for one table for SqlClient with code I made based on TToni's answer:
public static int GetAndSetMaxIdTable(DbProviderFactory factory, DbConnection cnctn, DbTransaction txn, int numberOfIds)
{
bool noPrevRow = false;
int realMaxId;
using (DbCommand getCmd = cnctn.CreateCommand())
{
getCmd.CommandText = "SELECT MaxId FROM IdMax WITH (TABLOCKX)"
getCmd.Transaction = txn;
object o = getCmd.ExecuteScalar();
if (o == null)
{
noPrevRow = true;
realMaxId = 0;
}
else
{
realMaxId = Convert.ToInt32(o);
}
}
using (DbCommand setCmd = cnctn.CreateCommand())
{
if (noPrevRow)
setCmd.CommandText = CreateInsertCommand(factory, tableId, userName, numberOfIds, realMaxId, setCmd, txn);
else
setCmd.CommandText = CreateUpdateCommand(factory, tableId, userName, numberOfIds, realMaxId, setCmd, txn);
setCmd.ExecuteNonQuery();
}
return realMaxId;
}
and it i's like this:
...
try
{
using (txn = cnctn.BeginTransaction())
{
oldMaxId = GetAndSetMaxIdTable(factory, cnctn, txn, 5);
for (i = 0; i < 5; i++)
{
UseNewIdToInsertStuff(factory, cnctn, txn, oldMaxId + i + 1)
}
txn.Commit();
return true;
}
}
catch (Exception e)
{
// don't know if this is needed
if (txn != null && cnctn.State == ConnectionState.Open)
txn.Rollback();
throw e;
}
...
For oracle client it seems to be desirable to have:
SELECT MaxId from IdMax WHERE ... FOR UPDATE OF MaxId
-m

How to get job name and status from sql sever to textbox follow time schedule recurring

Now, i can connect MS SQL Server 2000 via connection string and get job name and job
status in SQL Server Agent. but i want to show name and status continuously follow time
schedule recurring for monitoring JOB. Please suggestion how to do. (using MS Visual
Studio 2008 (C#))
public void ConnectJOB()
{
SQLDMO._SQLServer SQLServer = new SQLDMO.SQLServerClass();
try
{
SQLServer.Connect("10.17.13.70", "sa", "");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
SQLDMO.JobServer jobServer = SQLServer.JobServer;
foreach (SQLDMO.Job job in jobServer.Jobs)
{
string jobName = job.Name;
SQLDMO.SQLDMO_JOBEXECUTION_STATUS status = job.CurrentRunStatus;
richTextBox1.Text = jobName.ToString();
richTextBox2.Text = status.ToString();
}
You might want to check out the SQL Server Agent Job system tables in SQL Server 2000 and grab the information from there.
For an overview of the tables available, check out this wiki post here. The main tables would be sysjobs and sysjobhistory which you can inspect and find the information needed. One thing to watch out for: those tables are in the msdb database and you need fairly elevated privileges to be able to read those.
This query will return all jobs currently in the system, their job_ID (GUID), name, and enabled status:
select job_id, name, enabled
from msdb..sysjobs
Use that information, and join with the sysjobhistory or other relevant tables, to get the information you're looking for.
Update:
OK, here's the complete code sequence
// define the query to run
string sqlJobQuery = "select j.job_id, j.name, j.enabled, jh.run_status " +
"from sysjobs j inner join sysjobhistory jh on j.job_id = jh.jobid"
// create SQL connection and set up SQL Command for query
using(SqlConnection _con = new SqlConnection("server=10.17.13.70;database=msdb;user id=sa;pwd=XXXXXX")
using(SqlCommand _cmd = new SqlCommand(sqlJobQuery, _con))
{
// open connection
_con.Open();
// create SQL Data Reader and grab data
using(SqlDataReader rdr = _cmd.ExecuteReader())
{
// as long as we get information from the reader
while(rdr.Read())
{
Guid jobID = rdr.GetGuid(0); // read Job_id
string jobName = rdr.GetString(1); // read Job name
int jobEnabled = rdr.GetInt(2); // read Job enabled flag
int jobRunStatus = rdr.GetInt(3); // read job run status
// do something with your data, e.g. store it into a list or something
}
rdr.Close();
}
// close connection again
_con.Close();
}
You could use straight T-SQL statements to grab the info about jobs and their history (status) from the sysjobs and sysjobhistory tables in the msdb database - something like this:
public class JobInfo
{
public string Name { get; set; }
public Guid ID { get; set; }
public byte Enabled { get; set; }
public int Status { get; set; }
}
public List<JobInfo> GetJobsAndStatus()
{
List<JobInfo> _jobs = new List<JobInfo>();
string sqlJobQuery = "select j.job_id, j.name, j.enabled, jh.run_state " +
"from sysjobs j inner join sysjobhistory jh on j.job_id = jh.job_id";
// create SQL connection and set up SQL Command for query
using(SqlConnection _con = new SqlConnection("server=10.17.13.70;database=msdb;user id=sa;pwd=XXXXXXX"))
using(SqlCommand _cmd = new SqlCommand(sqlJobQuery, _con))
{
// open connection
_con.Open();
// create SQL Data Reader and grab data
using(SqlDataReader rdr = _cmd.ExecuteReader())
{
// as long as we get information from the reader
while(rdr.Read())
{
Guid jobID = rdr.GetGuid(0); // read Job_id
string jobName = rdr.GetString(1); // read Job name
byte jobEnabled = rdr.GetByte(2); // read Job enabled flag
int jobStatus = rdr.GetInt32(3); // read run_state from jobhistory
_jobs.Add(new JobInfo { Name = jobName,
ID = jobID,
Enabled = jobEnabled,
Status = jobStatus });
}
rdr.Close();
}
// close connection again
_con.Close();
return _jobs;
}
Once you have that list of jobs and their history/status, you can do with it whatever you like, e.g. grab the first one and put the values into textboxes, or whatever you want.

How to monitor SQL Server Agent Job info in C#

I need to create an application for monitoring SQL Server 2000 Agent Job status and info when Job occur same as show on Windows application event log. Now I connect to the database already via a connection string, but I don't know how to get the status and info from Job.
I need to show status and info on Textbox.
What do you suggestion how to do.
Developer tools :
MS SQL Sever 2000 SP4
MS Visual Studio 2008 (C#)
I am a rookie programmer.
i can do this already...
i select form table "Sysjobserver" in database "msdb" for read status, date, time of job that i want.
use this code
public void GetJobsAndStatus()
{
string sqlJobQuery = "select j.job_id, j.name, j.enabled, jh.run_status," +
" js.last_outcome_message, jh.run_date, jh.step_name, jh.run_time" +
" from sysjobs j left join sysjobhistory jh on (j.job_id = jh.job_id)" +
" left join sysjobservers js on (j.job_id = js.job_id)" +
" where jh.run_date = (select Max(run_date) from sysjobhistory)" +
" and jh.run_time = (select Max(run_time) from sysjobhistory)";
// create SQL connection and set up SQL Command for query
using (SqlConnection _con = new SqlConnection("server=10.15.13.70;database=msdb;user id=sa;pwd="))
using (SqlCommand _cmd = new SqlCommand(sqlJobQuery, _con))
{
try
{
// open connection
_con.Open();
SqlConnection.ClearPool(_con);
// create SQL Data Reader and grab data
using (SqlDataReader rdr = _cmd.ExecuteReader())
{
// as long as we get information from the reader
while (rdr.Read())
{
Guid jobID = rdr.GetGuid(0); // read Job_id
string jobName = rdr.GetString(1); // read Job name
byte jobEnabled = rdr.GetByte(2); // read Job enabled flag
int jobStatus = rdr.GetInt32(3); // read last_run_outcome from sysjobserver
string jobMessage = rdr.GetString(4); // read Message from sysjobserver
int jobRunDate = rdr.GetInt32(5); // read run_date from sysjobhistory
string jobStepName = rdr.GetString(6); // read StepName from sysjobhistory
int jobRunTime = rdr.GetInt32(7); // read run_time from sysjobhistory
String[] lviData = new String[] // ตัวแปรอะเรย์ชื่อ lviData
{
jobID.ToString(),
jobName.ToString(),
jobStepName.ToString(),
jobMessage.ToString(),
jobStatus.ToString(),
jobRunDate.ToString(),
jobRunTime.ToString(),
//jobEnabled.ToString(),
};
newData = lviData;
DisplayList(); // for display data on datagridview
}
rdr.Close();
}
}
thank you for everybody help very much. :-D
SQL stored procedures of queries don't give you any system data unless you have db_owner rights on the msdb system database, at lease in SQL Server 2008. Therefore mentioned methods normally don't work for applications where you want to show or manage jobs. However SMO namespace provides you with managed code solution for many SQL Server management features, including the SQL Server Agent functions that only require SQLServerAgent* permissions that you normally could get sorted for your application user. A good intro of using SMO classes to work with jobs is given here:
http://www.codeproject.com/Tips/367470/Manage-SQL-Server-Agent-Jobs-using-Csharp
I work on a similar task now and whilst SQL queries give me access denied, with C# code and Microsoft.SqlServer.Management.Smo.Agent namespace I just listed all jobs with this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo.Agent;
namespace SmoTest
{
class Program
{
static readonly string SqlServer = #"SQL01\SQL01";
static void Main(string[] args)
{
ServerConnection conn = new ServerConnection(SqlServer);
Server server = new Server(conn);
JobCollection jobs = server.JobServer.Jobs;
foreach (Job job in jobs)
{
Console.WriteLine(job.Name);
}
}
}
}
This should be a good starting point to find out how to find your SQL Agent jobs using T-SQL:
View (and disable) SQL Agent Jobs with TSQL
The script will list out all your jobs on your database, and when they will be run next and so forth.
Using the job_name, you should also be able to find out details about your jobs using the SQL Server Agent Stored Procedures in the msdb database on your server.
On SQL Server 2005 and above, you can use the system stored procedure msdb.dbo.sp_help_job to get information, including status, about SQL Server Agent Jobs. You can read more about sp_help_job at http://msdn.microsoft.com/en-us/library/ms186722(v=SQL.90).aspx.
Here is the sample code to do this from C#.
private Dictionary<int, string> ExecutionStatusDictionary = new Dictionary<int, string>()
{
{0, "Not idle or suspended"},
{1, "Executing"},
{2, "Waiting for thread"},
{3, "Between retries"},
{4, "Idle"},
{5, "Suspended"},
{7, "Performing completion actions"}
};
public string GetStatus()
{
SqlConnection msdbConnection = new SqlConnection("Data Source=SERVERNAME;Initial Catalog=msdb;Integrated Security=SSPI");
System.Text.StringBuilder resultBuilder = new System.Text.StringBuilder();
try
{
msdbConnection.Open();
SqlCommand jobStatusCommand = msdbConnection.CreateCommand();
jobStatusCommand.CommandType = CommandType.StoredProcedure;
jobStatusCommand.CommandText = "sp_help_job";
SqlParameter jobName = jobStatusCommand.Parameters.Add("#job_name", SqlDbType.VarChar);
jobName.Direction = ParameterDirection.Input;
jobName.Value = "LoadRegions";
SqlParameter jobAspect = jobStatusCommand.Parameters.Add("#job_aspect", SqlDbType.VarChar);
jobAspect.Direction = ParameterDirection.Input;
jobAspect.Value = "JOB";
SqlDataReader jobStatusReader = jobStatusCommand.ExecuteReader();
while (jobStatusReader.Read())
{
resultBuilder.Append(string.Format("{0} {1}",
jobStatusReader["name"].ToString(),
ExecutionStatusDictionary[(int)jobStatusReader["current_execution_status"]]
));
}
jobStatusReader.Close();
}
finally
{
msdbConnection.Close();
}
return resultBuilder.ToString();
}
You can get a list of all server jobs using this SELECT:
SELECT [name] FROM msdb.dbo.sysjobs
If you'd like to get a list of currently running jobs and their information, I would recommend writing a stored procedure in SQL which your application calls. There's a good demonstration here you could use...
http://feodorgeorgiev.com/blog/2010/03/how-to-query-currently-running-sql-server-agent-jobs/
Good luck!
For my use case, I specifically needed to know when the job was finished running, and whether or not it succeeded. Here is my code to do that:
using System;
using System.Data;
using System.Data.SqlClient;
namespace LaunchJobAndWaitTillDone
{
class Program
{
const string connectionString = "Data Source=YOURSERVERNAMEHERE;Initial Catalog=msdb;Integrated Security=SSPI";
const string jobName = "YOURJOBNAMEHERE";
static readonly TimeSpan waitFor = TimeSpan.FromSeconds(1.0);
enum JobExecutionResult
{
Succeeded,
FailedToStart,
FailedAfterStart,
Unknown
}
static void Main(string[] args)
{
var instance = new Program();
JobExecutionResult jobResult = instance.RunJob(jobName);
switch (jobResult)
{
case JobExecutionResult.Succeeded:
Console.WriteLine($"SQL Server Agent job, '{jobName}', ran successfully to completion.");
break;
case JobExecutionResult.FailedToStart:
Console.WriteLine($"SQL Server Agent job, '{jobName}', failed to start.");
break;
case JobExecutionResult.FailedAfterStart:
Console.WriteLine($"SQL Server Agent job, '{jobName}', started successfully, but encountered an error.");
break;
default:
Console.WriteLine($"Unknown result from attempting to run SQL Server Agent job, '{jobName}'.");
break;
}
Console.ReadLine();
return;
}
JobExecutionResult RunJob(string jobName)
{
int jobResult;
using (var jobConnection = new SqlConnection(connectionString))
{
SqlCommand jobCommand;
SqlParameter jobReturnValue;
SqlParameter jobParameter;
jobCommand = new SqlCommand("sp_start_job", jobConnection);
jobCommand.CommandType = CommandType.StoredProcedure;
jobReturnValue = new SqlParameter("#RETURN_VALUE", SqlDbType.Int);
jobReturnValue.Direction = ParameterDirection.ReturnValue;
jobCommand.Parameters.Add(jobReturnValue);
jobParameter = new SqlParameter("#job_name", SqlDbType.VarChar);
jobParameter.Direction = ParameterDirection.Input;
jobCommand.Parameters.Add(jobParameter);
jobParameter.Value = jobName;
jobConnection.Open();
try
{
jobCommand.ExecuteNonQuery();
jobResult = (Int32)jobCommand.Parameters["#RETURN_VALUE"].Value;
}
catch (SqlException)
{
jobResult = -1;
}
}
switch (jobResult)
{
case 0:
break;
default:
return JobExecutionResult.FailedToStart;
}
while (true)
{
using (var jobConnection2 = new SqlConnection(connectionString))
{
SqlCommand jobCommand2 = new SqlCommand("sp_help_jobactivity", jobConnection2);
jobCommand2.CommandType = CommandType.StoredProcedure;
SqlParameter jobReturnValue2 = new SqlParameter("#RETURN_VALUE", SqlDbType.Int);
jobReturnValue2.Direction = ParameterDirection.ReturnValue;
jobCommand2.Parameters.Add(jobReturnValue2);
SqlParameter jobParameter2 = new SqlParameter("#job_name", SqlDbType.VarChar);
jobParameter2.Direction = ParameterDirection.Input;
jobCommand2.Parameters.Add(jobParameter2);
jobParameter2.Value = jobName;
jobConnection2.Open();
SqlDataReader rdr = jobCommand2.ExecuteReader();
while (rdr.Read())
{
object msg = rdr["message"];
object run_status = rdr["run_status"];
if (!DBNull.Value.Equals(msg))
{
var message = msg as string;
var runStatus = run_status as Int32?;
if (message != null && message.StartsWith("The job succeeded")
&& runStatus.HasValue && runStatus.Value == 1)
{
return JobExecutionResult.Succeeded;
}
else if (message != null && message.StartsWith("The job failed"))
{
return JobExecutionResult.FailedAfterStart;
}
else if (runStatus.HasValue && runStatus.Value == 1)
{
return JobExecutionResult.Unknown;
}
}
}
}
System.Threading.Thread.Sleep(waitFor);
}
}
}
}
Note that you may need database/server owner permissions or something like that for this code to work.

Categories

Resources