Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.
I have two databases, in client and server. Each time im adding something in client database, i want to update it in server, so im running Sql tablediff.exe.

C#
for (int i = 0; i < diffList.Count; i++)
         {

             var proc = new Process()
             {
                 StartInfo = new ProcessStartInfo
                 {
                     FileName = "tablediff.exe",
                     Arguments = "here is all arguments",
                     UseShellExecute = false,
                     RedirectStandardOutput = true,
                     CreateNoWindow = true,

                 }

             };

             proc.Start();

         }

That works fine. Creating i-files, which contains queries. Now i want to read text from all that *.sql files and using sqlcommand execute them - and there is problem. When im trying like that (just running code, no breakpoints etc):


C#
for (int i = 0; i < 11; i++)
                 {
                     try
                     {
                         string text = System.IO.File.ReadAllText("diff"+i+".sql");
     
                         SqlConnection conn = new SqlConnection(cString.connString());
                         using (conn)
                         {
                             conn.Open();
                             SqlCommand cmd = new SqlCommand(text, conn);
                             cmd.ExecuteNonQuery();
                         }
     
                     }
                     catch { MessageBox.Show("No file diff" + i); }
                 }



It doesnt work. BUT.... When i put breakpoin in line conn.Open();, everything works just fine... I have no ide hwo to handle it. Can anyone help? ;)
Posted
Comments
_Zorro_ 19-Oct-12 11:44am    
Where's your datediff? What exactly isn't working?
trucha13657 19-Oct-12 16:49pm    
reading file, getting all text which is in this file and using it to update/insert or delete in database using query. when I just run application and click button to get data - doesnt work. when i put breakpoint in conn.open - works fine...

1 solution

The problem with your using statement as it inside a loop.


C#
for (int i = 0; i < 11; i++)
                 {
                     try
                     {
                         string text = System.IO.File.ReadAllText("diff"+i+".sql");
     
                         using(SqlConnection conn = new SqlConnection(cString.connString()))
                         {
                             conn.Open();
                             SqlCommand cmd = new SqlCommand(text, conn);
                             cmd.ExecuteNonQuery();
                         }
     
                     }
                     catch { MessageBox.Show("No file diff" + i); }
                 }
 
Share this answer
 
Comments
trucha13657 19-Oct-12 16:49pm    
Thanks for answer, ill check that soon. hope that works. by the way maybe you know any other free way to get difference between 2 same database and update/insert/delete data from one to another?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900