-4

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.

9
  • 1
    This question is unclear. Are you asking how to run SQL in C#, how to execute a program from a batch file, how to schedule a batch file or a program for periodic execution? Commented Jul 23, 2015 at 16:33
  • 2
    To simply schedule something, use the Windows Task Scheduler. Commented Jul 23, 2015 at 16:34
  • No in need to create procedure that will work as similar as this mentioned code worked... Commented Jul 23, 2015 at 16:36
  • 1
    Well, that's not at all what the subject of your question says, did I mention that your question was unclear? Commented Jul 23, 2015 at 16:39
  • 2
    I'm voting to close this question as off-topic because StackOverflow is not "do my work for me" service Commented Jul 23, 2015 at 17:25

3 Answers 3

1

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.

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

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

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.

Sign up to request clarification or add additional context in comments.

2 Comments

this is what i have already an idea but i need expert opinion and suggestions so in my console application i need to give connection of that particular database... am i write ?
Typically you would put the actual connection string in the Connection Strings section of app.config, and load it in your EXE from the config file.
0

If you need to run an application at a specific time each day, and you are using a Microsoft OS, look into the Windows Task Scheduler. You can set a schedule and an action. The action will be to execute your program.

2 Comments

I need to convert my code into SQL procedure.
Can you update your post title to reflect that? It is not clear that you are asking for help on converting your code to a stored procedure.
0

Look into sqlcmd.exe

Example in bacth:

sqlcmd -Q "EXEC dbo.your_sp" -S server -d db -o C:\output.txt

see Create a Stored Procedure

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.