I need to write a batch file which run automatically at particular time of day and i got many solutions from forums which are working fine but in my case i need to write SQL query which is quite complex i state my c# code which i need to run and i need it in SQL query
My C# code is looking as:
public void mark_absent()
{
int userid = 0, Last_rec = Last_Record();
bool isapproved;
cmd = new SqlCommand("select top(1) UserID,IsApproved from tblUser ", conn);
conn.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
userid = Convert.ToInt32(dr["UserID"].ToString());
isapproved = Convert.ToBoolean(dr["IsApproved"].ToString());
dr.Close();
conn.Close();
}
else
{
dr.Close();
conn.Close();
}
for (int i = userid; i <= Last_rec; i++)
{
cmd = new SqlCommand("select UserID,IsApproved from tblUser where UserID='" + userid + "' and IsApproved='True'", conn);
conn.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
dr.Close();
conn.Close();
cmd = new SqlCommand("select UserID,AtnDate from tblAttend where convert(date,AtnDate)='" + DateTime.Now.Date + "' and UserID='" + userid + "'", conn);
conn.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
conn.Close();
dr.Close();
//count++;
}
else
{
dr.Close();
conn.Close();
cmd = new SqlCommand("insert into tblAttend (AtnDate,CheckIn,ChkOut,Status,Workhrs,ExtraHrs,UserID,CrtUpDate) values ('" + DateTime.Now + "','00:00:00','00:00:00','Absent','00:00:00','-09:00:00','" + userid + "','" + DateTime.Now + "')", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
else
{
conn.Close();
dr.Close();
}
userid = userid + 1;
}
}
In batch file i found only select or delete or insert command but here i need sqldatareader as well as looping
so how can i write this c# code in SQL query. If i am wrong than what is best solution you people will suggest me...?
Thanks for your help.