翻译我的code为我的批处理文件 [英] translate my code to a batch file for me

查看:114
本文介绍了翻译我的code为我的批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写这在每天的特定时间自动运行的批处理文件,我得到了很多的解决方案,从这些做工精细的论坛,但在我的情况,我需要编写SQL查询,它是相当复杂的我说出我的C#code的我需要运行,我需要它在SQL查询

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

我的C#code正在为:

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;

            }
        }

在批处理文件中我发现只有选择或删除或插入命令,但在这里,我需要SqlDataReader对象以及循环

In batch file i found only select or delete or insert command but here i need sqldatareader as well as looping

所以,我怎么可以这样写C#code。在SQL查询。如果我错了,比什么是最好的解决方案,你的人会建议我...?

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...?

感谢您的帮助。

推荐答案

最简单的解决方案,因为你已经有工作C#code,是创建一个控制台应用程序,并使用现有的code在主()方法。

The simplest solution, since you already have working C# code, is to create a Console Application, and use your existing code in the Main() method.

编译成一个EXE,和某个位置的EXE文件,其中code必须在计算机上运行。

Compile it into an EXE, and place the EXE somewhere on the computer where the code must run.

使用Windows任务计划来安排你的EXE为需要运行。

Use Windows Task Scheduler to schedule your EXE to run as needed.

请确保您添加记录,所以你可以跟踪程序运行时无人值守这种情况发生的任何错误。 NLOG是一个良好的开端。

Make sure you add logging so you can keep track of any errors that happen when the program runs unattended. NLog is a good place to start.

这篇关于翻译我的code为我的批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆