[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1068
  • Last Modified:

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>();
            _tableToViews.Add("Orders");
            _tableToViews.Add("Products");
            _tableToViews.Add("Region");
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ClientConnectionString"].ConnectionString))
            {
                conn.Open();
                foreach (String _sView in _tableToViews)
                {
                    using (SqlCommand cmdAdapter = new SqlCommand(_sqlQuery.ToString().Replace("{NAME}",_sView) , conn))
                    {
                        cmdAdapter.CommandType = CommandType.Text;
                        Console.WriteLine("Execute:");
                        Console.WriteLine(cmdAdapter.CommandText);
                        try
                        {
                            cmdAdapter.ExecuteNonQuery();
                        }
                        catch ( Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                             
                        }                                              
                    }
                }
            }
            Console.ReadLine();  
0
joffranco
Asked:
joffranco
1 Solution
 
YiogiCommented:
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;
And
cmdAdapter.CommandText = "YourSPName";
0
 
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...
0
 
joffrancoAuthor Commented:
Thank's!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now