示例#1
0
        private bool ExecuteScriptFile(string pathToScriptFile, out string errorMsg)
        {
            StreamReader  streamReader  = null;
            SqlConnection sqlConnection = null;

            using (streamReader = new StreamReader(pathToScriptFile))
            {
                using (sqlConnection = new SqlConnection(this.GetConnectionString()))
                {
                    DbCommand dbCommand = new SqlCommand
                    {
                        Connection     = sqlConnection,
                        CommandType    = CommandType.Text,
                        CommandTimeout = 60
                    };
                    sqlConnection.Open();
                    while (!streamReader.EndOfStream)
                    {
                        try
                        {
                            string text = Install.NextSqlFromStream(streamReader);
                            if (!string.IsNullOrEmpty(text))
                            {
                                dbCommand.CommandText = text;
                                dbCommand.ExecuteNonQuery();
                            }
                        }
                        catch (Exception ex)
                        {
                            errorMsg = ex.Message;
                        }
                    }
                    sqlConnection.Close();
                }
                streamReader.Close();
            }
            errorMsg = null;
            return(true);
        }
示例#2
0
        private bool ExecuteScriptFile(string pathToScriptFile, out string errorMsg)
        {
            System.IO.StreamReader streamReader = null;
            System.Data.SqlClient.SqlConnection sqlConnection = null;
            bool result;

            try
            {
                string applicationPath = Globals.ApplicationPath;
                System.IO.StreamReader streamReader2;
                streamReader = (streamReader2 = new System.IO.StreamReader(pathToScriptFile));
                try
                {
                    System.Data.SqlClient.SqlConnection sqlConnection2;
                    sqlConnection = (sqlConnection2 = new System.Data.SqlClient.SqlConnection(this.GetConnectionString()));
                    try
                    {
                        System.Data.Common.DbCommand dbCommand = new System.Data.SqlClient.SqlCommand
                        {
                            Connection     = sqlConnection,
                            CommandType    = System.Data.CommandType.Text,
                            CommandTimeout = 60
                        };
                        sqlConnection.Open();
                        while (!streamReader.EndOfStream)
                        {
                            string text = Install.NextSqlFromStream(streamReader);
                            if (!string.IsNullOrEmpty(text))
                            {
                                dbCommand.CommandText = text.Replace("$VirsualPath$", applicationPath);
                                dbCommand.ExecuteNonQuery();
                            }
                        }
                        sqlConnection.Close();
                    }
                    finally
                    {
                        if (sqlConnection2 != null)
                        {
                            ((System.IDisposable)sqlConnection2).Dispose();
                        }
                    }
                    streamReader.Close();
                }
                finally
                {
                    if (streamReader2 != null)
                    {
                        ((System.IDisposable)streamReader2).Dispose();
                    }
                }
                errorMsg = null;
                result   = true;
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                errorMsg = ex.Message;
                if (sqlConnection != null && sqlConnection.State != System.Data.ConnectionState.Closed)
                {
                    sqlConnection.Close();
                    sqlConnection.Dispose();
                }
                if (streamReader != null)
                {
                    streamReader.Close();
                    streamReader.Dispose();
                }
                result = false;
            }
            return(result);
        }