Multiple Commands with SqlCommand

I need to execute several commands that come in a String, in this case I need to generate Views... the precise subject that the SqlCommand does not allow it, somebody knows as I can solve this?

            //My Multiple Commands
            StringBuilder _sqlQuery = new StringBuilder();          
            _sqlQuery.Append("CREATE VIEW WS_{NAME} AS SELECT * FROM {NAME} " + Environment.NewLine);
            _sqlQuery.Append("CREATE VIEW MXT_{NAME}_INET AS SELECT * FROM {NAME} " + Environment.NewLine);

            //My Tables To Views
            List<String> _tableToViews = new List<string>();
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ClientConnectionString"].ConnectionString))
                foreach (String _sView in _tableToViews)
                    using (SqlCommand cmdAdapter = new SqlCommand(_sqlQuery.ToString().Replace("{NAME}",_sView) , conn))
                        cmdAdapter.CommandType = CommandType.Text;
                        catch ( Exception ex)
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Create a stored procedure and call it with your command passing appropriate parameters if needed?

You'd just have to set your CommandType to CommandType.StoredProcedure
so you'd have
 cmdAdapter.CommandType = CommandType.StoredProcedure;
cmdAdapter.CommandText = "YourSPName";
Guy Hengel [angelIII / a3]Billing EngineerCommented:
SqlCommand can run multiple statements, but some (like CREATE VIEW, CREATE PROCEDURE, CREATE TRIGGER... ) cannot be used, as they have to be run in dedicated batches.
creating a stored procedure trying to do that will result in the same problem, you would need to use dynamic sql in there.

so, you need to run indeed 2 SqlCommands, one for each CREATE VIEW...
but, you can do that with a List<String> the same way you parametrize the tables to be handled...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joffrancoAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.