Link to home
Start Free TrialLog in
Avatar of IzzyTwinkly
IzzyTwinklyFlag for United States of America

asked on

Why isn't this query working?

Hi I got this query from SQL mgmt. studio, but when I executed the code, myDB.bak was created, showing myDB.bak with 0KB, then it disappeared.  Why my code is not working?

I am using MS Windows Server 2003

using System;
using System.Data.SqlClient;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string conStr = "server = localhost; database = myDB; user id = sa; password =  ";
                string query = @"BACKUP DATABASE [myDB] TO  DISK = N'C:\myDB.bak' WITH NOFORMAT, INIT,  NAME = N'myDB-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10";
                SqlConnection conn = new SqlConnection(conStr);
                conn.Open();

                SqlCommand comm = new SqlCommand(query, conn);
                comm.ExecuteNonQuery();


                conn.Close();
            }
            catch (Exception e)
            {
                e.Message.ToString();
            }
        }
    }
}
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

the backup process might be talking more than 30 seconds - the default for command timeout
set the command timeout to 0 (infinite) and try again
Avatar of IzzyTwinkly

ASKER

could you please tell me how? Also, it's working well on my desktop.  It's just not working on the server.
ASKER CERTIFIED SOLUTION
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry forgot to ask one thing - how much time its takes if you run the queru from the query window on the server machine
Thanks raqi0017!

definitely less than 30 seconds.  I would say around 5-10sec.  I just don't understand why it's working on my desktop and not working on the server.
try the command timeout option and see if it works
You are the Man!  Thanks~~~~~~~~~~~~~~~~!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!