I am running a query to execute/start a SQL Server Agent Job which takes sometimes 4 to 5 hours to finish. I would like to wait until that job finishes and then continue with the program/code.
Here is my query I am running to start the stored procedure:
Dim connectionString As String = "HIDDEN"
Dim con As SqlConnection = New SqlConnection(connectionString)
con.Open()
Dim cmd As SqlCommand = New SqlCommand()
cmd.Connection = con
cmd.CommandText = "USE msdb; EXEC dbo.sp_start_job N'FWP FULL Daily'"
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
con.Close()
// -- WAIT UNTIL STORED PROCEDURE FINISHES --
//Continue Code From Here on..........
How would I do this??
Thank you.
Related
I am trying to run a query/job and need to wait until the job/query is done running to continue onto the next line of code in the VB.NET application. How would I do this?
con.Open()
Dim cmd As SqlCommand = New SqlCommand()
cmd.Connection = con
cmd.CommandText = "USE msdb; EXEC dbo.sp_start_job N'FWP1 Incremental Daily'"
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
con.Close()
(WAIT UNTIL FINISHES) - How do I Code this???
\\Continue Code...........
Application.UseWaitCursor = True
con.Open()
Dim cmd As SqlCommand = New SqlCommand()
cmd.Connection = con
cmd.CommandText = "USE msdb; EXEC dbo.sp_start_job N'FWP1 Incremental Daily'"
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
con.Close()
Application.UseWaitCursor = False
MsgBox("Finished!")
Hope this will help.
I know that that ConfigurationManager.AppSettings("ADOConnectionString") gives the right location for my connection, and family_id is the number 6 (at family_id=6 I know I have the right data in my Product table) so why won't this run past execute reader?
Dim sqlConnection1 As New SqlConnection(ConfigurationManager.AppSettings("ADOConnectionString"))
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "SELECT * FROM [TID].[dbo].[Product] WHERE family_id=#family_id"
cmd.Parameters.AddWithValue("#family_id", family_id)
cmd.CommandType = CommandType.Text
sqlConnection1.Open()
reader = cmd.ExecuteReader()
You haven't attached the connection to your command. One way to do that is:
cmd.Connection = sqlConnection1
Another is to do it as part of the SqlCommand constructor:
Dim sql As String
Dim cmd As SqlCommand
Dim reader As SqlDataReader
sql = "SELECT * FROM [TID].[dbo].[Product] WHERE family_id=#family_id"
cmd = new SqlCommand(sql, sqlConnection1)
Using VB.Net and SQL
Code
myConnection = New SqlConnection(Connection)
myConnection.Open()
Dim myCommand As SqlCommand = New SqlCommand("proc_g_report", myConnection)
Dim myReader As SqlDataReader = myCommand.ExecuteReader()
DTResults.Load(myReader)
Throwing error as "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding"
Error on this line Dim myReader As SqlDataReader = myCommand.ExecuteReader()
And I cannot change the database pool size due to security reason, Please any one advise.
Try Setting CommandTimeout
myConnection = New SqlConnection(Connection)
myConnection.Open()
Dim myCommand As SqlCommand = New SqlCommand("proc_g_report", myConnection)
myCommand.CommandTimeout=0
Dim myReader As SqlDataReader = myCommand.ExecuteReader()
DTResults.Load(myReader)
The error seems to be unrelated but, if you want to call the execution of a stored procedure you need to tell what is the CommandType passed.
The default is CommandType = CommandType.Text, so your command text (proc_g_report) is treated as if it was a SELECT/INSERT or other standard T-SQL statement.
You need to set the CommandType and possibly add the Using Statement around the disposable objects....
Using myConnection = New SqlConnection(Connection)
Using myCommand = New SqlCommand("proc_g_report", myConnection)
myCommand.CommandType = CommandType.StoredProcedure
myConnection.Open()
Using myReader = myCommand.ExecuteReader()
DTResults.Load(myReader)
End Using
End Using
End Using
Of course you could easily test if this is the problem using the Sql Server Management Studio and call that stored procedure from you working PC and check what amount of time is required to complete. If the time is less than 30 seconds (the default timeout for the SqlCommand execution) then the problem is elsewhere and not in the Timeout value.
for (int i = 0; i < purchaseListView.Items.Count; i++)
Connection con = new Connection();
SqlCommand cmd = new SqlCommand();
SqlCommand cmdFifo = new SqlCommand();
con.OpenConnection();
cmd.Connection = con.DataBaseConnection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "insertDetail";
cmdFifo.Connection = con.DataBaseConnection;
cmdFifo.CommandType = CommandType.StoredProcedure;
cmdFifo.CommandText = "insertInToMain";
This is my code and I want to know if the loop affects performance of my software and if this is the right way to call stored procedure in loop.
I have stored the procedure in a class and I want to call it from a form when the save button is clicked and insert 10 items in the database via same stored procedure.
Well, you are opening 10 connections, and it seems that you are not closing them, so you may run out of connections, but im guessing that's not the whole code, could you post the entire for ?
I would suggest you to create a table and insert the whole data into it by iterating through all the inputs ie., try to create a single stored procedure.
Running a for loop multpile times is inefficient and also the database will generate the result set number of times which will affect your performance as well due to network overhead.
You are creating a new connection object for every item which is highly inefficient.
Create one connection object and execute the stored procedure n times with the product details. Alternatively create a stored procedure to accept 10 items and insert the data at that level.
Move this outside the loop (You can access con and cmd inside the loop without creating a new instance:
Connection con = new Connection();
SqlCommand cmd = new SqlCommand();
con.OpenConnection();
cmd.Connection = con.DataBaseConnection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "insertDetail";
Inside your loop, you can add all the parameters to the cmd object.
cmd.Parameters.Add("#Item", SqlDbType.Int);
cmd.Parameters["#Item"].Value = purchaseListView.Items[i];
cmd.ExecuteNonQuery();
You should set up your connection and stored procedure before the loop and only update command parameter values and exec within the loop. Like this:
Connection con = new Connection();
SqlCommand cmd = new SqlCommand();
SqlCommand cmdFifo = new SqlCommand();
con.OpenConnection();
cmd.Connection = con.DataBaseConnection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "insertDetail";
cmd.Parameters.Add("#Item", SqlDbType.Int);
cmdFifo.Connection = con.DataBaseConnection;
cmdFifo.CommandType = CommandType.StoredProcedure;
cmdFifo.CommandText = "insertInToMain";
//now that your connections & commands are set up, you can reuse them within the loop
for (int i = 0; i < purchaseListView.Items.Count; i++)
{
//ToDo:assign any SP parameter values
cmd.Parameters["#Item"].Value = purchaseListView.Items[i];
// ...
//then exec within the loop
cmd.ExecuteNonQuery();
cmdFifo.ExecuteNonQuery();
}
I am writing a windows service that will read records from one table and write them to another table. Problem is when i declare cmd = New SqlCommand(l_sSQL) the code stop executing
but the service will still be running. I can insert into the other table using the service but i cant read from the other table.
Code Sample:
Private Sub dbcon()
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim adapter As New SqlDataAdapter
Dim ds As New DataSet
Dim x As String
Try
con.ConnectionString = "Data Source=xxxx;Initial Catalog=xxxxx;Persist Security Info=True;User ID=sa;Password=xxxxxxx"
con.Open()
cmd.Connection = con
cmd = New SqlCommand()
Dim i As Integer
l_sSQL = "SELECT * ozekimessageout"
cmd = New SqlCommand(l_sSQL)\\stops executing here
adapter.SelectCommand = cmd
adapter.Fill(ds)
For i = 0 To ds.Tables(0).Rows.Count - 1
x = (ds.Tables(0).Rows(i).Item(1))
next
End Sub
Please Help....
Don't you need: SELECT * FROM ozekimessageout
Plus: You are 'newing' cmd twice, there is no need.