Ivan Ivan
Ivan Ivan

Reputation: 11

Synchronize SQL Server and program

I have two scripts that are called in one program, the first script creates a database, and the other is a script to fill the database.

How to know if the first script is finished before launching the second one?

How to wait for second script that the first notice that the process is over? How can server inform the creation of the database program and then proceed with further work? Code

string createDatabasePath = args[0];

        sqlConnectionStr = Properties.Settings.Default.conString;
        SqlConnection conn = new SqlConnection(sqlConnectionStr);
        conn.Open();
        SqlCommand comm = new SqlCommand();
        comm.Connection = conn;
        FileInfo file = new FileInfo(createDatabasePath);
        string script = file.OpenText().ReadToEnd();
        script = script.Replace("GO", "");
        comm.CommandText = script;
        comm.ExecuteNonQuery();

My problem is when executenonquery finiched?how I know ?

Upvotes: 0

Views: 162

Answers (1)

Russell Fox
Russell Fox

Reputation: 5427

We need to know what "in one program" means - is that a set of T-SQL commands or a C# application, etc.? However, the easiest way to make sure one is done before the other is just to combine them into a single procedure, or have the first one call the second one:

...Do the stuff in Process1;
EXEC Process2;

A more complicated option is to alter the second process to check that the new database exists and WAITFOR some time until it's complete:

DECLARE @I INT
SET @I = 0

WHILE @I = 0
BEGIN
    IF OBJECT_ID('NewDB..tblFinalTableCreated') IS NOT NULL 
        SET @I = 1
    ELSE 
        WAITFOR DELAY '00:05:00' --Wait 5 minutes
END

StartProcess2;

Upvotes: 2

Related Questions